简体   繁体   中英

Runtime error 91 vba

I've got this idea - in a file I'm setting name and file to search and want the VBA to open that file, iterate through all the sheets, find the value that i search for and return some values at the same row. First at all I only find a method to search only in one column, but in my file the value could be in 5-6 columns. Does anyone has a idea how can I search in hole worksheet? Also I've got simple solution (the data that i searched for will be always in columns D, F, H, J...) and I'll just check the worst case and iterate overall unless find it, return my information, close the file and close the macros. So far this is my code:

Sub BTS()

Dim RowID As Integer
Dim SiteID As String
Dim objFindSiteID As Range
Dim objControllerData As Workbook
Dim WS As Worksheet
Dim lastRow As Long
Dim v As Range



SiteID = ThisWorkbook.Sheets("Sheet1").Range("A3").Value

    If ThisWorkbook.Sheets("Sheet1").Range("B3").Value = "someValue" Then
       Set objControllerData = Workbooks.Open("C:\Users\bla\bababa\bla.xls", True, True)
    End If

    For Each WS In objControllerData.Worksheets
       lastRow = WS.Cells(WS.Rows.Count, "B").End(xlUp).Row
       For I = 1 To lastRow
         v = WS.Range("D" & I).Value
         If v = SiteID Then
           RowID = v.Row
           MsgBox lastRow
           MsgBox RowID
         End If
       Next I
    Next WS



End Sub

Im almost done with that task, but there is a problem - Run time error 91. I already managed error 424, 1004, but this one is rly hard to find why occurred :/

remove Dim v As Range , you give him a value, not a range.

or, keep the range, and remove .Value at the end of the equal line. then get the value to another primitive, like myValue = v.Value

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