簡體   English   中英

如何使用Excel VBA宏循環行?

[英]How to loop rows with Excel VBA macro?

我是VBA的新手,但對PHP非常好。 話雖這么說,我正在努力與VBA循環......

我有40行稱為“SH1”的這張表:

SH1

     A     B     C     D     E
 1   2   One    1.0a   12
 2   7   Two    2.0b   34
 3  13   Three  3.0c   56
 4  14   Four   4.0d   78
..
40

我需要遍歷40行並檢查A列中的值。如果A列中的值符合我的條件(見下文),則生成一些輸出並將其放入另一張表中。

我的輸出表是3列,稱為“SH2”:

SH2

     A     B     C     D     E
 1  1.0a   12    One
    2.0b   34    Two
 2  3.0c   56    Three
    4.0d   78    Four
..
15

我決定什么去哪里的標准:

// First loop:
if a1 < 8, put c1 in SH2 a1, put d1 in SH2 b1, put b1 in SH2 c1
if a2 < 8, put c2 in SH2 a1, put d2 in SH2 b1, put b2 in SH2 c1
// ... loop through a40 ...

然后:

// Second loop:
if a1 > 8 AND a1 < 16, put c1 in SH2 a2, put d1 in SH2 b2, put b1 in SH2 c2
if a2 > 8 AND a2 < 16, put c2 in SH2 a2, put d2 in SH2 b2, put b2 in SH2 c2
// ... loop through a40 ...

進展編輯:

似乎工作,但想知道是否有“更清潔”的方式?

Sub CatchersPick2()
    Dim curCell As Range

    For Each curCell In Sheet4.Range("C3:C40").Cells
        If curCell.Value > 0 And curCell.Value < 73 Then
            cLeft = cLeft _
                & curCell.Offset(0, 5) & "." _
                & curCell.Offset(0, 6) & vbLf
            cMidl = cMidl _
                & curCell.Offset(0, -2) & ", " _
                & curCell.Offset(0, -1) & " " _
                & curCell.Offset(0, 7) & vbLf
            cRght = cRght _
                & curCell.Offset(0, 9) & " " _
                & curCell.Offset(0, 2) & " " _
                & curCell.Offset(0, 11) & " " _
                & curCell.Offset(0, 10) & vbLf
        End If
    Next curCell

    Sheet6.Range("B3") = cLeft
    Sheet6.Range("C3") = cMidl
    Sheet6.Range("D3") = cRght
    Sheet6.Range("B3:D3").Rows.AutoFit
    Sheet6.Range("B3:D3").Columns.AutoFit

End Sub
Dim cell As Range
For Each cell In Range("a1:a40")
    'do stuff here
Next cell

您可以使用cell.Row獲取當前行。 祝你好運^ _ ^

怎么樣:

Sub Catchers()
    Dim cell As Range

    Sheet1.Select 'SHEET: C

    For Each cell In Range("C3:C40")
        If cell.Value < 35 And cell.Value > 0 Then
            With Sheet6
                .Range("B" & cell.Row) = cell.Offset(0, 5) _
                    & "." & cell.Offset(0, 6)

                .Range("C" & cell.Row) = cell.Offset(0, -2) _
                    & ", " & cell.Offset(0, -1) _
                    & " " & cell.Offset(0, 7)

                .Range("D" & cell.Row) = cell.Offset(0, 9) _
                    & " " & cell.Offset(0, 2) _
                    & " " & cell.Offset(0, 11) _
                    & " " & cell.Offset(0, 10)
            End With
        End If
    Next cell

    Sheet6.Range("B4:D4").Rows.AutoFit
    Sheet6.Range("B4:D4").Columns.AutoFit

End Sub

你可以做的不是很多,但......

首先,不要使用“單元格”這個詞作為變量,它可能會起作用,但它正在玩火,所以

Dim curCell as Range

其次,您應該遍歷Range的Cells屬性

For Each curCell In Range("C3:C40").Cells

第三,你不需要選擇單元格,你可以只操縱curCell變量

最后,您不需要使用ActiveCell,只需使用curCell變量即可。

If curCell.Value < 35 And curCell.Value > 0 Then

    cLefta = curCell.Offset(0, 5) & "."

事實上,您也可以使用像'c'這樣的短變量,並將整個事物放在一行上:

cLeft = c.Offset(0,5) & "." & c.Offset(0,6) & vblf

注意:如果您的設置每次都接近相同,那么使用工作表函數可能會更容易。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM