Excel VBA Global Range Object
Excel VBA Global Range Object
(OP)
I am having difficulty with range objects and global variables.
My overall goal is to have excel store a “FtInch_Hotcell” which is the most recent cell that a sub-routine has been run on. If I call a specific macro, I want to look at my current activecell location and compare it to the cell residing in the “FtInch_Hotcell.” If the cells are the same, then I want to do procedure A with the cell; and if the cells are different, then I want to do procedure B.
Here is what I have so far:
Upon opening a workbook I am declaring a global range object. I have one line of code in a separate module for global items.
'declare the HotCell as variable available to all subroutines
Public FtInch_HotCell As Range
I then assign a single cell to this range object. To start with, I just put a random cell in the FtInch_HotCell range. This occurs in the ‘workbook open’ module.
Set FtInch_HotCell = ActiveSheet.Range("a999")
In a new procedure, how do I compare the current active cell to the cell residing in ‘FtInch_HotCell’
I have tried several different combinations of logical arguments but cannot seem to get it to work. I’m not even sure I am accessing the global variable correctly.
Thank you.
Ps. Really big picture what I am doing is converting decimal feet to feet and inches. When you run the procedure the first time, the output is in 1/64 inches. The second run yields 1/32 inches and then 1/16... etc.. (if i can ever get it to work)
My overall goal is to have excel store a “FtInch_Hotcell” which is the most recent cell that a sub-routine has been run on. If I call a specific macro, I want to look at my current activecell location and compare it to the cell residing in the “FtInch_Hotcell.” If the cells are the same, then I want to do procedure A with the cell; and if the cells are different, then I want to do procedure B.
Here is what I have so far:
Upon opening a workbook I am declaring a global range object. I have one line of code in a separate module for global items.
'declare the HotCell as variable available to all subroutines
Public FtInch_HotCell As Range
I then assign a single cell to this range object. To start with, I just put a random cell in the FtInch_HotCell range. This occurs in the ‘workbook open’ module.
Set FtInch_HotCell = ActiveSheet.Range("a999")
In a new procedure, how do I compare the current active cell to the cell residing in ‘FtInch_HotCell’
I have tried several different combinations of logical arguments but cannot seem to get it to work. I’m not even sure I am accessing the global variable correctly.
Thank you.
Ps. Really big picture what I am doing is converting decimal feet to feet and inches. When you run the procedure the first time, the output is in 1/64 inches. The second run yields 1/32 inches and then 1/16... etc.. (if i can ever get it to work)
RE: Excel VBA Global Range Object
"current active cell"
CODE
I personally use lots of Named Ranges in my sheets, so I'd name that A999 range FtInch_HC (one time).
Then the only reference and code you'd need is...
CODE
Skip,
Just traded in my OLD subtlety...
for a NUance!
RE: Excel VBA Global Range Object
Thank you! Here's the working code:
Sub FtAndInch()
MsgBox "FtInch_Hotcell: " & FtInch_HotCell.Address
'We will reset the index counter to 1 if it is a new cell.
If ActiveCell.Address <> FtInch_HotCell.Address Then
Set FtInch_HotCell = ActiveCell
SwitchIndex = 1
End If
Call StatusCheck
End Sub
RE: Excel VBA Global Range Object
That will make the code simpler, quicker, and less like to suffer from unintended outcomes.
Doug Jenkins
Interactive Design Services
http://newtonexcelbach.wordpress.com/
RE: Excel VBA Global Range Object
Overall, I am going to pick up the value in the "Hotcell" and then convert it to ft and inches at a user selected level of precison (64ths, 32nds, 16ths, etc). The output will be put in a cell offset from the original cell since it will be a string.
For example, I might pick up the value 36.802 ft. out of cell A1. The macro will convert the 36.802 into 36'-9 5/8" and place the results in cell B1.
The offsetting of the results is what made me think I needed to use the range objects. I have a userform where the user selects relative cell offsets and desired outputs.
Here's the userform where the relative offset values are entered. If I use a string for the cell location will I still be able to offset?
RE: Excel VBA Global Range Object
Doug Jenkins
Interactive Design Services
http://newtonexcelbach.wordpress.com/
RE: Excel VBA Global Range Object
"Random"? You have coded A999 with intention. How is that random? I must be missing something.
You seem to be wanting to be testing for the intersection of your hot cell and the Active Cell. This could be accomplished using sheet events in the code window of the Sheet Object.
Right-click your sheet tab and select View Code.
Directly above of the code window...
...and to the left is the Object drop down. Select Worksheet.
...and to the right is the Procedure Drop Down. You will see in this drop down, a list of events like Change, that will fire when a change of value is made in the worksheet.
For instance, if you were to select SelectionChange, you would see the code stub...
CODE
This code will run each time you change your selection on this sheet. In your particular instance...
CODE
New vistas of coding.......
Skip,
Just traded in my OLD subtlety...
for a NUance!
RE: Excel VBA Global Range Object
Thank you all for your help. I have the whole framework of my program up and running well now. I am still working on some specific sub-procedures, but hopefully I can get them worked out this week. If I get stumped for more than a day, then I'll post another question.
RE: Excel VBA Global Range Object
I cannot figure out how to get VBA to place a formula into a desired cell. Unfortunately, my formula has a single quote and a double quote in it, which confuses things. As soon as Excel sees the single quote, it wants to 'comment out' the remainder of the line.
I have unsuccessfully tried several methods including attempting to build a string and concatenate all together. Any input or suggestions would be appreciated.
Here is the code in text form:
Sample Code on the spreadsheet:
=INT(B3) & "' - " & TEXT(12*MOD(MROUND(B3,1/(32*12)),1),"# #/###") &""""
Current non working version in VBA:
FtInch_HotCell.Offset(Offset_Y, Offset_X).Formula = _
"=INT(B3) & chr(39) & "" - "" & TEXT(12*MOD(MROUND(B3,1/(32*12)),1),""# #/###"") &"""""
RE: Excel VBA Global Range Object
CODE
Skip,
Just traded in my OLD subtlety...
for a NUance!
RE: Excel VBA Global Range Object
Thank you so much. I would have messed around with that syntax for days.
Do you have any need for a spreadsheet that converts decimal feet to feet & inches at 1/8 , 1/16th, etc. accuracy? Once I finish the spreadsheet I can upload it here.
RE: Excel VBA Global Range Object
Glad you got things working.
Skip,
Just traded in my OLD subtlety...
for a NUance!
RE: Excel VBA Global Range Object
RE: Excel VBA Global Range Object
I wrote a macro that converts decimal feet into feet and inches:
You input 25.613 and the program outputs 25’ – 3 13/16”
Enter a decimal foot in a cell and then hit CTRL-SHIFT-S
Running the program for a 2nd, 3rd, etc.. time will cycle through accuracy levels.
The control panel for the program can be reached with a right click.
RE: Excel VBA Global Range Object
The detailed notes on each routine are also very helpful. I recommend the download to anyone interested in VBA programming, even if not interested in feet and inches.
Have you seen my units spreadsheet (copy attached)? It's a bit of a different approach, but you might find some of the routines useful, especially if you want to do conversions outside the very limited built-in Convert function range.
One other thing - you know there is a specific VBA forum? I expect almost everyone who visits there also looks here, but there may be a few people who don't who are missing out.
Doug Jenkins
Interactive Design Services
http://newtonexcelbach.wordpress.com/
RE: Excel VBA Global Range Object
Thank you for the compliments!
Man, those are some awesome spreadsheets you have! That is some cool stuff for sure!