Which Variable to use?

AndrewKent

Well-known Member
Joined
Jul 26, 2006
Messages
889
Hi there,

On my last few threads I have been asking questions about how to best declare variables. After reading through the excellent content on C Pearsons website I now have a better understanding of how to declare a variable.

What it doesn't give me however is a list of what variables to use in certain situations. For example...

Text would be As String
Number would be As Intiger
True would be As Boolean

...does anyone have a list of what variables to use and when?

Andy
 
Cheers,

Code:
Set AddStatusNumber = Worksheets("Status History").Range("B4: B" & Range("B65536").End(xlUp).Offset(1, 0).Row & "").Find(What:=CheckStatusNumber)

Range?

PS - You have no idea how much this is helping!!!
 
Upvote 0

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
Yes.
As an aside, I would suggest you adopt a naming convention so that it is always clear what type your variables are - for example:
rngAddStatusNumber (Range)
lngActiveRow (Long)
and so on.
 
Upvote 0
Rorya - Stunning piece of advice, I'll get on that...

Okay next...

Code:
StatusNumber = ActiveRow - 4

Both integer?
 
Upvote 0
I would use Long - a row number could be greater than 32,767 resulting in Integer overflow.
 
Upvote 0
Here's a toughie...

Code:
    Dim Query As String
    
    Query = "SELECT * FROM tblTrackerFundData WHERE Record_ID = '" & Worksheets("Calculation Matrix").Range("CalculationMatrix_Search").Value & "'"

It declares a query in an Access database.........String?
 
Upvote 0
Yes - it's just text.
 
Upvote 0
Gents,

You are the best. Thankfully that's it as I'm now finishing work for the weekend but I expect I'll continue this on Monday, as will some of my programmers!

Again, the help you are providing is top drawer so thank you again.

Andy
 
Upvote 0
Actually what I do need before Monday is a quick justification on why I need to decalre the varables in my program properly. This is so my bosses will actually give the thumbs up to the work.

Now, If I've not decalred the variables properly, (or put Option Explicit in....I know, naughty of me) then VBA defaults each variable not decalred to Variant.

To which my bosses will say "Yeah, but it still works?"

I want to be able to quantify that there will be a performance increase in our application. So, if I've got 2,000 variables that I haven't declared properly (therefore are Dim X As Variant) and they should have been Dim X As String, what sort of effect is me correcting them going to have?

To put you in the picture, I had declared everything as such...

Code:
 Dim Version, Type, Name As String

Where I should have coded as such...

Code:
 Dim Version As String, Type As String, Name As String

So you can see, most of my variables will be Variant by default.
 
Upvote 0

Forum statistics

Threads
1,215,692
Messages
6,126,226
Members
449,303
Latest member
grantrob

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