How do you loop columns within a range in Excel VBA

Thrillho

The title may be similar to other questions but as far as I can tell no one has asked this one. I am trying to loop through all columns and add the sum of them at the bottom after the last row and make the values before the sum grey. Up until this point I have been writing out each column name but the sheets keep getting larger and this is becoming a waste of time because I know there must be a better way. Please keep in mind I am new to VBAs. The section of the code is:

LR = Range("I" & Rows.Count).End(xlUp).Row
Range("I1:I" & LR & "").Interior.Color = RGB(217, 217, 217)
Range("I" & LR + 2).Formula = "=SUM(I2:I" & LR & ")"
LR = Range("J" & Rows.Count).End(xlUp).Row
Range("J1:J" & LR & "").Interior.Color = RGB(217, 217, 217)
Range("J" & LR + 2).Formula = "=SUM(J2:J" & LR & ")"
LR = Range("K" & Rows.Count).End(xlUp).Row
Range("K1:K" & LR & "").Interior.Color = RGB(217, 217, 217)
Range("K" & LR + 2).Formula = "=SUM(K2:K" & LR & ")"
LR = Range("L" & Rows.Count).End(xlUp).Row
Range("L1:L" & LR & "").Interior.Color = RGB(217, 217, 217)
Range("L" & LR + 2).Formula = "=SUM(L2:L" & LR & ")"

I hope you can see a solution using a loop. Thank you.

Rory

No need to loop at all here for this:

Dim LR                    As Long
Dim sLastCol              As String

' change this as needed
sLastCol = "L"

LR = Range("I" & Rows.Count).End(xlUp).Row

Range("I1", Cells(LR, sLastCol)).Interior.Color = RGB(217, 217, 217)
Range("I" & LR + 2, sLastCol & LR + 2).FormulaR1C1 = "=SUM(R2C:R[-2]C)"

or if you can determine the last column using the last populated cell in row 1, you could use:

Dim LR                    As Long
Dim lLastCol              As Long

LR = Range("I" & Rows.Count).End(xlUp).Row
lLastCol = Cells(1, Columns.Count).end(xltoleft).column

Range("I1", Cells(LR, lLastCol)).Interior.Color = RGB(217, 217, 217)
Range("I" & LR + 2, Cells(LR + 2, lLastCol)).FormulaR1C1 = "=SUM(R2C:R[-2]C)"

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Excel VBA swapping columns of a vba range without a loop

How to increment the Range of a For Each loop - Excel VBA

How do I copy some specific columns from a given range which is auto filtered in Excel VBA?

How to loop through cells in a section of columns within a range?

How do I Loop Through A Sorted Excel Range in VBA in the Order I Sorted It In?

How to Export Outlook tasks into Excel within a specified date range (VBA)

Excel: How to find a string within a range and export the value of that cell (No VBA)

How do you reference Ranges in Excel VBA?

Working with columns in a range in Excel VBA

Delete the Range of Columns in Excel VBA

Excel VBA Loop Dynamic Range

How to save the range of all hidden columns in VBA for Excel

EXCEL-VBA How to export to a CSV... a custom range of columns?

How do you split rows within the same table into columns?

How to use an excel vba loop with a database query connection within?

How do you add large range of NaN columns to a pandas dataframe

How to Loop Range number in Excel VBA without changing letter

vba-excel: how to exclude copying a range of cells with for loop and if statement

How to update the value in a range of cells using a Loop in Excel VBA

How to create a For Loop for a varying range per project using Excel VBA?

How do you accumulate values within a ForEach loop in Mule?

Haskell - how do you make a loop that has getLine within it?

Summing columns in VBA/excel in for loop

Excel VBA - filter rows within dynamic range

Excel VBA to delete blank rows within a range

how do you through loop a certain date range

How do you reverse the sign of a range of numbers in Excel?

How do you INSERT a range from Excel to an SQL table

Can you do subpattern within range for regex?