PDA

View Full Version : [SOLVED:] Declaring Variable Type - Excel 2013



davis1118
12-05-2019, 08:25 PM
Hello, I have a question regarding declaring the variable type. My code is comparing two sets of data against each other and creating a separate list of any differences found. The data type can be a mix of either a number, number with a letter, or a number with a letter and special character. (i.e. 1, 1-2, 1A, 1A-2) My code is currently working fine as is, and until recently I didn't give this much thought. But now I am wondering if I should be doing something different. Currently, the variable that is used to compare each cell value is declared as a String. So my question is this. Is this, or can this be an issue declaring the variable as a String if the cell value is a number or a number mixed with a letter? I know I said my code is working, but I just want to hear other thoughts on this to know if I SHOULD change something.

As always, thanks for the help/input.

-David

Paul_Hossler
12-05-2019, 09:11 PM
1. If it's working don't mess with it :yes

2. Excel / VBA will cast a number to a string so IMHO you're fine

snb
12-06-2019, 04:06 AM
I would abstain from declaring any variable at all and remove option explicit.
You'll notice the code will still be running flawlessly.

Paul_Hossler
12-06-2019, 01:03 PM
Been discussed here many times :yes

If you're a perfect typist and never make a mistake, you might be able to get away with it

For me, I like as much of the the computer's help as I can get

For ex, I'd have to look hard to see where the bugs are below



'Option Explicit -- commented out
Sub test()
msg1 = "message"
msg2 = "another"


MsgBox msg2 & " " & mgs1
End Sub

Sub test2()
msg1 = "message"
msg2 = "another"


MsgBox msg2 & " " & msgl
End Sub

davis1118
12-06-2019, 08:46 PM
Thank you Paul and snb for the input. As you said Paul, "If it's working don't mess with it". I think I will stick with that approach with this one. If I start to find errors I will deal with them as they come. But for now everything is working. I just wanted to get someone else's viewpoint. I appreciate the input.
-David