我使用宏記錄器來記錄一些工作正常的步驟,但是我想做一個循環而不是重複每一步,因爲我想將它複製到下一行。Excel 2016中的VBA循環列
這個想法是我有一個誰擁有多個代碼的人的名單,他們可以有1到30個單獨的代碼,這給每個人一個額外的行。
VBA基本上增加了30列,然後在第一個新列中說1是這個行下的人1行?如果是這樣,添加他們的代碼,否則留空。 在第2列,在2行上下一致,因爲這行的人,如果是添加自己的代碼,否則留空,和等在列
'Loop through the columns
For i = 1 To 30
j = -10
k = -3
l = -1
ActiveCell.FormulaR1C1 = _
"=IF(RC[k]=""Diagnostic - HCP"",IF(RC[j]=RC[i],RC[l],""""),"""")"
Selection.Offset(0, 1).Select
'The line above causes an error
Next i
'These lines all work:
ActiveCell.FormulaR1C1 = _
' "=IF(R[1]C[-4]=""Diagnostic - HCP"",IF(RC[-11]=R[1]C[-11],R[1]C[-2],""""),"""")"
'Range("Q2").Select
'ActiveCell.FormulaR1C1 = _
' "=IF(R[2]C[-5]=""Diagnostic - HCP"",IF(RC[-12]=R[2]C[-12],R[2]C[-3],""""),"""")"
'Range("R2").Select
'ActiveCell.FormulaR1C1 = _
' "=IF(R[3]C[-6]=""Diagnostic - HCP"",IF(RC[-13]=R[3]C[-13],R[3]C[-4],""""),"""")"
'Range("S2").Select
'ActiveCell.FormulaR1C1 = _
' "=IF(R[4]C[-7]=""Diagnostic - HCP"",IF(RC[-14]=R[4]C[-14],R[4]C[-5],""""),"""")"
'Range("T2").Select
'ActiveCell.FormulaR1C1 = _
' "=IF(R[5]C[-8]=""Diagnostic - HCP"",IF(RC[-15]=R[5]C[-15],R[5]C[-6],""""),"""")"
'Range("U2").Select
'ActiveCell.FormulaR1C1 = _
' "=IF(R[6]C[-9]=""Diagnostic - HCP"",IF(RC[-16]=R[6]C[-16],R[6]C[-7],""""),"""")"
'Range("V2").Select
'ActiveCell.FormulaR1C1 = _
' "=IF(R[7]C[-10]=""Diagnostic - HCP"",IF(RC[-17]=R[7]C[-17],R[7]C[-8],""""),"""")"
'Range("W2").Select
'ActiveCell.FormulaR1C1 = _
' "=IF(R[8]C[-11]=""Diagnostic - HCP"",IF(RC[-18]=R[8]C[-18],R[8]C[-9],""""),"""")"
'Range("X2").Select
'ActiveCell.FormulaR1C1 = _
' "=IF(R[9]C[-12]=""Diagnostic - HCP"",IF(RC[-19]=R[9]C[-19],R[9]C[-10],""""),"""")"
'Range("Y2").Select
'ActiveCell.FormulaR1C1 = _
' "=IF(R[10]C[-13]=""Diagnostic - HCP"",IF(RC[-20]=R[10]C[-20],R[10]C[-11],""""),"""")"
'Range("Z2").Select
'ActiveCell.FormulaR1C1 = _
' "=IF(R[11]C[-14]=""Diagnostic - HCP"",IF(RC[-21]=R[11]C[-21],R[11]C[-12],""""),"""")"
'Range("AA2").Select
'ActiveCell.FormulaR1C1 = _
' "=IF(R[12]C[-15]=""Diagnostic - HCP"",IF(RC[-22]=R[12]C[-22],R[12]C[-13],""""),"""")"
'Range("AB2").Select
'ActiveCell.FormulaR1C1 = _
' "=IF(R[13]C[-16]=""Diagnostic - HCP"",IF(RC[-23]=R[13]C[-23],R[13]C[-14],""""),"""")"
'Range("AC2").Select
'ActiveCell.FormulaR1C1 = _
' "=IF(R[14]C[-17]=""Diagnostic - HCP"",IF(RC[-24]=R[14]C[-24],R[14]C[-15],""""),"""")"
'Range("AD2").Select
'ActiveCell.FormulaR1C1 = _
' "=IF(R[15]C[-18]=""Diagnostic - HCP"",IF(RC[-25]=R[15]C[-25],R[15]C[-16],""""),"""")"
'Range("AE2").Select
'ActiveCell.FormulaR1C1 = _
' "=IF(R[16]C[-19]=""Diagnostic - HCP"",IF(RC[-26]=R[16]C[-26],R[16]C[-17],""""),"""")"
'Range("AF2").Select
'ActiveCell.FormulaR1C1 = _
' "=IF(R[17]C[-20]=""Diagnostic - HCP"",IF(RC[-27]=R[17]C[-27],R[17]C[-18],""""),"""")"
'Range("AG2").Select
'ActiveCell.FormulaR1C1 = _
' "=IF(R[18]C[-21]=""Diagnostic - HCP"",IF(RC[-28]=R[18]C[-28],R[18]C[-19],""""),"""")"
'Range("AH2").Select
'ActiveCell.FormulaR1C1 = _
' "=IF(R[19]C[-22]=""Diagnostic - HCP"",IF(RC[-29]=R[19]C[-29],R[19]C[-20],""""),"""")"
'Range("AI2").Select
'ActiveCell.FormulaR1C1 = _
' "=IF(R[20]C[-23]=""Diagnostic - HCP"",IF(RC[-30]=R[20]C[-30],R[20]C[-21],""""),"""")"
'Range("AJ2").Select
'ActiveCell.FormulaR1C1 = _
' "=IF(R[21]C[-24]=""Diagnostic - HCP"",IF(RC[-31]=R[21]C[-31],R[21]C[-22],""""),"""")"
'Range("AK2").Select
'ActiveCell.FormulaR1C1 = _
' "=IF(R[22]C[-25]=""Diagnostic - HCP"",IF(RC[-32]=R[22]C[-32],R[22]C[-23],""""),"""")"
'Range("AL2").Select
'ActiveCell.FormulaR1C1 = _
' "=IF(R[23]C[-26]=""Diagnostic - HCP"",IF(RC[-33]=R[23]C[-33],R[23]C[-24],""""),"""")"
'Range("AM2").Select
'ActiveCell.FormulaR1C1 = _
' "=IF(R[24]C[-27]=""Diagnostic - HCP"",IF(RC[-34]=R[24]C[-34],R[24]C[-25],""""),"""")"
'Range("AN2").Select
'ActiveCell.FormulaR1C1 = _
' "=IF(R[25]C[-28]=""Diagnostic - HCP"",IF(RC[-35]=R[25]C[-35],R[25]C[-26],""""),"""")"
'Range("AO2").Select
'ActiveCell.FormulaR1C1 = _
' "=IF(R[26]C[-29]=""Diagnostic - HCP"",IF(RC[-36]=R[26]C[-36],R[26]C[-27],""""),"""")"
'Range("AP2").Select
'ActiveCell.FormulaR1C1 = _
' "=IF(R[27]C[-30]=""Diagnostic - HCP"",IF(RC[-37]=R[27]C[-37],R[27]C[-28],""""),"""")"
'Range("AQ2").Select
'ActiveCell.FormulaR1C1 = _
' "=IF(R[28]C[-31]=""Diagnostic - HCP"",IF(RC[-38]=R[28]C[-38],R[28]C[-29],""""),"""")"
'Range("AR2").Select
'ActiveCell.FormulaR1C1 = _
' "=IF(R[29]C[-32]=""Diagnostic - HCP"",IF(RC[-39]=R[29]C[-39],R[29]C[-30],""""),"""")"
謝謝MJH,根據你的公式我稍微編輯它,並添加正確的字段名稱,以獲得下面的答案 – Chris