简体   繁体   English

VBA 循环直到最后一列并将列中的值增加 1

[英]VBA loop until the last column and increase value in column by 1

I am working on a project where I need to populate the column headings with incremental values (increased by 1) until the Last Column.我正在做一个项目,我需要用增量值(增加 1)填充列标题,直到最后一列。

The code is working OK but the value in the column headings is NOT increased by 1. It is just taking the original value and place it over all columns.代码工作正常,但列标题中的值没有增加 1。它只是采用原始值并将其放在所有列上。

Could you help me?你可以帮帮我吗?

My code so far:到目前为止我的代码:

Sub LastColumn_PopulateHeadings()

'Declare variable for Last row (Prior FY)
Dim LastColumn As Integer
Dim i As Integer


'Find the last Column used

LastColumn = Range("XFD4").End(xlToLeft).Column

'populate headings with column values UNTIL LAST COLUMN

' Loop to populate the heading until LAST column

i = 8

Do While i < LastColumn
'MsgBox (LastColumn)
    
    Cells(4, i).Value = Cells(4, i).Value + 1
    i = i + 1
Loop

End Sub

I find your code a little strange, but probably i am missing something.我发现你的代码有点奇怪,但可能我遗漏了一些东西。 Anyway this one should work:无论如何,这个应该工作:

Sub LastColumn_PopulateHeadings()

'Declare variable for Last row (Prior FY)
Dim LastColumn As Integer
Dim i As Integer
Dim IntCounter01 As Integer '<<<<<<ADDED LINE (1 of 3)


'Find the last Column used

LastColumn = Range("XFD4").End(xlToLeft).Column

'populate headings with column values UNTIL LAST COLUMN

' Loop to populate the heading until LAST column

i = 8
IntCounter01 = 1 '<<<<<<ADDED LINE (2 of 3)

Do While i < LastColumn
'MsgBox (LastColumn)
    
    Cells(4, i).Value = IntCounter01
    i = i + 1
     IntCounter01 = IntCounter01 + 1 '<<<<<<ADDED LINE (3 of 3)
Loop

End Sub

I took your code and added 3 lines.我拿了你的代码并添加了 3 行。 You could also use a For-Next cycle instead of using a Do-While-Loop cycle since you already know your maximal value.您也可以使用 For-Next 循环而不是使用 Do-While-Loop 循环,因为您已经知道您的最大值。 Something like:就像是:

For i = i To LastColumn - 1

    Cells(4, i).Value = IntCounter01
    IntCounter01 = IntCounter01 + 1
    
Next

You could also use a formula to cover your range instead of picking each cell one by one.您还可以使用公式来覆盖您的范围,而不是一个一个地选择每个单元格。 Like this:像这样:

Sub LastColumn_PopulateHeadings()
    
    'Declarations.
    Dim IntFirstColumn As Integer
    Dim IntLastColumn As Integer
    Dim IntRow As Integer
    Dim IntFirstValue
    Dim RngRange01 As Range
    
    'Setting variables.
    IntFirstValue = 1
    IntRow = 4
    IntFirstColumn = 8
    IntLastColumn = Range("XFD4").End(xlToLeft).Column
    
    'Setting first value in the first cell.
    Cells(IntRow, IntFirstColumn).Value = IntFirstValue
    
    'Setting RngRange01.
    Set RngRange01 = Range(Cells(IntRow, IntFirstColumn + 1), Cells(IntRow, IntLastColumn - 1))
    
    'Setting formulas in RngRange01.
    RngRange01.FormulaR1C1 = "=RC[-1]+1"
    
    'Copy-pasting the values in RngRange01.
    RngRange01.Value = RngRange01.Value
    
End Sub

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

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