简体   繁体   English

VBA FOR循环增加列

[英]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). 等等......直到Kth专栏(因为我是VBA新手)。 Can someone help me to find the way to put them in for loop or any other suitable loop? 有人可以帮助我找到将它们放入for循环或任何其他合适循环的方法吗?

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: 如果我理解正确的,并且要使用for VBA中,每次引用不同的细胞,尝试这个例子:

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. 它以10x10填充单元格,值为1。

with Cells you can easily insert integer variables to select a cell. 使用Cells您可以轻松插入整数变量以选择单元格。

you can also combine it with Range like this example: 您也可以将它与Range组合,如下例所示:

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. 它只会从E到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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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