分配变量并在范围内使用它

时间:2013-10-24 07:30:53

标签: vba excel-vba excel

我是新来的,我刚刚开始使用VBA,如果有人能帮助我,我将非常感激。

基本上,来自单元格A1:D5是我收到的信息。这些信息会定期添加到电子表格中。 E1:F5是论坛,我已经在VBA中开发了自动填充,直到Column A结束。

问题在于,我有超过100,000行的数据。所以总是重放宏将需要大量的时间来处理。无论如何,我可以编写一个代码来填充前面的行(即E5和E6)吗?

    A   B   C   D       E       F
1   6J  6J  HND KMI formula 1   formula 2       
2   6J  6J  HND KMJ formula 1   formula 2       
3   6J  6J  HND NGS formula 1   formula 2       
4   6J  6J  KMI HND formula 1   formula 2       
5   6J  6J  KMJ HND formula 1   formula 2   
6   6J  6J  NGS HND             
7   7A  7A  FUK ISG             
8   7A  7A  FUK KMQ             
9   7A  7A  ISG FUK     

我写了一个简单的代码来测试它,但我被卡住了。我认为这个问题与x作为变量的声明有关:


Sub testloop()

Dim lastrowdatab As String
Dim lastrowpopulate As String
Dim x As String

lastrowdataoriginal = Cells(Rows.Count, "A").End(xlUp).Row
lastrowformula = Cells(Rows.Count, "E").End(xlUp).Row

x = Cells(Rows.Count, "E").End(xlUp).Offset(1, 0).Address(False, fase)
Range(":J" & lastrowdatab).Formula = "=2+3"

If lastrowdatab <> lastrowpopulate Then

    Range("x:E" & lastrowdataoriginal).Formula = "=2+3"

Else

    MsgBox "all good"

End If

End Sub

请帮帮我!非常感谢你!

1 个答案:

答案 0 :(得分:0)

这是你在尝试(经过试验和测试)吗?

Sub testloop()
    Dim ws As Worksheet
    Dim LrowA As Long, LRowE As Long, LRowF As Long
    Dim sFormula As String

    '~~> Change this to relevant sheet
    Set ws = ThisWorkbook.Sheets("Sheet1")

    With ws
        LrowA = .Range("A" & .Rows.Count).End(xlUp).Row
        LRowE = .Range("E" & .Rows.Count).End(xlUp).Row
        LRowF = .Range("F" & .Rows.Count).End(xlUp).Row

        sFormula = .Range("E" & LRowE).Formula
        .Range("E" & LRowE & ":E" & LrowA).Formula = sFormula

        sFormula = .Range("F" & LRowF).Formula
        .Range("F" & LRowF & ":F" & LrowA).Formula = sFormula
    End With
End Sub