I am new to VBA and have been trying to create a macro that will allow me to find where certain information is located in one worksheet, then paste other information to those corresponding rows in another worksheet.
So far I have been able to find where the information is located using .find and .findnext
I can copy the information I want from the second worksheet and select it to be pasted. I can not paste the multiple rows though. If I do it manually and type in which row I would like the info to go to it works, but I can't get it to work with the rest of my program on its own.
Below is the relevant code that I have been working on, all help is welcome. I don't know much so please explain if you have any other better ideas.
Dim rngthis As Range
Dim rngfind As Range
Dim firstaddress As String
Dim addSelection As String
'set range to all of Col A
Set rngthis = Sheet1.Columns("A")
With rngthis
'find all cells that have "item" in them
Set rngfind = Sheet1.Cells.Find(What:="Item", _ After:=[A1], SearchOrder:=xlByRows, _ SearchDirection:=xlPrevious)
'store in addselection
If Not rngfind Is Nothing Then
firstaddress = rngfind.Address
addSelection = addSelection & rngfind.Address & ","
Do
Set rngfind = .FindNext(rngfind)
addSelection = addSelection & rngfind.Address & ","
Loop While Not rngfind Is Nothing And_ rngfind.Address <> firstaddress
End If
End With
addSelection = Mid(addSelection, 1, Len(addSelection) - 1)
Sheet1.Range(addSelection).Rows.Select
Set rngthis = Nothing
'Call different worksheet and copy cells needed
Sheets("Yearly Capital Repl. Schd. ").Select
ActiveSheet.Range("A9:AE10").Select
Selection.Copy
'paste copied cells into found rows from above
ActiveSheet.Range(addSelection).Select
ActiveSheet.Paste
So far I have been able to find where the information is located using .find and .findnext
I can copy the information I want from the second worksheet and select it to be pasted. I can not paste the multiple rows though. If I do it manually and type in which row I would like the info to go to it works, but I can't get it to work with the rest of my program on its own.
Below is the relevant code that I have been working on, all help is welcome. I don't know much so please explain if you have any other better ideas.
Dim rngthis As Range
Dim rngfind As Range
Dim firstaddress As String
Dim addSelection As String
'set range to all of Col A
Set rngthis = Sheet1.Columns("A")
With rngthis
'find all cells that have "item" in them
Set rngfind = Sheet1.Cells.Find(What:="Item", _ After:=[A1], SearchOrder:=xlByRows, _ SearchDirection:=xlPrevious)
'store in addselection
If Not rngfind Is Nothing Then
firstaddress = rngfind.Address
addSelection = addSelection & rngfind.Address & ","
Do
Set rngfind = .FindNext(rngfind)
addSelection = addSelection & rngfind.Address & ","
Loop While Not rngfind Is Nothing And_ rngfind.Address <> firstaddress
End If
End With
addSelection = Mid(addSelection, 1, Len(addSelection) - 1)
Sheet1.Range(addSelection).Rows.Select
Set rngthis = Nothing
'Call different worksheet and copy cells needed
Sheets("Yearly Capital Repl. Schd. ").Select
ActiveSheet.Range("A9:AE10").Select
Selection.Copy
'paste copied cells into found rows from above
ActiveSheet.Range(addSelection).Select
ActiveSheet.Paste