Making a Public variable work as it should

chazrab

Well-known Member
Joined
Oct 21, 2006
Messages
884
Office Version
  1. 365
Platform
  1. Windows
I'm trying to use this as a Public or Global variable
Code:
colnum = Round((Date - DateValue("12/20/2017")) / 7, 0)
I did this:
Insert -> Module:
Code:
Public colnum As Integer
Function UpdateCorrectColumn() As Integer
 colnum = Round((Date - DateValue("12/20/2017")) / 7, 0)
End Function
Result:
Run time error 1004: Application defined or object defined error at this line in my VBA code
Code:
Dim r As Range
Case Is = "Food"
      Set r = Sheets("Budget").Cells(16, colnum) <----bugs out here highlighted yellow

Can anyone tell me what's wrong ? Seems simple enough to declare a Global variable only once in one place only. I keep copying
Code:
 colnum = Round((Date - DateValue("12/20/2017")) / 7, 0)
in VBA code in buttons on12 different Userforms. Having to change each one to a new colnum value at EOY is a real pain.


Thanks for anyone's help.

cr
 
First, it doesn't belong there. It belongs in a normal module (which you can rename, incidentally).
Second, if you put it into Thisworkbook, you have to remember to refer to it as Thisworkbook.colnum every time.
 
Upvote 0

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December

Forum statistics

Threads
1,215,492
Messages
6,125,116
Members
449,206
Latest member
burgsrus

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