问题
Trying to get text boxes to populate when a name is selected from my combo box (cboCo). Getting the error method range of object'_worksheet' failed
on fourth line of code.
I am not a strong programmer and VB has always been a struggle for me. I am working on creating a form for a vendor spreadsheet that will allow users to view data in the spreadsheet, edit vendor data, add new data and delete data as needed. I so far have got it to add data and show the company names in the combo box. What I am working on currently is getting the text boxes to populate with the data in the row for the company selected in the combobox.
Private Sub cboCo_Change()
Dim iRow As Long, LastRow As Long, ws As Worksheet
Set ws = Worksheets("VendorInfo")
LastRow = ws.Range(1 & Rows.Count).End(x1Up).Row
For iRow = 2 To LastRow
If (Me.cboCo.Value) = ws.Cells(iRow, 1) Then
Me.cboCat = ws.Cells(iRow, 19).Value
Me.cboCat = ws.Cells(iRow, 19).Value
Me.cboYrApprv = ws.Cells(iRow, 14).Value
Me.txtContact = ws.Cells(iRow, 2).Value
Me.txtPhone = ws.Cells(iRow, 3).Value
Me.txtEmail = ws.Cells(iRow, 4).Value
Me.txtCoAdd = ws.Cells(iRow, 5).Value
Me.txtWebSite = ws.Cells(iRow, 6).Value
Me.txtAccred = ws.Cells(iRow, 8).Value
Me.txtStanding = ws.Cells(iRow, 9).Value
Me.txtSince = ws.Cells(iRow, 10).Value
Me.txtNotes = ws.Cells(iRow, 11).Value
Me.txtVerified = ws.Cells(iRow, 12).Value
Me.txtToday = ws.Cells(iRow, 13).Value
Me.txtServProd = ws.Cells(iRow, 7).Value
Me.txtApprvBy = ws.Cells(iRow, 15).Value
Me.txtAprvReas = ws.Cells(iRow, 16).Value
Me.txtOrder = ws.Cells(iRow, 17).Value
Me.txtPurchs = ws.Cells(iRow, 18).Value
End If
Next iRow
End Sub
When I select a company from my cboCo combo box I get the method range of object'_worksheet' failed
error on the fourth Line (starting with LastRow just above my loop)
回答1:
You are calling Range()
incorrectly. Try this:
LastRow = ws.Cells(1, Rows.Count).End(xlUp).Row
or
LastRow = ws.Range("A" & rows.count).End(xlUp).Row
Originally, it was resolving to basically ws.Range(1 & 1048576)...
--> ws.Range(11048576).End(xlUp).Row
, which A) isn't a proper Range
and B) is a non-existent row number.
Also, note it's xlUp
, not x1Up
(it's an "L", not a "1")
来源:https://stackoverflow.com/questions/54258430/getting-an-a-error-when-i-try-to-populate-text-boxes-based-on-combo-box-selectio