Hello, I am terrible with functions but was looking for some help.
I have a bunch of SLookFor .Find code that look the same, so I wanted to convert it to a function so it would be cleaner. I wanted to do something like --
But this is not working, it is giving an error at the "Set oLookin = DT.Rows(1)" code. I believe it is because it is referencing the DT Workbook, but it is also not letting me define that in the Function itself (or I am doing it wrong). Would anyone be able to help? Hope I explained this correctly.
VBA Code:
Set OP = Workbooks.Add
OP.Worksheets("Sheet1").Name = "Transaction Download"
Set DT = OP.Worksheets("Transaction Download")
.
.
.
SLookFor = "FUND VALUE"
Set oLookin = DT.Rows(1)
Set oFound = oLookin.Find(What:=SLookFor, LookIn:=xlValues, LookAt:=xlPart, MatchCase:=True)
If Not oFound Is Nothing Then
FVCol = oFound.Column
Else
MsgBox "Could not find the '" & SLookFor & "' column in the Transaction Download sheet -- please review."
Exit Sub
End If
SLookFor = "PROGRAM CODE"
Set oLookin = DT.Rows(1)
Set oFound = oLookin.Find(What:=SLookFor, LookIn:=xlValues, LookAt:=xlPart, MatchCase:=True)
If Not oFound Is Nothing Then
PCCol = oFound.Column
Else
MsgBox "Could not find the '" & SLookFor & "' column in the Transaction Download sheet -- please review."
Exit Sub
End If
I have a bunch of SLookFor .Find code that look the same, so I wanted to convert it to a function so it would be cleaner. I wanted to do something like --
VBA Code:
FindRow("FUND VALUE")
.
.
Function FindRow(SLookFor as String)
Set oLookin = DT.Rows(1)
Set oFound = oLookin.Find(What:=SLookFor, LookIn:=xlValues, LookAt:=xlPart, MatchCase:=True)
If Not oFound Is Nothing Then
FVCol = oFound.Column
Else
MsgBox "Could not find the '" & SLookFor & "' column in the Transaction Download sheet -- please review."
Exit Sub
End If
But this is not working, it is giving an error at the "Set oLookin = DT.Rows(1)" code. I believe it is because it is referencing the DT Workbook, but it is also not letting me define that in the Function itself (or I am doing it wrong). Would anyone be able to help? Hope I explained this correctly.