Running the macro below for the first time there is no problem but running the macro the second time I get an error "Method 'Rows' Of Object '_Global' Failed" in the function "lastrow" ...
Can anyone explain why this happens the second time ?
rest of the code:
Can anyone explain why this happens the second time ?
Code:
Sub test()
read_NCdata "4022.635.94672"
End Sub
rest of the code:
Code:
Sub read_NCdata(NC As String)
'Declare the Excel workbook and application objects
Dim xlApp As Excel.Application
Dim xlWB As Excel.Workbook
Dim xlWS As Excel.Worksheet
Dim NCrow, i, LR, LC As Integer
Const WBname As String = "T:\Admin\TOC\12NCdatabase.xlsx"
Const WSinfo As String = "12NC Info"
Const WSautotext As String = "Autotext Info"
Set xlApp = New Excel.Application
xlApp.Visible = True 'excel visible yes or no
Set xlWB = xlApp.Workbooks.Open(WBname)
With xlWB.Sheets(WSinfo)
LR = lastrow(1)
NCrow = WorksheetFunction.Match(NC, xlWB.Sheets(WSinfo).Range("A1:A" & LR), 0)
LC = lastcolumn(NCrow)
End With
ReDim NCdata(LC)
For i = 1 To LC
NCdata(i) = xlWB.Sheets(WSinfo).Cells(NCrow, i).Value
Debug.Print NCdata(i)
Next i
xlWB.Close
xlApp.Quit
Set xlApp = Nothing
Set xlWB = Nothing
Set xlWS = Nothing
End Sub
Function lastrow(Clm As Integer) As Long
lastrow = Cells(Rows.count, Clm).End(xlUp).Offset(Abs(Cells(Rows.count, Clm).End(xlUp).Value <> ""), 0).Row - 1
End Function
Function lastcolumn(Rw) As Long
lastcolumn = Cells(Rw, Columns.count).End(xlToLeft).Offset(Abs(Cells(Rw, Columns.count).End(xlToLeft).Value <> ""), 0).Column
End Function