我是VBA新手,但對PHP很不錯。話雖這麼說,我用VBA循環掙扎......如何使用Excel 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中的值符合我的標準(請參見下文),則生成一些輸出並將其放入另一張表中。
我的輸出表是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
抱歉,但是這看起來相當混亂,似乎不匹配前面的東西。順便說一句,你說`如果a2> 8 AND a1 <16`你的意思是如果a2> 8 AND a2 <16`(第二個循環,第二行)。你想做更早的東西(第一,第二個Lopp)嗎? – Fionnuala 2011-02-10 16:04:17
我以前的東西只是一個構建在我需要生成的裸露輸出上的示例。我上面的例子工作,但是VBA循環和變量是新手,我確信有一個更清晰的方法(幫助!)。回答你的問題,「是的」,這是一個錯字。 – Jeff 2011-02-10 16:06:58