VBA code to work in multiple worksheet

help_andy

New Member
Joined
Jul 5, 2021
Messages
4
Office Version
  1. 365
Platform
  1. Windows
I know next to nothing about VBA

I have VBA code that works in a worksheet and i would like it to work in an identical worksheet but it always gives me an error message

this is the code

Sub PreviousMonth()
If thisworkbook.Sheets("Admin).Range("A3").Value = 1 Then
Exit Sub
Else:
Range("A3").Value = Range("A3").Value - 1
LeaveTracker.Columns("B:NI").Hidden = True
LeaveTracker.Range(Columns(Range("A3").Value * 31 - 29), Columns(Range("A3").Value * 31 + 1)).Hidden = False
End If
End Sub

Sub NextMonth()
If ActiveSheet.Range("A3").Value = 12 Then
Exit Sub
Else:
Range("A3").Value = Range("A3").Value + 1
LeaveTracker.Columns("B:NI").Hidden = True
LeaveTracker.Range(Columns(Range("A3").Value * 31 - 29), Columns(Range("A3").Value * 31 + 1)).Hidden = False
End If
End Sub
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Any line of code that refers to a specific sheet by name, try changing the sheet name to : ActiveSheet.Range("....
 
Upvote 0
As the codes are identical EXCEPT for the Range("A3").value...what will make each one run differently ?
I'm guessing you will need something like
If ActiveSheet.Range("A3").Value <> ?? Then

What are you actually trying to achieve ?
 
Upvote 0
What error message and what line is indicated?
 
Upvote 0
Your line:

VBA Code:
If thisworkbook.Sheets("Admin).Range("A3").Value = 1 Then

Is going to result in an error

Is that the error you are referring to?
 
Upvote 0
appreciate the feedback. thank you

the code works perfectly in the worksheet

but i create an identical copy of that worksheet because i want to use it for different staff and i get - "run time error '1004'" - method 'range' of object'_worksheet' failed"

how do i get it to work in the new worksheet - so working in both identical worksheets but independently of one another?
 
Upvote 0
If you are able to, upload your workbook here <--- and then paste the link that it provides here in the thread so we can see what you are doing.
 
Upvote 0
If you are able to, upload your workbook here <--- and then paste the link that it provides here in the thread so we can see what you are doing.
 
Upvote 0

Forum statistics

Threads
1,214,992
Messages
6,122,631
Members
449,095
Latest member
bsb1122

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