Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

Excel VBA Global Range Object 2

Status
Not open for further replies.

JoelTXCive

Civil/Environmental
Jul 24, 2016
919
0
16
US
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)
 
Replies continue below

Recommended for you

Hi,

"current active cell"
Code:
If ActiveCell.Value = FtInch_HotCell.Value Then
   'Do this if the values are equal
Else
   'Do this if they are not equal
End If

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:
If Active.Cell.Value = [FtInch_HC].Value Then
'.....

Skip,
[sub]
[glasses]Just traded in my OLD subtlety...
for a NUance![tongue][/sub]
 
It worked! I had to change the property from FtInch_Hotcell.Value to *.ADDRESS, but it worked! The object variables seem to be finicky.

Thank you! Here's the working code:

Sub FtAndInch()

MsgBox "FtInch_Hotcell: " & FtInch_HotCell.Address

'Collect current active cell and compare to global hotcell.
'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
 
Just a suggestion: since the only property of the range that you are interested in is the address, why not just store the address (as a string), rather than creating a range object?

That will make the code simpler, quicker, and less like to suffer from unintended outcomes.

Doug Jenkins
Interactive Design Services
 
I'm open to using a string variable versus the range variable. I'm just learning as a programmer and was worried the string value might mess up further operations in the code.

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?

Capture_dri3ur.jpg
 
OK, if you are reading and writing from/to that cell (or an offset) it makes sense to create a range object. You could use the address string inside Range().Offset().Value, but it's probably easier to do it as you have done.

Doug Jenkins
Interactive Design Services
 
???
To start with, I just put a random cell in the FtInch_HotCell range

"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:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)

End Sub

This code will run each time you change your selection on this sheet. In your particular instance...
Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Not Intersect(FtInch_HotKey, Target) Is Nothing Then
        'do this if there's an intersection
    Else
        'do this if there's NO intersection
    End If
End Sub

New vistas of coding.......

Skip,
[sub]
[glasses]Just traded in my OLD subtlety...
for a NUance![tongue][/sub]
 
SkipVought - Random was a poor choice of words by me. I should have said I wanted to initiate the range variable with an arbitrary cell address of A999. I just wanted to put a cell address in the FtInch_Hotcell range so that comparisons could be made. The cell address in that range variable gets updated every time the program runs.

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.
 
I'm stuck again. Maybe I am not cut out to be a programmer and should stick with the engineering.

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.

VBA_Scan_Temp_ned0ai.jpg


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),""# #/###"") &"""""
 
Code:
'
    Dim ref As String
    
    ref = "B3"[b][/b]
    
    FtInch_HotCell.Offset(Offset_Y, Offset_X).Formula = _
"=INT([highlight #E9B96E]" & ref & "[/highlight]) & [highlight #FCE94F]ch[b]a[/b]r[/highlight](39) & "" - "" & TEXT(12*MOD(MROUND([highlight #E9B96E]" & ref & "[/highlight],1/(32*12)),1),""# #/###"") &"""""


Skip,
[sub]
[glasses]Just traded in my OLD subtlety...
for a NUance![tongue][/sub]
 
SkipVought! You are a genius!

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.
 
Okay, I'll upload the file on here when I finish. It will probably take me a few more evenings to get it all up and running with the code cleaned up and commented well.
 
Here is the Beta version of the spreadsheet. Everything is up and running.

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.
 
 http://files.engineering.com/getfile.aspx?folder=43afa60e-4333-4aeb-9c86-c0300276bfbe&file=Foot_Inch_Beta_2017_08_04.xlsm
Thanks Joel, very nice.

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
 
IDS -

Thank you for the compliments!
Man, those are some awesome spreadsheets you have! That is some cool stuff for sure!
 
Status
Not open for further replies.
Back
Top