If Then Otherwise VBA Help

squnibi

Board Regular
Joined
May 26, 2010
Messages
74
I have the following code but its not working:

Code:
Sub test()

Dim Per3 As String
Dim Per4 As String
Dim Per5 As String

Per3 = ThisWorkbook.Sheets("Diagnostics").Range("PerfCode3")
Per4 = ThisWorkbook.Sheets("Diagnostics").Range("PerfCode4")
Per5 = ThisWorkbook.Sheets("Diagnostics").Range("PerfCode5")

    If Per3 <> "" Then
    ThisWorkbook.Sheets("Diagnostics").Range("PerfCode3").Copy
    ThisWorkbook.Sheets("Report").Range("PerTable1").PasteSpecial Paste:=xlPasteValues
     
    ElseIf Per4 <> "" Then
    ThisWorkbook.Sheets("Diagnostics").Range("PerfCode4").Copy
    ThisWorkbook.Sheets("Report").Range("PerTable2").PasteSpecial Paste:=xlPasteValues
    
    ElseIf Per5 <> "" Then
    ThisWorkbook.Sheets("Diagnostics").Range("PerfCode5").Copy
    ThisWorkbook.Sheets("Report").Range("PerTable3").PasteSpecial Paste:=xlPasteValues
    
    Sheets("Report").Activate
    Sheets("Report").Calculate
    Sheets("Charts").Activate
    Sheets("Charts").Calculate
    
    End If
    
    
End Sub
I'm trying to say if Per3 is blank, then check Per4, otherwise copy and paste the value into the specified region.

Then If Per4 is blank, then check Per5, otherwise copy and paste the value into the specified region.

Then If Per5 is blank, do nothing, otherwise copy and paste the value into the specified region.
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
Maybe something like:
Code:
    If Per3 <> "" Then
        ThisWorkbook.Sheets("Diagnostics").Range("PerfCode3").Copy
        ThisWorkbook.Sheets("Report").Range("PerTable1").PasteSpecial Paste:=xlPasteValues
        GoTo Here   'If the code catches this line it'll skip the next parts
    End If
    
    If Per4 <> "" Then
        ThisWorkbook.Sheets("Diagnostics").Range("PerfCode4").Copy
        ThisWorkbook.Sheets("Report").Range("PerTable2").PasteSpecial Paste:=xlPasteValues
        GoTo Here   'If the code catches this line it'll skip the last part
    End If
    
    If Per5 <> "" Then  'None of the abowe has happened
        ThisWorkbook.Sheets("Diagnostics").Range("PerfCode5").Copy
        ThisWorkbook.Sheets("Report").Range("PerTable3").PasteSpecial Paste:=xlPasteValues
    End If
    
Here:
    Sheets("Report").Activate
    Sheets("Report").Calculate
    Sheets("Charts").Activate
    Sheets("Charts").Calculate
I used the line "Here:" as a "bookmark" telling the macro where to go next.
 
Upvote 0

Forum statistics

Threads
1,224,613
Messages
6,179,894
Members
452,948
Latest member
Dupuhini

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