Referencing a newly created Workbook in a Function

cappla011

New Member
Joined
Mar 13, 2013
Messages
35
Office Version
  1. 365
Platform
  1. Windows
Hello, I am terrible with functions but was looking for some help.

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.
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
I'd add the search range as another argument:

VBA Code:
Function FindCol(SLookFor as String, SearchRange as Range) As Long
   Dim oFound as Range
   Set oFound = SearchRange.Find(What:=SLookFor, LookIn:=xlValues, LookAt:=xlPart, MatchCase:=True)
    If Not oFound Is Nothing Then
        FindCol = oFound.Column
    Else
        FindCol = 0
    End If
End Function

then call it with:

VBA Code:
SLookFor ="FUND VALUE"
FVCol = FindCol(SLookFor, DT.Rows(1))
If FVCol = 0 then
        MsgBox "Could not find the '" & SLookFor & "' column in the Transaction Download sheet -- please review."
        Exit Sub
End If
 
Upvote 0
Solution

Forum statistics

Threads
1,224,823
Messages
6,181,171
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top