简体   繁体   中英

VBA FOR loop to increment columns

'Calculating Sum of Max. values of Eth column from pivot table values
    Dim lastRowE As Long
    lastRowE = ActiveSheet.Cells(Rows.Count, 4).End(xlUp).Row
    'Excluding grandtotal
    lastRowE = lastRowE - 1
    Worksheets("pmrrcconnmax").Activate
    Cells(1, 5).Value = WorksheetFunction.SumIfs(Range("E7:E" & lastRowE), _
                                        Range("E7:E" & lastRowE), ">0")

 'Calculating Sum of Max. values of Fth column from pivot table values
    'Dim lastRowF As Long
    lastRowF = ActiveSheet.Cells(Rows.Count, 4).End(xlUp).Row
    'Excluding grandtotal
    lastRowF = lastRowF - 1
    'Worksheets("pmrrcconnmax").Activate
    Cells(1, 6).Value = WorksheetFunction.SumIfs(Range("F7:F" & lastRowF), _
                                            Range("F7:F" & lastRowF), ">0")

'Calculating Sum of Max. values of Gth column from pivot table values
    'Dim lastRowG As Long
    lastRowG = ActiveSheet.Cells(Rows.Count, 4).End(xlUp).Row
    'Excluding grandtotal
    lastRowG = lastRowG - 1
    'Worksheets("pmrrcconnmax").Activate
    Cells(1, 7).Value = WorksheetFunction.SumIfs(Range("G7:G" & lastRowG), _
                                         Range("G7:G" & lastRowG), ">0")

and so on.... upto Kth column (since I am new to VBA). Can someone help me to find the way to put them in for loop or any other suitable loop?

Thank you in advance

if i understood correctly, and you want to use for in VBA and refer to a different cell each time, try this example:

Dim row As Integer
Dim col As Integer

For row = 1 To 10
    For col = 1 To 10
        Cells(row, col).Value = 1
    Next
Next

it fills the cells in 10x10 with value of 1.

with Cells you can easily insert integer variables to select a cell.

you can also combine it with Range like this example:

Range(Cells(1, 1), Cells(5, 5)).Select

you cal also select a whole column like this:

Columns(5).Select

Try this. It will only loop through from E to K.

Option Explicit

Sub Add_Formulas()

'Declaring the variable lColumn as long to store the last Column number
Dim lColumn As Long
'Declaring the variable iCntr as long to use in the For loop
Dim iCntr As Long, iCell As Long
Dim ColLetter As String
Dim lastCol As Long, lastRow As Long
Dim wks As Worksheet

' Set wks so it is the activesheet
Set wks = ThisWorkbook.ActiveSheet

'lastCol = wks.Cells(1, wks.Columns.Count).End(xlToLeft).Column
' Using column D as you are using column no. 4 in your code
lastRow = wks.Range("D" & wks.Rows.Count).End(xlUp).Row

'Excluding grandtotal
lastRow = lastRow - 1

'Assigning the last Column value to the variable lColumn
'lColumn = lastCol
lColumn = 11

'Using for loop
' 5-11 = E-K
For iCntr = lColumn To 5 Step -1

    ColLetter = GetColumnLetter(iCntr)
    Cells(1, iCntr).Value = WorksheetFunction.SumIfs(Range(ColLetter & "7:" & ColLetter & lastRow), _
                                         Range(ColLetter & "7:" & ColLetter & lastRow), ">0")
Next

End Sub

Function GetColumnLetter(colNum As Long) As String
Dim vArr
vArr = Split(Cells(1, colNum).Address(True, False), "$")
GetColumnLetter = vArr(0)
End Function

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