Thank you in advance for any helps anyone can provide. My knowledge of VBA is very limited.
I have a list of vba codes that I have to update the cell references every month when new column was added for new month.
I am looking for a way to simplify those codes, so that instead of updating the range (column) for all of them, I only need to update a few.
Is there a way to simplify the codes below?
The range that will be change monthly (move up one column as new column were added for a new month), is the "K" and "H" as in example below. The rows stay the same.
This is referenced to several tables within a worksheets (with headers), that's why some rows were not included (for example, row 1 to 3 are headers, row 12 & 13 are empty, and row 14 & 15 are headers again, etc).
The iOffset is an integer declaration (set for cases to look at values in certain ranges)
Range("W4").Formula = "=AVERAGE(K4:OFFSET(H4,," & iOffset & "))"
Range("W5").Formula = "=AVERAGE(K5:OFFSET(H5,," & iOffset & "))"
Range("W6").Formula = "=AVERAGE(K6:OFFSET(H6,," & iOffset & "))"
Range("W7").Formula = "=AVERAGE(K7:OFFSET(H7,," & iOffset & "))"
Range("W8").Formula = "=AVERAGE(K8:OFFSET(H8,," & iOffset & "))"
Range("W9").Formula = "=AVERAGE(K9:OFFSET(H9,," & iOffset & "))"
Range("W10").Formula = "=AVERAGE(K10:OFFSET(H10,," & iOffset & "))"
Range("W11").Formula = "=AVERAGE(K11:OFFSET(H11,," & iOffset & "))"
Range("W22").Formula = "=AVERAGE(K22:OFFSET(H22,," & iOffset & "))"
Range("W23").Formula = "=AVERAGE(K23:OFFSET(H23,," & iOffset & "))"
Range("W24").Formula = "=SUM(W22:W23)"
Range("W25").Formula = "=AVERAGE(K25:OFFSET(H25,," & iOffset & "))"
Range("W26").Formula = "=AVERAGE(K26:OFFSET(H26,," & iOffset & "))"
Range("W27").Formula = "=SUM(W25:W26)"
Range("W28").Formula = "=AVERAGE(K28:OFFSET(H28,," & iOffset & "))"
Range("W29").Formula = "=AVERAGE(K29:OFFSET(H29,," & iOffset & "))"
Range("W36").Formula = "=AVERAGE(K36:OFFSET(H36,," & iOffset & "))"
Range("W37").Formula = "=AVERAGE(K37:OFFSET(H37,," & iOffset & "))"
Range("W38").Formula = "=AVERAGE(K38:OFFSET(H38,," & iOffset & "))"
Range("W51").Formula = "=SUM(K51:OFFSET(BH51,," & iOffset & "))"
Range("W52").Formula = "=SUM(K52:OFFSET(BH52,," & iOffset & "))"
Range("W53").Formula = "=SUM(K53:OFFSET(BH53,," & iOffset & "))"
Range("W54").Formula = "=SUM(K54:OFFSET(BH54,," & iOffset & "))"
Range("W61").Formula = "=SUM(K61:OFFSET(H61,," & iOffset & "))"
Range("W62").Formula = "=SUM(K62:OFFSET(H62,," & iOffset & "))"
Range("W63").Formula = "=SUM(K63:OFFSET(H63,," & iOffset & "))"
Range("W64").Formula = "=SUM(K64:OFFSET(H64,," & iOffset & "))"
I have a list of vba codes that I have to update the cell references every month when new column was added for new month.
I am looking for a way to simplify those codes, so that instead of updating the range (column) for all of them, I only need to update a few.
Is there a way to simplify the codes below?
The range that will be change monthly (move up one column as new column were added for a new month), is the "K" and "H" as in example below. The rows stay the same.
This is referenced to several tables within a worksheets (with headers), that's why some rows were not included (for example, row 1 to 3 are headers, row 12 & 13 are empty, and row 14 & 15 are headers again, etc).
The iOffset is an integer declaration (set for cases to look at values in certain ranges)
Range("W4").Formula = "=AVERAGE(K4:OFFSET(H4,," & iOffset & "))"
Range("W5").Formula = "=AVERAGE(K5:OFFSET(H5,," & iOffset & "))"
Range("W6").Formula = "=AVERAGE(K6:OFFSET(H6,," & iOffset & "))"
Range("W7").Formula = "=AVERAGE(K7:OFFSET(H7,," & iOffset & "))"
Range("W8").Formula = "=AVERAGE(K8:OFFSET(H8,," & iOffset & "))"
Range("W9").Formula = "=AVERAGE(K9:OFFSET(H9,," & iOffset & "))"
Range("W10").Formula = "=AVERAGE(K10:OFFSET(H10,," & iOffset & "))"
Range("W11").Formula = "=AVERAGE(K11:OFFSET(H11,," & iOffset & "))"
Range("W22").Formula = "=AVERAGE(K22:OFFSET(H22,," & iOffset & "))"
Range("W23").Formula = "=AVERAGE(K23:OFFSET(H23,," & iOffset & "))"
Range("W24").Formula = "=SUM(W22:W23)"
Range("W25").Formula = "=AVERAGE(K25:OFFSET(H25,," & iOffset & "))"
Range("W26").Formula = "=AVERAGE(K26:OFFSET(H26,," & iOffset & "))"
Range("W27").Formula = "=SUM(W25:W26)"
Range("W28").Formula = "=AVERAGE(K28:OFFSET(H28,," & iOffset & "))"
Range("W29").Formula = "=AVERAGE(K29:OFFSET(H29,," & iOffset & "))"
Range("W36").Formula = "=AVERAGE(K36:OFFSET(H36,," & iOffset & "))"
Range("W37").Formula = "=AVERAGE(K37:OFFSET(H37,," & iOffset & "))"
Range("W38").Formula = "=AVERAGE(K38:OFFSET(H38,," & iOffset & "))"
Range("W51").Formula = "=SUM(K51:OFFSET(BH51,," & iOffset & "))"
Range("W52").Formula = "=SUM(K52:OFFSET(BH52,," & iOffset & "))"
Range("W53").Formula = "=SUM(K53:OFFSET(BH53,," & iOffset & "))"
Range("W54").Formula = "=SUM(K54:OFFSET(BH54,," & iOffset & "))"
Range("W61").Formula = "=SUM(K61:OFFSET(H61,," & iOffset & "))"
Range("W62").Formula = "=SUM(K62:OFFSET(H62,," & iOffset & "))"
Range("W63").Formula = "=SUM(K63:OFFSET(H63,," & iOffset & "))"
Range("W64").Formula = "=SUM(K64:OFFSET(H64,," & iOffset & "))"