Forum Discussion
aphk12
Jul 03, 2024Copper Contributor
How can I make this macro dynamic?
How can I replace the original row in "list orders export" with the rows in Sheet 3 and Sheet 4? This should be based on the name / data in the column B, in this example, "UBS Switzerland AG" i.e...
HansVogelaar
Jul 04, 2024MVP
Sub ReplaceUBSRow()
Dim wsExport As Worksheet
Dim wsSheet2 As Worksheet
Dim wsSheet3 As Worksheet
Dim investorName As String
Dim foundCell As Range
Dim foundRow As Long
' Set references to the sheets
Set wsExport = ThisWorkbook.Sheets("List Orders export")
Set wsSheet2 = ThisWorkbook.Sheets("Sheet2")
Set wsSheet3 = ThisWorkbook.Sheets("Sheet3")
' Set the investor name to search for
investorName = wsSheet2.Range("B2").Value
' Find the row with investor name in the "Export Order List" sheet
Set foundCell = wsExport.Range("B:B").Find(What:=investorName, _
LookAt:=xlWhole, MatchCase:=False)
' If the name is found
If Not foundCell Is Nothing Then
foundRow = foundCell.Row
' Delete the found row
foundCell.EntireRow.Delete
' Copy row 2 from Sheet2 and Sheet3 and insert into Export
wsSheet2.Rows(2).Copy
wsExport.Rows(foundRow).Insert
wsSheet3.Rows(2).Copy
wsExport.Rows(foundRow).Insert
Application.CutCopyMode = False
End If
End Sub