Excel VBA:如何在For Each语句中使用更改单元格值?

时间:2014-01-26 15:50:03

标签: excel vba excel-vba

所以我构建了以下宏,它几乎捕获了它需要做的事情。它从一张sheet3复制数据,粘贴sheet1中的数据。之后,它将变量1-7替换为在sheet2中找到的值。

当宏执行一行时,宏工作正常。但是我希望它遍历Sheet2中所有填充的行,并用Sheet2中相应的行值替换变量。

希望在这里找到答案,因为我对VBA很陌生并且还没有找到答案。

Sub Macro1()

For Each c In Sheets("Sheet2").Range("A2:G13").Rows

Sheets("Sheet3").Activate
Range("A1:AH125").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Sheet1").Activate
lMaxRows = Cells(Rows.Count, "A").End(xlUp).row
Range("A" & lMaxRows + 1).Select
ActiveSheet.Paste

Selection.Replace What:="Variable1", Replacement:=Sheets("Sheet2").Range("A").Value, LookAt:=xlPart, _
    SearchOrder:=xlByRows, MatchCase:=True, SearchFormat:=False, _
    ReplaceFormat:=False
Selection.Replace What:="Variable2", Replacement:=Sheets("Sheet2").Range("B").Value, LookAt:=xlPart, _
    SearchOrder:=xlByRows, MatchCase:=True, SearchFormat:=False, _
    ReplaceFormat:=False
Selection.Replace What:="Variable3", Replacement:=Sheets("Sheet2").Range("C").Value, LookAt:=xlPart, _
    SearchOrder:=xlByRows, MatchCase:=True, SearchFormat:=False, _
    ReplaceFormat:=False
Selection.Replace What:="Variable4", Replacement:=Sheets("Sheet2").Range("D").Value, LookAt:=xlPart, _
    SearchOrder:=xlByRows, MatchCase:=True, SearchFormat:=False, _
    ReplaceFormat:=False
Selection.Replace What:="Variable5", Replacement:=Sheets("Sheet2").Range("E").Value, LookAt:= _
    xlPart, SearchOrder:=xlByRows, MatchCase:=True, SearchFormat:=False, _
    ReplaceFormat:=False
Selection.Replace What:="Variable6", Replacement:=Sheets("Sheet2").Range("F").Value, LookAt:=xlPart, _
    SearchOrder:=xlByRows, MatchCase:=True, SearchFormat:=False, _
    ReplaceFormat:=False
Selection.Replace What:="Variable7", Replacement:=Sheets("Sheet2").Range("G").Value, LookAt:=xlPart, _
    SearchOrder:=xlByRows, MatchCase:=True, SearchFormat:=False, _
    ReplaceFormat:=False

Next

End Sub

1 个答案:

答案 0 :(得分:1)

您显然已使用宏录制器创建了此宏。在尝试理解新语句的语法时,这是一个很好的起点。但是,宏录制器不知道您的目标,并将每个小步骤记录为单个语句。在尝试围绕它们循环之前,有必要整理和组合这些语句。

考虑:

Sheets("Sheet3").Activate
Range("A1:AH125").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Sheet1").Activate
lMaxRows = Cells(Rows.Count, "A").End(xlUp).row
Range("A" & lMaxRows + 1).Select
ActiveSheet.Paste

我可以用:

替换它
  lMaxRows = Cells(Rows.Count, "A").End(xlUp).Row
  Sheets("Sheet3").Range("A1:AH125").Copy _
                   Destination:=Sheets("Sheet1").Range("A" & lMaxRows + 1)

我保留了您的语句以计算lMaxRows,但已使用单个Copy语句替换了所有其他语句。该语句的语法是:

SourceRange.Copy Destination:=TopLeftCellOfDestination

Destination:=是可选的,但它有助于使语法更清晰。

我没有切换工作表或选择任何东西。这个VBA要快得多,一旦掌握了语法,就更容易理解VBA正在做什么。

现在考虑:

Selection.Replace What:="Variable1", Replacement:=Sheets("Sheet2").Range("A").Value, LookAt:=xlPart, _
    SearchOrder:=xlByRows, MatchCase:=True, SearchFormat:=False, _
    ReplaceFormat:=False

Selection必须离开,因为我没有选择任何东西。但真正的问题是:

Sheets("Sheet2").Range("A").Value

这是无效的VBA,并且包含For变量c

正确的对象是:

c.Columns("A").Value

c是一个范围,例如A2:G2A13:G13。我已采用该范围的A列给我A2A13。然后我取了那个单元格的值。

要替换Selection我已在此With块的外部放置Replace语句,以提供我认为可以满足您的需求的代码:

Sub Macro1()

For Each c In Sheets("Sheet2").Range("A2:G13").Rows

  lMaxRows = Cells(Rows.Count, "A").End(xlUp).Row
  Sheets("Sheet3").Range("A1:AH125").Copy _
                   Destination:=Sheets("Sheet1").Range("A" & lMaxRows + 1)

  With Worksheets("Sheet1")
    With .Range(.Cells(lMaxRows + 1, "A"), .Cells(lMaxRows + 126, "AH"))

     .Replace What:="Variable1", Replacement:=c.Columns("A").Value, LookAt:=xlPart, _
         SearchOrder:=xlByRows, MatchCase:=True, SearchFormat:=False, _
         ReplaceFormat:=False
     .Replace What:="Variable2", Replacement:=c.Columns("B").Value, LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=True, SearchFormat:=False, _
        ReplaceFormat:=False
     .Replace What:="Variable3", Replacement:=c.Columns("C").Value, LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=True, SearchFormat:=False, _
        ReplaceFormat:=False
     .Replace What:="Variable4", Replacement:=c.Columns("D").Value, LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=True, SearchFormat:=False, _
        ReplaceFormat:=False
     .Replace What:="Variable5", Replacement:=c.Columns("E").Value, LookAt:= _
        xlPart, SearchOrder:=xlByRows, MatchCase:=True, SearchFormat:=False, _
        ReplaceFormat:=False
     .Replace What:="Variable6", Replacement:=c.Columns("F").Value, LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=True, SearchFormat:=False, _
        ReplaceFormat:=False
     .Replace What:="Variable7", Replacement:=c.Columns("G").Value, LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=True, SearchFormat:=False, _
        ReplaceFormat:=False

    End With
  End With

Next

End Sub