Excel & VBA Problem
Discussion
I've created a macro which I want to run on three sheets automatically.
So I created another macro:
Sub RunMacroOnMultipleSheets()[
Sheets("First Sheet").Activate
Call Col_Display
Sheets("Second Sheet").Activate
Call Col_Display
Sheets("Third Sheet").Activate
Call Col_Display
End Sub
But when I run it, it fails at the first Call command, telling me "Compile Error. Argument Not Optional".
WTF is an argument?
If it matters, the macro 'Col_Display' begins like this:
Sub Col_Display(ByVal Target As Range)
If Not Application.Intersect(Range("A5"), Range(Target.Address)) Is Nothing Then
Select Case Target.Value
Case Is = "1": Columns("h:r").EntireColumn.Hidden = True
Columns("f:f").EntireColumn.Hidden = False
So I created another macro:
Sub RunMacroOnMultipleSheets()[
Sheets("First Sheet").Activate
Call Col_Display
Sheets("Second Sheet").Activate
Call Col_Display
Sheets("Third Sheet").Activate
Call Col_Display
End Sub
But when I run it, it fails at the first Call command, telling me "Compile Error. Argument Not Optional".
WTF is an argument?
If it matters, the macro 'Col_Display' begins like this:
Sub Col_Display(ByVal Target As Range)
If Not Application.Intersect(Range("A5"), Range(Target.Address)) Is Nothing Then
Select Case Target.Value
Case Is = "1": Columns("h:r").EntireColumn.Hidden = True
Columns("f:f").EntireColumn.Hidden = False
An 'argument' is another word for 'parameter'.
Your Col_Display is expecting a range in order for it to process, and because you're not giving it this parameter, it's giving you an error.
Can't quite remember the exact syntax for a range; it's been a very long time since I've had to do this!
Your Col_Display is expecting a range in order for it to process, and because you're not giving it this parameter, it's giving you an error.
Can't quite remember the exact syntax for a range; it's been a very long time since I've had to do this!
balham123 said:
Normally it would be the cells you want to affect.
Ultimately depends what your function is doing
I want to hide specific columns based upon a value in A5, on each of three sheets.Ultimately depends what your function is doing
So if A5 - 1, I want to hide columns J - R. If A5 = 2, I want to hide I and K-R etc.
I'm trying to run the macro on First Sheet, Second Sheet and Third Sheet in turn, based upon the values in A5 on each sheet. But those cells are linked, so if I change the value in A5 on First Sheet, the values in A5 on the other two sheets will change. But the macro will only run on First Sheet. Nothing happens on the other two.
So I've deleted the RunMacroOnMultipleSheets code and tried the Col_Display macro, with the code repeated three times, separated by
Sheets("First Sheet").Activate
Sheets("Second Sheet").Activate
Sheets("Third Sheet").Activate
It works perfectly (so I know the code is good), but it still only acts upon the first sheet. Third Sheet becomes the active sheet, so I know it's running the entire macro, but it's not hiding any columns on the second two sheets.
You probably want to just loop through all sheets in the workbook
Something like this bit
Sub forEachWs()
Dim ws As Worksheet
For Each ws In ActiveWorkbook.Worksheets
Call resizingColumns(ws)
Next
End Sub
From
https://stackoverflow.com/questions/21918166/excel...
Something like this bit
Sub forEachWs()
Dim ws As Worksheet
For Each ws In ActiveWorkbook.Worksheets
Call resizingColumns(ws)
Next
End Sub
From
https://stackoverflow.com/questions/21918166/excel...
Gassing Station | Computers, Gadgets & Stuff | Top of Page | What's New | My Stuff