Run code from UserForm

DanGK1

Board Regular
Joined
Jul 7, 2010
Messages
60
Hi

I am using checkboxes on a UserForm to call a macro but I have to have the required sheet active for the code to run. Even running the code from Visual Basic requires it and now puzzling me. Any suggestions or observations please


VBA Code:
Sub HideRecurring
Application.ScreenUpdating = false
With Sheets("TestSheet")
lastrow = Range("c" & Rows.Count).End(xlUp).Row
BeginRow = 4
EndRow = lastrow
ChkCol = 5
For RowCnt = BeginRow To EndRow
if Cells(RowCnt, ChkCol).Value = "0" then
Cells(RowCnt,ChkCol).EntireRow.Hidden = True
end if
Next RowCnt
end with
Application.ScreenUpdating = true
end sub
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Your With statement is not doing anything. Anything that is part of the With should have a period at the start like
VBA Code:
With Sheets("TestSheet")
Lastrow = .Range("c" & Rows.Count).End(xlUp).Row
BeginRow = 4
 
Upvote 0
If your going to use With Sheets then any cell in that sheets can be refer to as .Range("A3").Value but if your going to refer to a different sheet called My New Sheet then the code will like Sheets("My New Sheet").Range("A3").Value
 
Upvote 0
I am running based on a checkbox on a UserForm and when the file opens, the sheet seen is Home. I want the code to run on TestSheet but I don’t want to have to ‘activate’ that sheet for the code to run
 
Upvote 0
I have added the period but it still won’t run if not active on the sheet
Did you add it to all the cells, ranges that are referring to that sheet?
 
Upvote 0
Can you also post you amended code?
 
Upvote 0
Yes - here is the amended code
VBA Code:
Sub HideRecurring()
Application.ScreenUpdating = False
With Sheets("TestSheet")
    lastrow = .Range("c" & Rows.Count).End(xlUp).Row
    BeginRow = 4
    EndRow = lastrow
    ChkCol = 5
    For RowCnt = BeginRow To EndRow
        If Cells(RowCnt, ChkCol).Value = "0" Then
            Cells(RowCnt, ChkCol).EntireRow.Hidden = True
        End If
    Next RowCnt
End With
Application.ScreenUpdating = True
End Sub

Sub UnHideRecurring()
Application.ScreenUpdating = False
With Sheets("TestSheet")
    lastrow = .Range("c" & Rows.Count).End(xlUp).Row
    BeginRow = 4
    EndRow = lastrow
    ChkCol = 5
    For RowCnt = BeginRow To EndRow
        If Cells(RowCnt, ChkCol).Value = "0" Then
            Cells(RowCnt, ChkCol).EntireRow.Hidden = False
        End If
    Next RowCnt
End With
Application.ScreenUpdating = True
End Sub
 
Upvote 0
You have not added the period to all the Ranges & cells, which you need to do.
Any object, property or method that belongs to the With must start with a period.
 
Upvote 0
Solution
Ah got it, you mean like this. I wasn't sure Cells applied because I wasn't directly specifying anything there. That has done the trick, thank you very much
VBA Code:
Sub HideRecurring()
Application.ScreenUpdating = False
With Sheets("TestSheet")
    lastrow = .Range("c" & Rows.Count).End(xlUp).Row
    BeginRow = 4
    EndRow = lastrow
    ChkCol = 5
    For RowCnt = BeginRow To EndRow
        If .Cells(RowCnt, ChkCol).Value = "0" Then
            .Cells(RowCnt, ChkCol).EntireRow.Hidden = True
        End If
    Next RowCnt
End With
Application.ScreenUpdating = True
End Sub

Sub UnHideRecurring()
Application.ScreenUpdating = False
With Sheets("TestSheet")
    lastrow = .Range("c" & Rows.Count).End(xlUp).Row
    BeginRow = 4
    EndRow = lastrow
    ChkCol = 5
    For RowCnt = BeginRow To EndRow
        If .Cells(RowCnt, ChkCol).Value = "0" Then
            .Cells(RowCnt, ChkCol).EntireRow.Hidden = False
        End If
    Next RowCnt
End With
Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,402
Messages
6,124,708
Members
449,182
Latest member
mrlanc20

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