Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
992 views
in Technique[技术] by (71.8m points)

excel - VBA variable declaration okay on two lines but not when comma separated. Compiler bug?

I have the following variable declaration in a procedure which works fine.

Dim table_rng As Range
Dim attachments_rng As Range

I did have it as:

Dim table_rng, attachments_rng As Range

but this caused a "Compile error: ByRef argument type mismatch".

Since I have working code I'm not in a crisis but I wasted an hour finding this solution.

I'm using Excel 2010 with Visual Basic for Applications 7.0.1628

As far as I know the second declaration is syntactically correct. Am I missing something? I searched in vain on the web and stackoverflow.com for any wisdom on the topic.

Thanks in advance.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

So, please take a look at VBA editor on this. First statement seems like to declare 3 integers, but not. Variable i and j are variant and only k is integer.

So if you look on your code, table_rng is declared as variant/empty and maybe this is what causing issue, but without rest of your code i cant tell you more.

But i recommend you to use single variable declaration on line, its far more easier to read. Or if you wana to declare more variable on single line, you need to specifi date type for each of them (like last line on my example. If you wana to know variable types, use debugging in VBA and View/Locals window).

Sub testVarDeclar()

    Dim i, j, k As Integer

    Dim a As Integer, b As Integer

    Dim table_rng, attachments_rng As Range

    Dim table_rng2 As Range, attachments_rng2 As Range
End Sub

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...