简体   繁体   中英

VBA Loop in Excel 2016 for columns

I used the macro recorder to record some steps which works fine, however I wanted to make a loop rather than repeat each step as I want to replicate it further down the line.

The idea is I have a list of people who have who have multiple codes, they could have 1 to 30 separate codes, which gives an extra line for each of them.

The VBA basically adds 30 columns, then says in the first new column 1 is the person 1 row down the same as this line? if so add their code, else leave blank. In column 2, is the person in 2 rows down the same as this line, if so add their code, else leave blank, and etc for the rest of the columns

在此处输入图片说明

    '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],""""),"""")"

My numbers may be off a bit, but I believe may want something like this:

For i = 1 To 30
    j = i - 12
    k = i - 5
    l = i - 3
    ActiveCell.FormulaR1C1 = _
        "=IF(R[" & i & "]C[" & k & "]=""Diagnostic - HCP"",IF(RC[" & j & "]=R[" & i & "]C[" & j & "],R[" & i & "]C[" & l & "],""""),"""")"
    Selection.Offset(0, 1).Select
Next i

Thanks MJH, based on your formula this was the working code:

MemberNo = -10
Diagnosis = -3
ServiceClass = -1
For i = 1 To 30

    ActiveCell.FormulaR1C1 = _
        "=IF(R[" & i & "]C[" & Diagnosis & "]=""Diagnostic - HCP"",IF(R[0]C[" & MemberNo & "]=R[" & i & "]C[" & MemberNo & "],R[" & i & "]C[" & ServiceClass & "],""""),"""")"
    Selection.Offset(0, 1).Select
    MemberNo = MemberNo - 1
    Diagnosis = Diagnosis - 1
    ServiceClass = ServiceClass - 1

Next i </code>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM