简体   繁体   中英

Excel VBA Pass Worksheet Name as Variable

I am trying to pass a worksheet name in as a variable to the following VBA. I am obviously doing something wrong because I keep getting Invalid Qualifier errors. This function works when I have the actual sheet name in it, but I want to be able to use it with different worksheets. Thanks!

Function fnGetRowNumSNCT(ByVal valueToFind As Long, SheetName As String) As Long
   
    Dim matchResult As Variant

    matchResult = Application.Match(valueToFind, SheetName.Columns(1), 0)
    

    If Not IsError(matchResult) Then
       fnGetRowNumSNCT = matchResult
    End If
    
End Function

The sub routine that calls it is this:
Private Sub cmdView_Click()

'open SNCT form to selected record

'On Error goto Err_cmdView_Click

Dim lngRecID As Long
Dim lngRowNum As Long
Dim strMRN As String
Dim strSheetName As String

'get selected recordID
For i = 0 To lstSNCT.ListCount - 1
    If lstSNCT.Selected(i) = True Then
        lngRecID = strSelectedItems & lstSNCT.List(i)
        Exit For
    End If
Next i

strSheetName = "Sheet3"
lngRowNum = fnGetRowNumSNCT(lngRecID, strSheetName)

Return Row of Found Number (ID) in First Column

Worksheet

Option Explicit

Function fnGetRowNumSNCT( _
    ByVal ws As Worksheet, _
    ByVal SearchInteger As Long) _
As Long
    If ws Is Nothing Then Exit Function
  
    Dim RowNum As Variant
    RowNum = Application.Match(SearchInteger, ws.Columns(1), 0)
    
    If IsNumeric(RowNum) Then
        fnGetRowNumSNCT = RowNum
    End If
    
End Function

Private Sub cmdView_Click()
    
    Dim wb As Workbook: Set wb = ThisWorkbook ' workbook containing this code
    Dim ws As Worksheet: Set ws = wb.Worksheets("Sheet3")
    Dim lngRecID As Long: lngRecID = 3
    
    Dim lngRowNum As Long: lngRowNum = fnGetRowNumSNCT(ws, lngRecID)
    
    If lngRowNum > 0 Then
        MsgBox "'RecID' found in row " & lngRowNum, vbInformation
    Else
        MsgBox "'RecID' not found.", vbCritical
    End If
    
End Sub
  • You could also consider one of the following solutions.

Workbook

Function fnGetRowNumSNCTWorkbook( _
    ByVal wb As Workbook, _
    ByVal WorksheetName As String, _
    ByVal SearchNumber As Long) _
As Long
    If wb Is Nothing Then Exit Function
    
    On Error Resume Next
    Dim ws As Worksheet: Set ws = wb.Worksheets(WorksheetName)
    On Error GoTo 0
    
    If ws Is Nothing Then
        MsgBox "The worksheet '" & WorksheetName & "' doesn't exist.", _
            vbCritical
        Exit Function
    End If
       
    Dim RowNum As Variant
    RowNum = Application.Match(SearchNumber, ws.Columns(1), 0)
    
    If IsNumeric(RowNum) Then
        fnGetRowNumSNCTWorkbook = RowNum
    End If
    
End Function

Sub TestWorkbook()
    
    Dim wb As Workbook: Set wb = ThisWorkbook ' workbook containing this code
    Dim lngRecID As Long: lngRecID = 3
    
    Dim lngRowNum As Long
    lngRowNum = fnGetRowNumSNCTWorkbook(wb, "Sheet1", lngRecID)
    
    If lngRowNum > 0 Then
        MsgBox "'RecID' found in row " & lngRowNum, vbInformation
    Else
        MsgBox "'RecID' not found.", vbCritical
    End If
    
End Sub

Range

Function fnGetRowNumSNCTRange( _
    ByVal rg As Range, _
    ByVal SearchNumber As Long) _
As Long
    If rg Is Nothing Then Exit Function
    
    Dim RowNum As Variant: RowNum = Application.Match(SearchNumber, rg, 0)
    
    If IsNumeric(RowNum) Then
        fnGetRowNumSNCTRange = RowNum
    End If
    
End Function

Sub TestRange()
    
    Dim wb As Workbook: Set wb = ThisWorkbook ' workbook containing this code
    ' Note that an error will occur if 'Sheet3' doesn't exist.
    Dim ws As Worksheet: Set ws = wb.Worksheets("Sheet3")
    Dim rg As Range: Set rg = ws.Columns(1)
    ' Or just (not recommended):
    'Dim rg As Range: Set rg = ThisWorkbook.Worksheets("Sheet3").Columns(1)
    Dim lngRecID As Long: lngRecID = 3
    
    Dim lngRowNum As Long: lngRowNum = fnGetRowNumSNCTRange(rg, lngRecID)
    
    If lngRowNum > 0 Then
        MsgBox "'RecID' found in row " & lngRowNum, vbInformation
    Else
        MsgBox "'RecID' not found.", vbCritical
    End If
    
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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM