VBA循环范围,按列优先

VBA循环范围,按列优先,vba,excel,Vba,Excel,他们是否有办法在可以首先循环列的范围内循环(对于X中的每个单元格,最好是a)?e、 g.A1、A2、A3、A4而非A1、B1、C1、D1 我试着像这样变换范围,但可惜这不起作用 For Each cell In application.transpose(.Range("C3:G100")) 我有一种感觉,我将不得不使用 for c = 0 to .Range("C3:G100").columns.count for r =0 to .range("C3:G100").rows.cou

他们是否有办法在可以首先循环列的范围内循环(对于X中的每个单元格,最好是a)?e、 g.A1、A2、A3、A4而非A1、B1、C1、D1

我试着像这样变换范围,但可惜这不起作用

For Each cell In application.transpose(.Range("C3:G100"))
我有一种感觉,我将不得不使用

for c = 0 to .Range("C3:G100").columns.count
    for r =0 to .range("C3:G100").rows.count
把它放在一个数组中

Dim i As Long, j As Long
Dim avArray As Variant

avArray = Range("C3:G100").Value

For j = LBound(avArray, 2) To UBound(avArray, 2)
    For i = LBound(avArray, 1) To UBound(avArray, 1)
        avArray(i, j) = CStr(avArray(i, j)) 'do something here, this 
                                            'example just makes the 
                                            'value a string
    Next
Next
如果要进行更改,则在完成更改后将数组转储回工作表

Range("C3:G100").Value = avArray

试试这个。下面的宏将:
活动工作表中的(*):
(1) 识别所有非空单元格的范围,
(2) 逐列循环遍历该范围,
(3) 逐单元格循环遍历每列单元格

Sub LoopThruUsedRangeColByCol()
    Dim rngCol, rngAll, cell As Range, cnt As Long
    Set rngAll = Range("A1").CurrentRegion
    'MsgBox R.Address(0, 0), , "All data"
    cnt = 0
    For Each rngCol In rngAll.Columns
        rngCol.Select
        For Each cell In Selection
            cnt = cnt + 1
            Debug.Print (cell.Address)
            Debug.Print (cell.row)
            Debug.Print (cell.Column)
            If cnt > 3 Then End
        Next cell
    Next rngCol
End Sub
笔记: (1)
cnt
仅添加以使输出不被淹没。如果cnt>3,则删除
,然后在演示后结束

(2) 您需要打开“即时窗口”以查看debug.print输出
要执行此操作:菜单栏=>查看菜单=>即时窗口

For Each cell In application.transpose(.Range("C3:G100"))
     
  'Your Code here
     
Next cell


选择一个答案,说明是否可以关闭。保持清洁,不要夸大未回答的问题,因为它尚未得到回答。我知道如何做下面提到的循环,但我不是在问这个问题。我在问你是否可以在一个类似的maner中为每个单元做一个测试,例如,首先循环列。
For Each cell In application.transpose(.Range("C3:G100"))
     
  'Your Code here
     
Next cell
Dim col As Range
For Each col In Range("C3:G100").Columns
   For Each cell In col.Cells
     
     'Your Code here
   
   Next cell
Next col