如何成功运行循环进行计算?

时间:2019-05-23 08:12:41

标签: excel vba

我必须在100.00行上进行计算,我必须进行循环并声明变量。但是,仅将第一行填满,就不可能将公式推到最后。

'variable declaration
 Dim ls As Worksheet
 Set ls = ThisWorkbook.Worksheets("BASE2")
 Dim Lrow As Long
 Lrow = ls.Cells(ls.Rows.Count, "A").End(xlUp).Row

 'calculation
 ls.Range("I2:I" & Lrow).FormulaR1C1 = "=TRIM(LEFT(RC[-6],10))"
 Range("I1").Select
 ActiveCell.FormulaR1C1 = "Article sans désignation"
 Columns("I:I").ColumnWidth = 20.71

我想将此公式下调至95,000行的底部(这在一次到下一次之间是可变的,这就是我循环的原因)

1 个答案:

答案 0 :(得分:0)

您的代码正确无误(见下文)。我只是认为您可能没有正确的列来选择最后一行。因此,这是您的代码,其中包含一些调试信息。也许这将帮助您使事情正常进行。

'variable declaration
 Dim ls As Worksheet
 Set ls = ThisWorkbook.Worksheets("BASE2")
 Dim Lrow As Long
 Lrow = ls.Cells(ls.Rows.Count, "A").End(xlUp).Row

 MsgBox "Column A is filled up to row " & Lrow & " with data. Therefore," & Chr(10) _
    & "the following range will be used: " & ls.Range("I2:I" & Lrow).Address

 'calculation
 ls.Range("I2:I" & Lrow).FormulaR1C1 = "=TRIM(LEFT(RC[-6],10))"
 Range("I1").Value2 = "Article sans désignation"
 Columns("I:I").ColumnWidth = 20.71

enter image description here