Code error

noelmus

Board Regular
Joined
Dec 30, 2018
Messages
105
Hi,
I have these codes in Workbook

Private Sub Workbook_SheetActivate(ByVal Sh As Object)
Dim ws As Worksheet


Set ws = Worksheets("CHIT")
If Sh.Name = ws.Name Then
ws.Range("C12") = Application.UserName
End If


Set ws = Nothing
End Sub
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Sheets("CHIT").Range("J9,J12,C14,E28,E29").Interior.ColorIndex = 6
End Sub

Now I added this code:

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Dim c As Range, a
Set c = Worksheets("Chit").Range("J8")
c.NumberFormat = "@"
If InStr(c, "/") = 0 Then
c = "000001/" & Format(Date, "yy")
Else
a = Split(c, "/")
a(0) = Format(a(0) + 1, "00000#")
If Format(a(1), "00") <> Format(Date, "yy") Then a(1) = Format(Date, "yy")
c = Join(a, "/")
End If
End Sub

When I added the code above, it gave me this error:

Compile error:
Ambiguous name detected: Workbook_BeforeSave

and the line below became highlighted.

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean

Will someone help me to fix this.

Thanks in advance
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
you can only use

Private Sub Workbook_BeforeSave(

once, same for any other sub (with the exception of if they are in multiple sheets
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top