I have these two functions that are checking for a column index for the specific header assigned to each. For the first function, sys_position there is no problem and the message box appears, however in the second function partnum_position the expected array error is thrown. The only difference that I can see of the two is the specific header that is assigned in `Set sys_Rng = sht.Cells.Find("System Number"). Does anyone have any idea why this is happening?
Public Sub Main()
Dim wb As Workbook, ws As Worksheet, system_position As Integer, partnum_position As Integer
Set wb = ThisWorkbook
Set ws = wb.Worksheets("Sheet1")
system_position = sys_position(ws)
partnum_position = partnum_position(ws)
End Sub
Function sys_position(sys_Rng As Variant) As Integer
Dim sht As Worksheet
Set sht = Worksheets("Sheet1") 'define start sheet
Set sys_Rng = sht.Cells.Find("System Number(s)") 'set values to look for
If Not sys_Rng Is Nothing Then 'if found then do this oporation
MsgBox ("System Number Column is " & sys_Rng.Column)
Else
MsgBox ("No Header Found.")
End If
Set sys_Rng = Nothing ' clear variable
Set sht = Nothing ' clear variable
End Function
Function partnum_position(partnum_Rng As Variant) As Integer
Dim sht As Worksheet
Set sht = Worksheets("Sheet1") 'define start sheet
Set partnum_Rng = sht.Cells.Find("Part Number")
If Not partnum_Rng Is Nothing Then
MsgBox ("Part Number Column is " & partnum_Rng.Column)
Else
MsgBox ("No " & partnum_Rng & " Header Found.")
End If
Set partnum_Rng = Nothing ' clear variable
Set sht = Nothing ' clear variable
End Function
EDIT
Public Sub Main()
Dim wb As Workbook, ws As Worksheet, i As Range, dict As Object, system_position As Long, partnum_position As Long
Dim wbSrc As Workbook
Set wb = ThisWorkbook
Set ws = wb.Worksheets("Sheet1")
Call position
End Sub
Sub position()
Dim sht As Worksheet
Dim sys_Rng As Range, partnum_Rng As Range, syswaivernum As Range, partnumber As Range
Set sht = Worksheets("Sheet1") 'define start sheet
Set sys_Rng = sht.Cells.Find("System Waiver Number(s)") 'set values to look for
Set partnum_Rng = sht.Cells.Find("Axsun Part Number")
If Not sys_Rng Is Nothing Then 'if found then do this oporation
MsgBox ("System Waiver Number Column is " & sys_Rng.Column)
Else
MsgBox ("No Header Found.")
End If
If Not partnum_Rng Is Nothing Then
MsgBox ("Axsun Part Number Column is " & partnum_Rng.Column)
Else
MsgBox ("No " & partnum_Rng & " Header Found.")
End If
Set sys_Rng = Nothing ' clear variable
Set partnum_Rng = Nothing ' clear variable
Set sht = Nothing ' clear variable
End Sub
Find, always specify theWhat,LookAt, andLookInparameters. See the remarks in the docs.Set sys_Rng = sht.Cells.Find("System Number(s)")to be before I call the function ` system_position = sys_position(ws)`