Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

Method range of object worksheet failed try running userform

0

Hi 

I need fixing  my problem when  try  running userform  shows  error

Method range of object worksheet failed

a = sh.Range("A2:E" & sh.Range("A" & Rows.Count).End(xlUp)).Value

the  code  should show data  based on sheet with add auto number  in first  column in listbox. I 'm not sure   if  auto numbers in first  column in listbox causes  this  problem 

thanks  guys  

Answer
Discuss

Answers

0
Selected Answer

Tubrak

Your code is incomplete (and makes no sense so far) but you missed the .Row end when defining the range. It should read (addition in bold):

Private Sub UserForm_Initialize()
  Dim LastRow As Long
  Set sh = Sheets("ATS")
  a = sh.Range("A2:E" & sh.Range("A" & Rows.Count).End(xlUp).Row).Value
  With MY_List
    .ColumnCount = 6
    .ColumnWidths = "20;50;100;100;70;70;70"

  End With
End Sub

Hope this helps.

Discuss

Discussion

Hi John,
thanks  for  this important spotting  , now  it  run without any error.
tubrak (rep: 24) May 17, '23 at 11:32 pm
Add to Discussion
0

@Tubrak,

Another thing (error) I noticed is it seems you are wanting to assign a range object to " a ". If so, you should declare " a " as a Range and not a Variant.

You have declared a variable "LastRow" but have not given it a value. Add this line:

LastRow = Range("A" & Rows.Count).End(xlUp).Row

 My suggestion for your code is:
Private Sub UserForm_Initialize()
  Dim LastRow As Long
  Set sh = Sheets("ATS")
  LastRow = Range("A" & Rows.Count).End(xlUp).Row
   Set a = sh.Range("A2:E" & LastRow)
 
  With MY_List
  ' 6 columns but 7 widths ??
    .ColumnCount = 6
    .ColumnWidths = "20;50;100;100;70;70;70"
   End With

End Sub

You also state "ColumnCount = 6" but the next line has 7 widths

Hope this helps. If this solves your problem, please mark my answer as selected.

Cheers   :-)

Discuss

Discussion

Hi Willie !
thanks  but  gives mismach error in this line 
For i = 1 To UBound(a, 1)

as to  columns  yes  you'r  right.
in  reality I 'm still modify that's  why  you  see  7 columns widths
about " a "  this is  array  . you  can't  declare  as  range  you  can  do  that and  note  what happen.
tubrak (rep: 24) May 17, '23 at 11:35 pm
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login