SpreedsheetCrusader
Board Regular
- Joined
- Sep 30, 2015
- Messages
- 130
Hi
i am trying to save some ink on a VBA code i am writing a code which is about toggling cells colors on/off by pressing a set of buttons on the sheet (e.g. button 1 toggles cells with yellow color on/off, button 2 toggles cells with green color on/off, and so forth..)
i have this piece of code at the beginning of each Subroutine dedicated to each button to set the range:
this aint going to change between subs it will remain the same so i tried declaring those two variables in a Sub and calling that Sub inside my buttons dedicated Subs see below:
when i do the above i start getting an Error "Argument ByRef" and the code does not work, i highlight on myrange i find it = nothing
do not know what is the problem it is driving me mad, especially that i used the same technique with other projects and it worked smoothly, however this is the first time i pass a variable that is a range.
looking for your help my spreadsheets brothers and sisters.
i am trying to save some ink on a VBA code i am writing a code which is about toggling cells colors on/off by pressing a set of buttons on the sheet (e.g. button 1 toggles cells with yellow color on/off, button 2 toggles cells with green color on/off, and so forth..)
i have this piece of code at the beginning of each Subroutine dedicated to each button to set the range:
Code:
Dim lastrowlastrow = Cells(3, 6).SpecialCells(xlCellTypeLastCell).Row
Dim myrange As Range
Set myrange = Range(Cells(3, 6), Cells(lastrow, 12))
this aint going to change between subs it will remain the same so i tried declaring those two variables in a Sub and calling that Sub inside my buttons dedicated Subs see below:
Code:
Sub Vardef (Lastrow as long,myrange as range)
lastrow = Cells(3, 6).SpecialCells(xlCellTypeLastCell).Row
Set myrange = Range(Cells(3, 6), Cells(lastrow, 12))
end sub
sub ToggleYellow()
Call Vardef (Lastrow,myrange)
.
.
.
.
rest of the code
when i do the above i start getting an Error "Argument ByRef" and the code does not work, i highlight on myrange i find it = nothing
do not know what is the problem it is driving me mad, especially that i used the same technique with other projects and it worked smoothly, however this is the first time i pass a variable that is a range.
looking for your help my spreadsheets brothers and sisters.