Use IF dependent on Sheet Name to Call a VBA Function?

justanotheruser

Board Regular
Joined
Aug 14, 2010
Messages
96
Hi all,

I have some VBA code that I'm using for an Invoice system. As part of the system, it needs to call up a certain function based on the sheet name. I have two choices, I can either make 4 copies of the same code (when you press a button, called "Process Invoice", it runs a macro that runs a few different functions at the same time), and then just change VATCopy1, VATCopy2, VATCopy3, VATCopy4 and assign a different macro to each sheet, or use an IF formula.

So my code is:

Code:
Call VATCopy1 'This will do the VATCopy function and copy the relevant data to the "Database" sheet

Basically, I'm trying to do something that says:
  • if Sheet Name is "VAT", run VATCopy1
  • if Sheet Name is "VAT & Disc", run VATCopy2
  • if Sheet Name is "Zero VAT", run VATCopy3
  • if Sheet Name is "Zero VAT & Disc", run VATCopy4

Those are the four conditions I need to put in my code.

My attempt (for the first 2 sheets) is this:

Code:
Dim wsSheet As Worksheet
    For Each wsSheet In Worksheets
        If wsSheet.Name = "VAT" Then
            Call VATCopy1 'This will do the VATCopy function and copy the relevant data to the "Database" sheet from the sheet named "VAT"
        Else
        If wsSheet.Name = "VAT & Disc" Then
            Call VATCopy2 'This will do the VATCopy function and copy the relevant data to the "Database" sheet from the sheet named "VAT & Disc"
        End If

However this returns the Compile Error Block if without End if.

Can anyone please help? :)

Thanks in advance!
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
Try like this

Code:
Dim wsSheet As Worksheet
    For Each wsSheet In Worksheets
        If wsSheet.Name = "VAT" Then
            Call VATCopy1 'This will do the VATCopy function and copy the relevant data to the "Database" sheet from the sheet named "VAT"
        ElseIf wsSheet.Name = "VAT & Disc" Then
            Call VATCopy2 'This will do the VATCopy function and copy the relevant data to the "Database" sheet from the sheet named "VAT & Disc"
        End If
 
Upvote 0
Try like this

Code:
Dim wsSheet As Worksheet
    For Each wsSheet In Worksheets
        If wsSheet.Name = "VAT" Then
            Call VATCopy1 'This will do the VATCopy function and copy the relevant data to the "Database" sheet from the sheet named "VAT"
        ElseIf wsSheet.Name = "VAT & Disc" Then
            Call VATCopy2 'This will do the VATCopy function and copy the relevant data to the "Database" sheet from the sheet named "VAT & Disc"
        End If

Thanks for the fast response! Unfortunately, now I get a "For without Next" error - is this because all 4 conditions haven't been included? Thanks again. :)
 
Upvote 0
You need to close the For loop

Code:
Dim wsSheet As Worksheet
    For Each wsSheet In Worksheets
        If wsSheet.Name = "VAT" Then
            Call VATCopy1 'This will do the VATCopy function and copy the relevant data to the "Database" sheet from the sheet named "VAT"
        ElseIf wsSheet.Name = "VAT & Disc" Then
            Call VATCopy2 'This will do the VATCopy function and copy the relevant data to the "Database" sheet from the sheet named "VAT & Disc"
        End If
    Next wsSheet
 
Upvote 0
use this

Dim wsSheet As Worksheet
For Each wsSheet In Worksheets
If wsSheet.Name = "VAT" Then
Call VATCopy1
ElseIf wsSheet.Name = "VAT & Disc" Then
Call VATCopy2
ElseIf wsSheet.Name = "Zero VAT" Then
Call VATCopy3
ElseIf wsSheet.Name = "Zero VAT & Disc" Then
Call VATCopy4
End If
Next wsSheet
 
Upvote 0
You need to close the For loop

Code:
Dim wsSheet As Worksheet
    For Each wsSheet In Worksheets
        If wsSheet.Name = "VAT" Then
            Call VATCopy1 'This will do the VATCopy function and copy the relevant data to the "Database" sheet from the sheet named "VAT"
        ElseIf wsSheet.Name = "VAT & Disc" Then
            Call VATCopy2 'This will do the VATCopy function and copy the relevant data to the "Database" sheet from the sheet named "VAT & Disc"
        End If
    Next wsSheet

Thanks, I added the other conditions to the code and ran the macro on the sheet called "VAT & Disc". It ran VATCopy2, but it also ran VATCopy3 and VATCopy4, which is very odd, perhaps I'm using the wrong operator?

Code:
Dim wsSheet As Worksheet
    For Each wsSheet In Worksheets
        If wsSheet.Name = "VAT" Then
            Call VATCopy1 'This will do the VATCopy function and copy the relevant data to the "Database" sheet from the sheet named "VAT"
        ElseIf wsSheet.Name = "VAT & Disc" Then
            Call VATCopy2 'This will do the VATCopy function and copy the relevant data to the "Database" sheet from the sheet named "VAT & Disc"
        ElseIf wsSheet.Name = "Zero VAT" Then
            Call VATCopy3 'This will do the VATCopy function and copy the relevant data to the "Database" sheet from the sheet named "Zero VAT"
        ElseIf wsSheet.Name = "Zero VAT & Disc" Then
            Call VATCopy4 'This will do the VATCopy function and copy the relevant data to the "Database" sheet from the sheet named "Zero VAT & Disc"
        End If
    Next wsSheet

Thanks again. :)
 
Upvote 0
Your code loops through all the worksheets, so if you have all those sheets, all 4 macros will be called.
 
Upvote 0
Instead of looping

Code:
Select Case ActiveSheet.Name
    Case "VAT"
        Call VATCopy1
    Case "VAT & Disc"
        Call VATCopy2
    Case "Zero VAT"
        Call VATCopy3
    Case "Zero VAT & Disc"
        Call VATCopy4
End Select
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,284
Members
452,630
Latest member
OdubiYouth

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