Why sometimes the same variables declaration gives a compilation error?Why Use Integer Instead of Long?IF statement with RC in vba gives compile error, why?'Type mismatch' error for Index MatchVBA variable declaration okay on two lines but not when comma separated. Compiler bug?Declaring worksheet variable issuesDeclare (automatically) variables in vbaError: Compile Error,ByRef argument type mismatchWhy do I sometimes get errors when I try to set the value of a variable in VBA?An compile error in VBA after requiring variable declarationWhy are missing `Set` for certain types a runtime error rather than compile error?New Compile Error for “Declare PtrSafe Function” - what changed in Windows?
Fried gnocchi with spinach, bacon, cream sauce in a single pan
Church Booleans
Verb form to finish someone else's sentence?
Why doesn't the Falcon-9 first stage use three legs to land?
Why is 日本 read as "nihon" but not "nitsuhon"?
When translating the law, who ensures that the wording does not change the meaning of the law?
Can you help me understand Modes from the aspect of chord changes?
The teacher logged me in as administrator for doing a short task, is the whole system now compromised?
Is “I am getting married with my sister” ambiguous?
How to write triplets in 4/4 time without using a 3 on top of the notes all the time
Justifying the use of directed energy weapons
Why were movies shot on film shot at 24 frames per second?
Concatenation of the result of a function with a mutable default argument in python
How to persuade recruiters to send me the Job Description?
Why did MS-DOS applications built using Turbo Pascal fail to start with a division by zero error on faster systems?
Defense against attacks using dictionaries
Is it appropriate for a prospective landlord to ask me for my credit report?
Vacuum collapse -- why do strong metals implode but glass doesn't?
Why does my house heat up, even when it's cool outside?
Is there such a thing as too inconvenient?
Why did this happen to Thanos's ships at the end of "Avengers: Endgame"?
Is refusing to concede in the face of an unstoppable Nexus combo punishable?
How to avoid using System.String with Rfc2898DeriveBytes in C#
How would a situation where rescue is impossible be handled by the crew?
Why sometimes the same variables declaration gives a compilation error?
Why Use Integer Instead of Long?IF statement with RC in vba gives compile error, why?'Type mismatch' error for Index MatchVBA variable declaration okay on two lines but not when comma separated. Compiler bug?Declaring worksheet variable issuesDeclare (automatically) variables in vbaError: Compile Error,ByRef argument type mismatchWhy do I sometimes get errors when I try to set the value of a variable in VBA?An compile error in VBA after requiring variable declarationWhy are missing `Set` for certain types a runtime error rather than compile error?New Compile Error for “Declare PtrSafe Function” - what changed in Windows?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
When there is a variable type mismatch compilation error in VBA sometimes the solution is to change how I declare the variables, but not the type.
e.g.:
If I have:
Dim numCables, numCaColumn, firstCableRow, numCable, numDiColumn, posInArray, j, numMaterialWDifSEColumn, numSEMaterials As Integer
And I replace it for:
Dim numCables, numCaColumn, firstCableRow, numCable, numDiColumn, posInArray, j, numMaterialWDifSEColumn As Integer
Dim numSEMaterials As Integer
Now with this change I don't have a compilation error and all is working fine.
I would like to know why this is happening.
Thanks.
vba
add a comment |
When there is a variable type mismatch compilation error in VBA sometimes the solution is to change how I declare the variables, but not the type.
e.g.:
If I have:
Dim numCables, numCaColumn, firstCableRow, numCable, numDiColumn, posInArray, j, numMaterialWDifSEColumn, numSEMaterials As Integer
And I replace it for:
Dim numCables, numCaColumn, firstCableRow, numCable, numDiColumn, posInArray, j, numMaterialWDifSEColumn As Integer
Dim numSEMaterials As Integer
Now with this change I don't have a compilation error and all is working fine.
I would like to know why this is happening.
Thanks.
vba
1
This question pops up all the time, but in such a way that it is seldom an outright duplicate. Arguably VBA made a poor design decision here, but it is what it is.
– John Coleman
Mar 27 at 16:15
add a comment |
When there is a variable type mismatch compilation error in VBA sometimes the solution is to change how I declare the variables, but not the type.
e.g.:
If I have:
Dim numCables, numCaColumn, firstCableRow, numCable, numDiColumn, posInArray, j, numMaterialWDifSEColumn, numSEMaterials As Integer
And I replace it for:
Dim numCables, numCaColumn, firstCableRow, numCable, numDiColumn, posInArray, j, numMaterialWDifSEColumn As Integer
Dim numSEMaterials As Integer
Now with this change I don't have a compilation error and all is working fine.
I would like to know why this is happening.
Thanks.
vba
When there is a variable type mismatch compilation error in VBA sometimes the solution is to change how I declare the variables, but not the type.
e.g.:
If I have:
Dim numCables, numCaColumn, firstCableRow, numCable, numDiColumn, posInArray, j, numMaterialWDifSEColumn, numSEMaterials As Integer
And I replace it for:
Dim numCables, numCaColumn, firstCableRow, numCable, numDiColumn, posInArray, j, numMaterialWDifSEColumn As Integer
Dim numSEMaterials As Integer
Now with this change I don't have a compilation error and all is working fine.
I would like to know why this is happening.
Thanks.
vba
vba
edited Mar 27 at 16:10
BigBen
10.1k3 gold badges9 silver badges24 bronze badges
10.1k3 gold badges9 silver badges24 bronze badges
asked Mar 27 at 16:06
MandragorMandragor
153 bronze badges
153 bronze badges
1
This question pops up all the time, but in such a way that it is seldom an outright duplicate. Arguably VBA made a poor design decision here, but it is what it is.
– John Coleman
Mar 27 at 16:15
add a comment |
1
This question pops up all the time, but in such a way that it is seldom an outright duplicate. Arguably VBA made a poor design decision here, but it is what it is.
– John Coleman
Mar 27 at 16:15
1
1
This question pops up all the time, but in such a way that it is seldom an outright duplicate. Arguably VBA made a poor design decision here, but it is what it is.
– John Coleman
Mar 27 at 16:15
This question pops up all the time, but in such a way that it is seldom an outright duplicate. Arguably VBA made a poor design decision here, but it is what it is.
– John Coleman
Mar 27 at 16:15
add a comment |
2 Answers
2
active
oldest
votes
In variable declaration, you must specify the type for each variable, otherwise it's a Variant
.
In other words, in
Dim numCables, numCaColumn, firstCableRow, numCable, numDiColumn, posInArray, j, numMaterialWDifSEColumn, numSEMaterials As Integer
only numSEMaterials
is an Integer
and the rest are Variant
s.
You need
Dim numCables as Integer, numCaColumn as Integer, firstCableRow as Integer...
Note (thanks for the reminder @John Coleman) that it's almost always better to use a Long
variable type instead of Integer
. For a detailed explanation, see Why use integer instead of Long.
add a comment |
Vba code like Dim a, b, c as integer
does NOT declare a, b and c as integer type! It does only declare c as integer. The variables a and b are declared as variant!
To declare all variables as integer you have to decare each one individually:
Dim a as integer
Dim b as integer
Dim c as integer
.
PROOF
Option Explicit
Sub Debug_VarType()
Dim a, b, c As Integer
Debug.Print "Definition of a, b and c:"
Debug.Print "a: "; VarType(a); MyVarType(VarType(a))
Debug.Print "b: "; VarType(b); MyVarType(VarType(b))
Debug.Print "c: "; VarType(c); MyVarType(VarType(c))
Debug.Print ""
Dim d As Integer
Dim e As Integer
Dim f As Integer
Debug.Print "Definition of d, e and f:"
Debug.Print "d: "; VarType(d); MyVarType(VarType(d))
Debug.Print "e: "; VarType(e); MyVarType(VarType(e))
Debug.Print "f: "; VarType(f); MyVarType(VarType(f))
Debug.Print ""
End Sub
Function MyVarType(Varint As Integer) As String
Select Case Varint
Case 0: MyVarType = "vbEmpty"
Case 1: MyVarType = "vbNull"
Case 2: MyVarType = "vbInteger"
Case 3: MyVarType = "vbLong"
Case 4: MyVarType = "vbSingle"
Case 5: MyVarType = "vbDouble"
Case 6: MyVarType = "vbCurrency"
Case 7: MyVarType = "vbDate"
Case 8: MyVarType = "vbString"
Case 9: MyVarType = "vbObject"
Case 10: MyVarType = "vbError"
Case 11: MyVarType = "vbBoolean"
Case 12: MyVarType = "vbVariant"
Case 13: MyVarType = "vbDataObject"
Case 14: MyVarType = "vbDecimal"
Case 17: MyVarType = "vbByte"
Case 20: MyVarType = "vbLongLong"
Case 36: MyVarType = "vbUserDefinedType"
Case 8192: MyVarType = "vbArray"
End Select
End Function
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55381729%2fwhy-sometimes-the-same-variables-declaration-gives-a-compilation-error%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
In variable declaration, you must specify the type for each variable, otherwise it's a Variant
.
In other words, in
Dim numCables, numCaColumn, firstCableRow, numCable, numDiColumn, posInArray, j, numMaterialWDifSEColumn, numSEMaterials As Integer
only numSEMaterials
is an Integer
and the rest are Variant
s.
You need
Dim numCables as Integer, numCaColumn as Integer, firstCableRow as Integer...
Note (thanks for the reminder @John Coleman) that it's almost always better to use a Long
variable type instead of Integer
. For a detailed explanation, see Why use integer instead of Long.
add a comment |
In variable declaration, you must specify the type for each variable, otherwise it's a Variant
.
In other words, in
Dim numCables, numCaColumn, firstCableRow, numCable, numDiColumn, posInArray, j, numMaterialWDifSEColumn, numSEMaterials As Integer
only numSEMaterials
is an Integer
and the rest are Variant
s.
You need
Dim numCables as Integer, numCaColumn as Integer, firstCableRow as Integer...
Note (thanks for the reminder @John Coleman) that it's almost always better to use a Long
variable type instead of Integer
. For a detailed explanation, see Why use integer instead of Long.
add a comment |
In variable declaration, you must specify the type for each variable, otherwise it's a Variant
.
In other words, in
Dim numCables, numCaColumn, firstCableRow, numCable, numDiColumn, posInArray, j, numMaterialWDifSEColumn, numSEMaterials As Integer
only numSEMaterials
is an Integer
and the rest are Variant
s.
You need
Dim numCables as Integer, numCaColumn as Integer, firstCableRow as Integer...
Note (thanks for the reminder @John Coleman) that it's almost always better to use a Long
variable type instead of Integer
. For a detailed explanation, see Why use integer instead of Long.
In variable declaration, you must specify the type for each variable, otherwise it's a Variant
.
In other words, in
Dim numCables, numCaColumn, firstCableRow, numCable, numDiColumn, posInArray, j, numMaterialWDifSEColumn, numSEMaterials As Integer
only numSEMaterials
is an Integer
and the rest are Variant
s.
You need
Dim numCables as Integer, numCaColumn as Integer, firstCableRow as Integer...
Note (thanks for the reminder @John Coleman) that it's almost always better to use a Long
variable type instead of Integer
. For a detailed explanation, see Why use integer instead of Long.
edited Mar 27 at 16:20
answered Mar 27 at 16:12
BigBenBigBen
10.1k3 gold badges9 silver badges24 bronze badges
10.1k3 gold badges9 silver badges24 bronze badges
add a comment |
add a comment |
Vba code like Dim a, b, c as integer
does NOT declare a, b and c as integer type! It does only declare c as integer. The variables a and b are declared as variant!
To declare all variables as integer you have to decare each one individually:
Dim a as integer
Dim b as integer
Dim c as integer
.
PROOF
Option Explicit
Sub Debug_VarType()
Dim a, b, c As Integer
Debug.Print "Definition of a, b and c:"
Debug.Print "a: "; VarType(a); MyVarType(VarType(a))
Debug.Print "b: "; VarType(b); MyVarType(VarType(b))
Debug.Print "c: "; VarType(c); MyVarType(VarType(c))
Debug.Print ""
Dim d As Integer
Dim e As Integer
Dim f As Integer
Debug.Print "Definition of d, e and f:"
Debug.Print "d: "; VarType(d); MyVarType(VarType(d))
Debug.Print "e: "; VarType(e); MyVarType(VarType(e))
Debug.Print "f: "; VarType(f); MyVarType(VarType(f))
Debug.Print ""
End Sub
Function MyVarType(Varint As Integer) As String
Select Case Varint
Case 0: MyVarType = "vbEmpty"
Case 1: MyVarType = "vbNull"
Case 2: MyVarType = "vbInteger"
Case 3: MyVarType = "vbLong"
Case 4: MyVarType = "vbSingle"
Case 5: MyVarType = "vbDouble"
Case 6: MyVarType = "vbCurrency"
Case 7: MyVarType = "vbDate"
Case 8: MyVarType = "vbString"
Case 9: MyVarType = "vbObject"
Case 10: MyVarType = "vbError"
Case 11: MyVarType = "vbBoolean"
Case 12: MyVarType = "vbVariant"
Case 13: MyVarType = "vbDataObject"
Case 14: MyVarType = "vbDecimal"
Case 17: MyVarType = "vbByte"
Case 20: MyVarType = "vbLongLong"
Case 36: MyVarType = "vbUserDefinedType"
Case 8192: MyVarType = "vbArray"
End Select
End Function
add a comment |
Vba code like Dim a, b, c as integer
does NOT declare a, b and c as integer type! It does only declare c as integer. The variables a and b are declared as variant!
To declare all variables as integer you have to decare each one individually:
Dim a as integer
Dim b as integer
Dim c as integer
.
PROOF
Option Explicit
Sub Debug_VarType()
Dim a, b, c As Integer
Debug.Print "Definition of a, b and c:"
Debug.Print "a: "; VarType(a); MyVarType(VarType(a))
Debug.Print "b: "; VarType(b); MyVarType(VarType(b))
Debug.Print "c: "; VarType(c); MyVarType(VarType(c))
Debug.Print ""
Dim d As Integer
Dim e As Integer
Dim f As Integer
Debug.Print "Definition of d, e and f:"
Debug.Print "d: "; VarType(d); MyVarType(VarType(d))
Debug.Print "e: "; VarType(e); MyVarType(VarType(e))
Debug.Print "f: "; VarType(f); MyVarType(VarType(f))
Debug.Print ""
End Sub
Function MyVarType(Varint As Integer) As String
Select Case Varint
Case 0: MyVarType = "vbEmpty"
Case 1: MyVarType = "vbNull"
Case 2: MyVarType = "vbInteger"
Case 3: MyVarType = "vbLong"
Case 4: MyVarType = "vbSingle"
Case 5: MyVarType = "vbDouble"
Case 6: MyVarType = "vbCurrency"
Case 7: MyVarType = "vbDate"
Case 8: MyVarType = "vbString"
Case 9: MyVarType = "vbObject"
Case 10: MyVarType = "vbError"
Case 11: MyVarType = "vbBoolean"
Case 12: MyVarType = "vbVariant"
Case 13: MyVarType = "vbDataObject"
Case 14: MyVarType = "vbDecimal"
Case 17: MyVarType = "vbByte"
Case 20: MyVarType = "vbLongLong"
Case 36: MyVarType = "vbUserDefinedType"
Case 8192: MyVarType = "vbArray"
End Select
End Function
add a comment |
Vba code like Dim a, b, c as integer
does NOT declare a, b and c as integer type! It does only declare c as integer. The variables a and b are declared as variant!
To declare all variables as integer you have to decare each one individually:
Dim a as integer
Dim b as integer
Dim c as integer
.
PROOF
Option Explicit
Sub Debug_VarType()
Dim a, b, c As Integer
Debug.Print "Definition of a, b and c:"
Debug.Print "a: "; VarType(a); MyVarType(VarType(a))
Debug.Print "b: "; VarType(b); MyVarType(VarType(b))
Debug.Print "c: "; VarType(c); MyVarType(VarType(c))
Debug.Print ""
Dim d As Integer
Dim e As Integer
Dim f As Integer
Debug.Print "Definition of d, e and f:"
Debug.Print "d: "; VarType(d); MyVarType(VarType(d))
Debug.Print "e: "; VarType(e); MyVarType(VarType(e))
Debug.Print "f: "; VarType(f); MyVarType(VarType(f))
Debug.Print ""
End Sub
Function MyVarType(Varint As Integer) As String
Select Case Varint
Case 0: MyVarType = "vbEmpty"
Case 1: MyVarType = "vbNull"
Case 2: MyVarType = "vbInteger"
Case 3: MyVarType = "vbLong"
Case 4: MyVarType = "vbSingle"
Case 5: MyVarType = "vbDouble"
Case 6: MyVarType = "vbCurrency"
Case 7: MyVarType = "vbDate"
Case 8: MyVarType = "vbString"
Case 9: MyVarType = "vbObject"
Case 10: MyVarType = "vbError"
Case 11: MyVarType = "vbBoolean"
Case 12: MyVarType = "vbVariant"
Case 13: MyVarType = "vbDataObject"
Case 14: MyVarType = "vbDecimal"
Case 17: MyVarType = "vbByte"
Case 20: MyVarType = "vbLongLong"
Case 36: MyVarType = "vbUserDefinedType"
Case 8192: MyVarType = "vbArray"
End Select
End Function
Vba code like Dim a, b, c as integer
does NOT declare a, b and c as integer type! It does only declare c as integer. The variables a and b are declared as variant!
To declare all variables as integer you have to decare each one individually:
Dim a as integer
Dim b as integer
Dim c as integer
.
PROOF
Option Explicit
Sub Debug_VarType()
Dim a, b, c As Integer
Debug.Print "Definition of a, b and c:"
Debug.Print "a: "; VarType(a); MyVarType(VarType(a))
Debug.Print "b: "; VarType(b); MyVarType(VarType(b))
Debug.Print "c: "; VarType(c); MyVarType(VarType(c))
Debug.Print ""
Dim d As Integer
Dim e As Integer
Dim f As Integer
Debug.Print "Definition of d, e and f:"
Debug.Print "d: "; VarType(d); MyVarType(VarType(d))
Debug.Print "e: "; VarType(e); MyVarType(VarType(e))
Debug.Print "f: "; VarType(f); MyVarType(VarType(f))
Debug.Print ""
End Sub
Function MyVarType(Varint As Integer) As String
Select Case Varint
Case 0: MyVarType = "vbEmpty"
Case 1: MyVarType = "vbNull"
Case 2: MyVarType = "vbInteger"
Case 3: MyVarType = "vbLong"
Case 4: MyVarType = "vbSingle"
Case 5: MyVarType = "vbDouble"
Case 6: MyVarType = "vbCurrency"
Case 7: MyVarType = "vbDate"
Case 8: MyVarType = "vbString"
Case 9: MyVarType = "vbObject"
Case 10: MyVarType = "vbError"
Case 11: MyVarType = "vbBoolean"
Case 12: MyVarType = "vbVariant"
Case 13: MyVarType = "vbDataObject"
Case 14: MyVarType = "vbDecimal"
Case 17: MyVarType = "vbByte"
Case 20: MyVarType = "vbLongLong"
Case 36: MyVarType = "vbUserDefinedType"
Case 8192: MyVarType = "vbArray"
End Select
End Function
edited Mar 27 at 16:25
answered Mar 27 at 16:12
simple-solutionsimple-solution
5793 silver badges7 bronze badges
5793 silver badges7 bronze badges
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55381729%2fwhy-sometimes-the-same-variables-declaration-gives-a-compilation-error%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
This question pops up all the time, but in such a way that it is seldom an outright duplicate. Arguably VBA made a poor design decision here, but it is what it is.
– John Coleman
Mar 27 at 16:15