簡體   English   中英

搜索lastrow VBA Excel時,對象_worksheet的方法“范圍”失敗

[英]Method “Range” of object _worksheet failed when searching for lastrow VBA Excel

因此,這是我對您的專家提出的第二個問題。 我試圖找到一列的最后一行,但是我繼續使“對象工作表的方法范圍失敗。

我曾嘗試將語法更改為各種內容,但仍然無法解決。

我是VBA excel的新手,非常感謝您的幫助。

謝謝。 違規行標有********

Private Sub GetProductCode()
'Opens jobs file
'Trims down customer reference column to job number only, then searches job file to return product code
Dim sJobNumber As String
Dim sCustomerReference As String
Dim wsRevRownum As Long
Dim wbkJobs As Workbook
Dim wsJobs As Worksheet
Dim wsRev As Worksheet
Dim strPathFile As String
Dim strSearch As String
Dim fCell As Range
Dim fCellRowNum As String
Dim wsJobsLastrow As Long
Dim wsRevLastrow As Long


'On Error GoTo ErrHandler
Application.ScreenUpdating = False

'Change as desired
    strPathFile = "\\ACHILLES\Company\Production_schedule\Jobs.xlsm"
    strSearch = "Specific text"

 Set wbkJobs = Workbooks.Open _
              (Filename:=strPathFile, _
              UpdateLinks:=0, _
              ReadOnly:=True, _
              AddToMRU:=False)

 Set wsRev = ThisWorkbook.Sheets(3)
 MsgBox wsRev.Name

 Set wsJobs = wbkJobs.Sheets("Jobs")

'MsgBox wbkJobs.Name
 wsRevRownum = 2

With wsRev
 wsRevLastrow = wsRev.Range("E" & Rows.Count).End(xlUp).Row ********
End With

With wsJobs
 wsJobsLastrow = wsJobs.Range("A" & Rows.Count).End(xlUp).Row
End With


'Loop through revenue file and search for entries in jobs file. If found then take the product code from the jobs file and populate revenue file
Do Until wsRev.Range("E" & wsRevRownum).Address = (wsRevLastrow + 1)
    'initialise variables
    sCustomerReference = ""
    sJobNumber = ""
    strSearch = ""

如果要在工作表中查找最后一行,請嘗試以下方法之一:

Sub GetLastRow()

'Different ways to find the last row number of a range

Dim ws As Worksheet
Dim LastRow As Long

Set ws = ThisWorkbook.Worksheets("Sheet1")

'Ctrl + Shift + End
  LastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row

'Using UsedRange
  ws.UsedRange 'Refresh UsedRange
  LastRow = ws.UsedRange.Rows(ws.UsedRange.Rows.Count).Row

'Using Table Range
  LastRow = ws.ListObjects("Table1").Range.Rows.Count

'Using Named Range
  LastRow = ws.Range("MyNamedRange").Rows.Count

'Ctrl + Shift + Down (Range should be first cell in data set)
  LastRow = ws.Range("A1").CurrentRegion.Rows.Count

End Sub

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM