Home > Software design >  Skip call macro based on cell value
Skip call macro based on cell value

Time:07-05

I saw 1 similar question but it didn't address my issue so I decided to post it.

  • I have a VBA macro that calls for different macros in a workbook.
  • I would like to skip calling specific macros if specific cells in the "Setup" worksheet are empty.
  • I came up with the code below. The macro runs fine if the cells are not empty but it actually still calls the macros even when the specific cells are empty.

I would greatly appreciate if someone could point me in the right direction! Thank you in advance.

Sub CE_Import()

Dim rng As Range 

Set ws = ThisWorkbook.Sheets("Setup") 
Set rng = Range("A3") 
For Each cell In rng 
If cell.Value <> "" Then 
Call MSI_Import 
End If

Next

Set ws = ThisWorkbook.Sheets("Setup") 
Set rng = Range("D3") 
For Each cell In rng 
If cell.Value <> "" Then 
Call IGH_Import 
End If

Next

Set ws = ThisWorkbook.Sheets("Setup") 
Set rng = Range("G3") 
For Each cell In rng 
If cell.Value <> "" Then 
Call TCell_Import 
End If

Next

Call Hidden_Import

End Sub
'''

CodePudding user response:

You can greatly reduce your current code.

What I'm unsure about is if the later methods are meant to be called even if the previous methods weren't.

E.g. do you want IGH_Import or Hidden_Import to be called even if MSI_Import wasn't called? I think that may be the root of your issue, either that or those cells aren't really empty.

Sub CE_Import()

If ThisWorkbook.Sheets("Setup").Range("A3").Value2 <> "" Then Call MSI_Import
If ThisWorkbook.Sheets("Setup").Range("D3").Value2 <> "" Then Call IGH_Import 
If ThisWorkbook.Sheets("Setup").Range("G3").Value2 <> "" Then Call TCell_Import 

Call Hidden_Import

End Sub

CodePudding user response:

Try below sub-

Sub CE_Import()
Dim ws As Worksheet

Set ws = ThisWorkbook.Sheets("Setup")

    If ws.Range("A3") <> "" Then
        Call MSI_Import
    End If
    
    If ws.Range("D3") <> "" Then
        Call IGH_Import
    End If
    
    If ws.Range("G3") <> "" Then
        Call TCell_Import
    End If

    Call Hidden_Import
    Set ws = Nothing
End Sub
  • Related