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
 
Ok @help_andy

What do you do that do that file to start giving you the error?

I think I found the issue, Lemme see.
 
Upvote 0

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
@help_andy See if the following works for you:

VBA Code:
Sub PreviousMonth()
'
    Dim ws As Worksheet
    Set ws = Sheets("Leave Tracker")                        ' <--- Set this to sheet name that you want
'
    If ws.Range("A3").Value = 1 Then
        Exit Sub
    Else:
        ws.Range("A3").Value = ws.Range("A3").Value - 1
        ws.Columns("B:NI").Hidden = True
        Range(Columns(ws.Range("A3").Value * 31 - 29), Columns(ws.Range("A3").Value * 31 + 1)).Hidden = False
    End If
End Sub

Sub NextMonth()
'
    Dim ws As Worksheet
    Set ws = Sheets("Leave Tracker")                        ' <--- Set this to sheet name that you want
'
    If ws.Range("A3").Value = 12 Then
        Exit Sub
    Else:
        ws.Range("A3").Value = ws.Range("A3").Value + 1
        ws.Columns("B:NI").Hidden = True
        Range(Columns(ws.Range("A3").Value * 31 - 29), Columns(ws.Range("A3").Value * 31 + 1)).Hidden = False
    End If
End Sub

And what are 'triagle' icons? :)
 
Upvote 0
I'm still a bit confused, but does this do it ??
VBA Code:
Sub PreviousMonth()
Dim ws As Worksheet
Set ws = ActiveSheet
    If ws.Range("A3").Value = 1 Then
        Exit Sub
        Else:
            ws.Range("A3").Value = ws.Range("A3").Value - 1
            ws.Columns("B:NI").Hidden = True
            Range(Columns(ws.Range("A3").Value * 31 - 29), Columns(ws.Range("A3").Value * 31 + 1)).Hidden = False
    End If
If ws.Range("A3").Value = 12 Then
        Exit Sub
    Else:
        ws.Range("A3").Value = ws.Range("A3").Value + 1
        ws.Columns("B:NI").Hidden = True
        Range(Columns(ws.Range("A3").Value * 31 - 29), Columns(ws.Range("A3").Value * 31 + 1)).Hidden = False
    End If
End Sub
 
Upvote 0
@johnnyL
And what are 'triagle' icons? :)

They are the Triangle Shapes either side of the Month text on row 3
 
Upvote 0
LOL I know, I was just pointing out the misspelling in the file. :)
 

Attachments

  • test.PNG
    test.PNG
    5.2 KB · Views: 5
Upvote 0
@Michael M The code you submitted is flawed. :(

For example if A3 > 1 & A3 < 12, Which of the 'Else' statements should be executed?

;)
 
Upvote 0
I suppose the code I submitted could be shortened to:

VBA Code:
Sub PreviousMonth()
'
    Dim ws As Worksheet
    Set ws = Sheets("Leave Tracker")                        ' <--- Set this to sheet name that you want
'
    If ws.Range("A3").Value = 1 Then Exit Sub
'
    ws.Range("A3").Value = ws.Range("A3").Value - 1
    ws.Columns("B:NI").Hidden = True
    Range(Columns(ws.Range("A3").Value * 31 - 29), Columns(ws.Range("A3").Value * 31 + 1)).Hidden = False
End Sub

Sub NextMonth()
'
    Dim ws As Worksheet
    Set ws = Sheets("Leave Tracker")                        ' <--- Set this to sheet name that you want
'
    If ws.Range("A3").Value = 12 Then Exit Sub
'
    ws.Range("A3").Value = ws.Range("A3").Value + 1
    ws.Columns("B:NI").Hidden = True
    Range(Columns(ws.Range("A3").Value * 31 - 29), Columns(ws.Range("A3").Value * 31 + 1)).Hidden = False
End Sub
 
Upvote 0

Forum statistics

Threads
1,216,105
Messages
6,128,859
Members
449,472
Latest member
ebc9

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