So I've build the following Macro that almost captures whats it needs to do. It copies data form one sheet3, pastes the data in sheet1. After which it replaces variables1-7 with values that are to be found in sheet2.
The Macro works just fine when it is Executed for one row. However I would like it to loop through all the filled in rows in Sheet2 and replace the variables with the corresponding row values in Sheet2.
Hoping to find the answer here, since I'm quite new to VBA and haven't been able to find the answer yet.
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
You have obviously created this macro with the Macro recorder. That is a good place to start when trying to understand the syntax of a new statement. However, the Macro recorder does not know your objective and records each little step as a single statement. It is necessary to tidy up and combine these statements before trying to put a loop round them.
Consider:
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
I can replace that with:
lMaxRows = Cells(Rows.Count, "A").End(xlUp).Row
Sheets("Sheet3").Range("A1:AH125").Copy _
Destination:=Sheets("Sheet1").Range("A" & lMaxRows + 1)
I have retained your statement to calculate lMaxRows
but have replaced all the other statements with a single Copy
statement. The syntax of this statement is:
SourceRange.Copy Destination:=TopLeftCellOfDestination
The Destination:=
is optional but it helps make the syntax clearer.
I have not switched worksheets or selected anything. This VBA is much faster and, once you have mastered the syntax, it is easier to understand what the VBA is doing.
Now consider:
Selection.Replace What:="Variable1", Replacement:=Sheets("Sheet2").Range("A").Value, LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=True, SearchFormat:=False, _
ReplaceFormat:=False
The Selection
will have to go because I have not selected anything. But the real problem is:
Sheets("Sheet2").Range("A").Value
which is invalid VBA and does include the For variable c
.
The correct object is:
c.Columns("A").Value
c
is a range for example A2:G2
or A13:G13
. I have taken column A of the range to give me A2
or A13
. I have then taken the value of that cell.
To replace the Selection
I have placed With
statement around the outside of this Replace
block to give the code below which I believe does what you want:
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.