"For Each Cell In Range(Cells(etc....)" wont work in userform...(?)

kbishop94

Active Member
Joined
Dec 5, 2016
Messages
476
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
This runs fine in module or worksheet I have...

Code:
Dim lCol As Long
Dim wt As Worksheet
Set wt = Worksheets("Employee Training Matrix")
lCol = ActiveSheet.UsedRange.Columns.Count

With wt
For Each Cell In Range(Cells(4, 7), Cells(4, lCol))

If Cell = "b" Then Columns(Cell.Column).Hidden = True Else Columns(Cell.Column).Hidden = False
Next

End With

but excel doesnt like it one bit when its in a userform... Its probably the structure of how I need to define the range, but I cannot figure it out. :confused:

I get the error: "Compile error: Variable not defined" with "Cell" highlighted in the VBA window.

Please and thank you for any help/suggestions. :)

Keith
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
You need a range variable. Don't use the keyword "Cell" here. Also, you use "With wt" but then don't reference wt the way you should (with dots). Use this:

Code:
Dim rCell As Range

With wt
    For Each rCell In .Range(.Cells(4, 7), .Range(4, lCol))
 
Upvote 0
Awesome, thnk you Jon. (i did reference 'wt' in my code... just forgot to include it in my post. :) )

THanks again

You need a range variable. Don't use the keyword "Cell" here. Also, you use "With wt" but then don't reference wt the way you should (with dots). Use this:

Code:
Dim rCell As Range

With wt
    For Each rCell In .Range(.Cells(4, 7), .Range(4, lCol))
 
Upvote 0

Forum statistics

Threads
1,223,897
Messages
6,175,271
Members
452,628
Latest member
dd2

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