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
 
Variants use more memory, for one thing and there is an overhead involved in coercion to the relevant type. Whether this will actually have an impact on your app, it's hard to say. More importantly, you might find the wrong types of values being allowed by your code; again, whether this will impact your app is hard to say.
More importantly I think, in the long run it will make your code easier to maintain, so you can either do it now, or you can spend more later (but as a hidden cost!) trying to track down strange errors in your program! :)
 
Upvote 0

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Try and throw the phrase "lower TCO" in there. Seems to work with most IT heads... :)
 
Upvote 0
As promised, I'm back with more questions,

Code:
Message = MsgBox("Warning: Your RACF is not recognised as being part of the Financial Planning Direct department structure" & vbCrLf & vbCrLf & "Please contact the Financial Planning Direct MI Team for assistance.", Config1, "" & VersionName & "")

Would this be object?
 
Upvote 0
Nope - MsgBox returns a VbMsgBoxResult enum, which is essentially a Long integer.
 
Upvote 0
I'm not sure why you would assign that to a variable. If it was

Code:
Message = MsgBox("Warning: Your RACF is not recognised as being part of the Financial Planning Direct department structure" & vbCrLf & vbCrLf & "Please contact the Financial Planning Direct MI Team for assistance.", Config1, "" & VersionName & "", vbYesNo)

I'd say VbMsgBoxResult
 
Upvote 0
Hi guys,

Code:
Item = WorksheetFunction.Index(Range("PolicyDataCapture_PolicySummary"), RowNo, 1)

What would item be declared as in this situation? Long?
 
Upvote 0
Variant is probably better unless you know beforehand what sort of data should be returned.
 
Upvote 0
Thanks that's great,

here's another...

Code:
Confirmation = Application.CountIf(Worksheets("True - False").Range("F8:F11"), "False")
 
Upvote 0

Forum statistics

Threads
1,215,232
Messages
6,123,765
Members
449,120
Latest member
Aa2

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