How can you run another sub from an IF statement?

GossopsGreen

New Member
Joined
Jun 6, 2012
Messages
4
Hello All,</SPAN></SPAN>

Can one of you intelligent people help me with a problem I am having on a macro please?</SPAN></SPAN>

I am trying to get an IF statement to run another sub but I cannot get it to work for some reason.</SPAN></SPAN>

Here's basically what I have at the moment……</SPAN></SPAN>

Public Sub CommandButton1_Click()</SPAN></SPAN>
Dim Period As String</SPAN></SPAN>
Period = Application.InputBox("Current Tax Period (in 2 digit format)")</SPAN></SPAN>

If Period <= "06" Then</SPAN></SPAN>
GetDates1</SPAN></SPAN>
ElseIf Period >= "07" Then</SPAN></SPAN>
GetDates2</SPAN></SPAN>
End If</SPAN></SPAN>
End Sub</SPAN></SPAN>

Public Sub GetDates1()</SPAN></SPAN>

If Period = "01" Then</SPAN></SPAN>
Sheets("010").Range("B4").Formula = "='F:\......\Richard\[Paygroup listing.xls]Paygroup Listing'!$G$25"</SPAN></SPAN>

ElseIf Period = "02" Then</SPAN></SPAN>
Sheets("010").Range("B4").Formula = "='F:\......\Richard\[Paygroup listing.xls]Paygroup Listing'!$H$25"</SPAN></SPAN>

End If</SPAN></SPAN>
End Sub</SPAN></SPAN>

I have spent a long time trawling through Google looking for a resolution but no such luck unfortunately.</SPAN></SPAN>

Is the IF statement in the wrong format? Is the "Period" string not automatically carried through to the other sub?</SPAN></SPAN>

At the moment, it does run through, without any errors, but it doesn't input the required formula's anywhere.</SPAN></SPAN>

As you can tell, I'm not the most experienced in VB so your help will be massively appreciated.

Richard</SPAN></SPAN>
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
Try removing the quotes, e.g.

Code:
If CInt(Period) <= 6 Then
    GetDates1
ElseIf CInt(Period) >= 7 Then
    GetDates2
End If
 
Upvote 0
In addition to what VoG said, because you have declared Period in the CommandButton1_Click procedure it isn't visible to the GetDates1 procedure. One way to solve this is to declare your GetDates1 procedure with a parameter like this:

Code:
Public Sub GetDates1(Period)

and call it like this:

Code:
Call GetDates1(Period)
 
Upvote 0
Thanks for the rapid response guys.

I will try both your suggestions tomorrow and let you know accordingly.

Thanks, once again.
 
Upvote 0

Forum statistics

Threads
1,214,905
Messages
6,122,178
Members
449,071
Latest member
cdnMech

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