Quantcast
Channel: Active questions tagged excel - Stack Overflow
Viewing all articles
Browse latest Browse all 88854

Having trouble passing variable into a sub searching - getting runtime error '91'

$
0
0

I am trying to have my code run a search for the "Part Number" string in a cell then copy that entire column. I have the code working when I run it without passing the string in and replacing the variable, however, when I try to do it with a string passed in as colName I get an error on the 'Set r = ActiveSheet.Range(c.Address) line.'

Here's the main function

Public Sub PreSE()

Dim Wk As Workbook
Dim fname As String, fPathFile As String

fname = InputBox("Enter the file name to use, including file extension exactly as in the next window.")
Set Wk = Workbooks.Add
fPathFile = Application.GetSaveAsFilename(fname, "Excel Files (*.xlsx), *.xlsx")
Wk.SaveAs fPathFile

Call FindSelCol("Part Number")
Call FindSelCol("Manufacturer Part Number")
Call FindSelCol("Cage Code")
Call FindSelCol("Description")

End Sub

And the sub for finding and selecting the column

Sub FindSelCol(colName As String)


Dim c As Variant

With ActiveSheet.UsedRange
    Set c = .find(colName, LookIn:=xlValues)
    If Not c Is Nothing Then
        ActiveSheet.Range(c.Address).Offset(1, 0).Copy
    End If
End With

    Dim r As Range
    Set r = ActiveSheet.Range(c.Address) 'Erroring here

    ActiveSheet.Range(r, ActiveSheet.Cells(Rows.Count, r.Column).End(xlUp).Address).Copy

End Sub

Thanks!


Viewing all articles
Browse latest Browse all 88854


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>