2016-06-29 80 views
0

下面是一個代碼段,我有我的VBA程序:類型不匹配選擇用公式VBA(Excel)中列時

Columns("currentColumn:(currentColumn+1)").Select 

出於某種原因,我有一個類型不匹配,當我運行這一點。 currentColumn是一個整數。這是我第一次使用VBA,所以我確信我錯過了一些東西。我想知道是否有人能指引我走向正確的軌道。

感謝

+0

哦,我看到了。是否有類似於允許我傳入數字的列? – bugsyb

+0

'Range(Columns(currentColumn),Columns(currentColumn + 1))。Select' –

回答

0

要闡述斯科特的建議:

的問題不僅在於Columns不接受輸入,比如"2:3"Rows將與工作,但對於Columns你需要"B:C")。問題還在於,"currentColumn:(currentColumn+1)"是由這些字符組成的字符串。變量currentColumn"未被替換,並且+1也未被執行。如果你想建立一個字符串數量currentColumn而不是字符串"currentColumn你需要使用&

currentColumn & ":" & currentColumn + 1 'if currentColumn = 2 then this is the same as "2:3" 

注意,你通常不需要Select對象。 Here是如何避免它的一些例子。