Running module in If statement in VBA

Billdub417

New Member
Joined
Nov 5, 2019
Messages
45
Hello,

Hopefully a relatively simple question!

I am running the below code (Sub Update()), with an If statement that identifies if there is an error. The only issue is, if it identifies an error, the module that it has run earlier "Toggle-sheets True" remains as it is, rather than reverting back to how it was.

I could do with being able to add the below "Sub toggle()" before the "Exit Sub" - any help greatly appreciated on how to do this!

thanks,

VBA Code:
Sub toggle()
Sheets("sheet1").Select
  If [d6] = "Monthly" Then
            toggle_sheets True
        Else
            toggle_sheets False
            
        End If
End Sub


VBA Code:
Sub Update()
  toggle_sheets True
    Sheets("sheet1").Select
     With Sheet22
        Dim i As Integer
        i = 1
        For Each cell In .Range("v3:v14").Cells
            If cell.value <> 0 Then
                MsgBox ("Period " & i & " does not balance")
                Sheets("sheet2").Select
                Exit Sub
            End If
        i = i + 1
        Next
        Sheets("sheet2").Select
End With
End Sub
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Your description is a bit vague, could you be a bit clearer what exactly you are trying to achieve and in what condition. For example, I see that a "toggle_sheets" procedure is called with a boolean argument, but the code is missing. What is your ultimate goal?
 
Upvote 0
try this:
VBA Code:
Sub Update()
  toggle_sheets True
    Sheets("sheet1").Select
     With Sheet22
        Dim i As Integer
        i = 1
        For Each cell In .Range("v3:v14").Cells
            If cell.Value <> 0 Then
                MsgBox ("Period " & i & " does not balance")
                Sheets("sheet2").Select
                Call toggle
                Exit Sub
            End If
        i = i + 1
        Next
        Sheets("sheet2").Select
End With
End Sub
 
Upvote 0
Solution
try this:
VBA Code:
Sub Update()
  toggle_sheets True
    Sheets("sheet1").Select
     With Sheet22
        Dim i As Integer
        i = 1
        For Each cell In .Range("v3:v14").Cells
            If cell.Value <> 0 Then
                MsgBox ("Period " & i & " does not balance")
                Sheets("sheet2").Select
                Call toggle
                Exit Sub
            End If
        i = i + 1
        Next
        Sheets("sheet2").Select
End With
End Sub
perfect, thanks.
 
Upvote 0

Forum statistics

Threads
1,214,784
Messages
6,121,539
Members
449,038
Latest member
Guest1337

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