2016-12-19 135 views
0

我的目的是選擇連續的其他單元格。我的代碼(我敢肯定,我們有了一個更好的辦法,在一個時間來完成這一點,但一個步驟我猜):VBA運行時錯誤1004帶雙位數的範圍

Range("C" & row & ":C" & row + 1 & ", E" & row & ":E" & row + 1 & ", G" & row & ":G" & row + 1 & ", I" & row & ":I" & row + 1 & ", K" & row & ":K" & row + 1 & ", M" & row & ":M" & row + 1 & ", O" & row & ":O" & row + 1 & ", Q" & row & ":Q" & row + 1 & ", S" & row & ":S" & row + 1 & ", U" & row & ":U" & row + 1 & ", W" & row & ":W" & row + 1 & ", Y" & row & ":Y" & row + 1 & ", AA" & row & ":AA" & row + 1 & ", AC" & row & ":AC" & row + 1 & ", AE" & row & ":AE" & row + 1 & ", AG" & row & ":AG" & row + 1 & ", AI" & row & ":AI" & row + 1 & ", AK" & row & ":AK" & row + 1 & ", AM" & row & ":AM" & row + 1 & ", AO" & row & ":AO" & row + 1 & ", AQ" & row & ":AQ" & row + 1 & ", AS" & row & ":AS" & row + 1 & ", AU" & row & ":AU" & row + 1 & ", AW" & row & ":AW" & row + 1 & ", AY" & row & ":AY" & row + 1 & ", BA" & row & ":BA" & row + 1).Select 

我2遞增突出每隔一行也。當row超過10時,它返回一個錯誤(對象'_Global'失敗的方法'範圍')。我一直無法通過Google找到任何答案。似乎不是一個常見問題。我真的很困惑,爲什麼它只適用於單個數字的行。任何幫助表示讚賞。

+3

IIR對於文本'Range'規範有255個字符限制。 – Comintern

+0

或者把整個字符串粘貼到即時窗口中,就像這個'?string'看看它是什麼產生的 – Vityata

回答

3

這可能更容易編碼|閱讀|調試。

Dim ws as Worksheet 
Set ws = Worksheets("mySheet") 'change as needed 

Dim x as Integer 
For x = 3 to 53 Step 2 'loop from C to BA skipping 1 column in between 

    Dim rngEveryOther as Range 
    If rngEveryOther is Nothing 
     Set rngEveryOther = ws.Range(ws.Cells(i,x),ws.Cells(i+1,x)) 
    Else 
     Set rngEveryOther = Union(rngEveryOther, ws.Range(ws.Cells(i,x),ws.Cells(i+1,x))) 
    End If 

Next 

rngEveryOther.Select 

Union語句會將單元格加入現有範圍(如果代碼中不明顯)。

+0

稍作調整,它就像一個魅力!非常感謝! –

相關問題