2012-12-21 55 views
10

時,我們要做的行的循環,我們可以使用如下代碼:Excel的VBA環路上列

i = 1 
Do 
    Range("E" & i & ":D" & i).Select 
    i = i + 1 
Loop Until i > 10 

但是如果我們想要做的一列一個循環?

我們可以使用與上面相同的方法嗎?

而Excel中的列是一個複雜的如A,B,C,...,Y,Z,AA,AB,AC,等等。 問題將出現在從「Z 「到」AA「。

如何我們就從「A」到「Z」字母循環列,然後一直持續到「AA」,「AB」等

有什麼可以幫助?

回答

21

是的,讓我們使用Select作爲一個例子

示例代碼:Columns("A").select

如何通過列循環:

方法1:(您可以使用索引來代替Excel的地址)

For i = 1 to 100 
    Columns(i).Select 
next i 

方法2 :(使用地址)

For i = 1 To 100 
Columns(Columns(i).Address).Select 
Next i 

編輯: 地帶的柱爲OP

columnString = Replace(Split(Columns(27).Address, ":")(0), "$", "") 

例如你想獲得27列 - > AA,你可以得到它這樣

+0

+ 1給了這兩種方法... :)雖然第二屆一個變得無用時u有方法1. –

+0

你知道我們如何能與環路串拿到列索引? like「A」,「B」,...,「AA」,「AB」,...等。 所以我可以將它應用到我的循環中, &「:」&colIndex&I).Select' _「colIndex」是一個變量循環中excel_ – wahyueka31

+1

列索引@Rebel看到編輯 – Larry

1

如果你想堅持的同種循環的,那麼這將工作:

Option Explicit 

Sub selectColumns() 

Dim topSelection As Integer 
Dim endSelection As Integer 
topSelection = 2 
endSelection = 10 

Dim columnSelected As Integer 
columnSelected = 1 
Do 
    With Excel.ThisWorkbook.ActiveSheet 
     .Range(.Cells(columnSelected, columnSelected), .Cells(endSelection, columnSelected)).Select 
    End With 
    columnSelected = columnSelected + 1 
Loop Until columnSelected > 10 

End Sub 

編輯

如果在現實中你只是想遍歷在電子表格中的一個區域的每一個細胞,然後使用這樣的事情:

Sub loopThroughCells() 

'============= 
'this is the starting point 
Dim rwMin As Integer 
Dim colMin As Integer 
rwMin = 2 
colMin = 2 
'============= 

'============= 
'this is the ending point 
Dim rwMax As Integer 
Dim colMax As Integer 
rwMax = 10 
colMax = 5 
'============= 

'============= 
'iterator 
Dim rwIndex As Integer 
Dim colIndex As Integer 
'============= 

For rwIndex = rwMin To rwMax 
     For colIndex = colMin To colMax 
      Cells(rwIndex, colIndex).Select 
     Next colIndex 
Next rwIndex 

End Sub 
+0

+ 1 OP現在有很多選項:) –

+0

你知道我們如何才能得到列索引與循環作爲字符串? like「A」,「B」,...,「AA」,「AB」,...等。 所以我可以將它應用到我的循環中, &「:」&colIndex&i).Select' _「colIndex」是一個變量,用於循環excel_ – wahyueka31

+0

@Rebel中的列索引,它與'.Range(.Cells(i,colIndex),。 Cells(i,colIndex))。選擇'....這就是我在我的建議答案中提供的更多簡化名稱。但在你的評論中你提到了'Range(colIndex&i&「:」&colIndex&i).Select' ....不需要冒號後的_repeated_節:'Range(colIndex&i).Select' ..實際上它就等價於'.cells(i,colIndex)' – whytheq

7

另一種方法來嘗試Ø UT。 也可以在將初始列設置爲Range對象時替換select。性能明智,它有幫助。

Dim rng as Range 

Set rng = WorkSheets(1).Range("A1") '-- you may change the sheet name according to yours. 

'-- here is your loop 
i = 1 
Do 
    '-- do something: e.g. show the address of the column that you are currently in 
    Msgbox rng.offset(0,i).Address 
    i = i + 1 
Loop Until i > 10 

**兩種方法使用列數來獲得列名**

  • 斯普利特()

代碼

colName = Split(Range.Offset(0,i).Address, "$")(1) 
  • 字符串操作:

代碼

Function myColName(colNum as Long) as String 
    myColName = Left(Range(0, colNum).Address(False, False), _ 
    1 - (colNum > 10)) 
End Function 
+2

+ 1另一種方法 –

+0

你知道我們如何才能得到列索引與循環作爲字符串? like「A」,「B」,...,「AA」,「AB」,...等。 所以我可以將它應用到我的循環中, &「:」&colIndex&i).Select' _「colIndex」是一個變量來循環excel_中的列索引 – wahyueka31

+0

@Rebel這是可能的。但出於好奇心,因爲有許多有效的方法來遍歷列,爲什麼你想要獲得字符串'列名'? – bonCodigo