fixing error subscript out of range in userform

abdelfattah

Well-known Member
Joined
May 3, 2019
Messages
985
Office Version
  1. 2010
hello
i have a problem about this error subscript out of range when run userform and highlights in this line in module
VBA Code:
userform2.show
I'm pretty sure about name of userform and this is the whole codes
Code:
Private Sub Commandbutton1_Click()
Dim cNum As Integer
Dim X As Integer
Dim nextrow As Range
Dim sht As String
sht = ComboBox1.Value
If Me.ComboBox1.Value = "" Then
MsgBox "Select a sheet from the combobox and add the date"
Exit Sub
End If
cNum = 4
Set nextrow = Sheets(sht).Cells(Rows.Count, 1).End(xlUp).Offset(1, 0)
For X = 1 To cNum
nextrow = Me.Controls("Reg" & X).Value
Set nextrow = nextrow.Offset(0, 1)
Next
For X = 1 To cNum
Me.Controls("Reg" & X).Value = ""
Next

MsgBox "The values have been sent to the " & sht & " sheet"

End Sub

Private Sub UserForm_Initialize()
Dim s As Long
    For s = 1 To Sheets.Count
        ComboBox1.AddItem Sheets(s).Name
    Next
 
  Dim ws As Worksheet, a, i As Long, j As Long
 
 ListBox1.ColumnWidths = "100;85;85;80;50"
ListBox1.ColumnCount = 4
Z = 1
For j = 1 To 4
  inarr = Sheets("SH" & Format(j, "00")).Range("A3:l" & [a65536].End(3).Row).Value
  With ListBox1
    For i = 1 To UBound(inarr)
      .AddItem
      .List(Z, 0) = (inarr(i, 1))
      .List(Z, 1) = (inarr(i, 2))
      .List(Z, 2) = (inarr(i, 3))
      .List(Z, 3) = (inarr(i, 4))
      Z = Z + 1
    Next i
  End With
Next j
End Sub
I hope anybody help
thanks
 

Some videos you may like

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.

dmt32

Well-known Member
Joined
Jul 3, 2012
Messages
6,484
Office Version
  1. 2019
Platform
  1. Windows
actually about sheets I have four sheets but the first name is list if you see change from 2 to 4 also doesn't success

Sorry
actually about sheets I have four sheets but the first name is list if you see change from 2 to 4 also doesn't success
fat finger I meant last one to be List

Rich (BB code):
For j = 1 To 3
  inarr = Sheets("SH" & j).Range("A3:D" & [a65536].End(3).Row).Value

If you only want SH1 SH2 SH3 try changes above & see if helps

Dave
 

abdelfattah

Well-known Member
Joined
May 3, 2019
Messages
985
Office Version
  1. 2010
For j = 1 To 3
inarr = Sheets("SH" & j).Range("A3:D" & [a65536].End(3).Row).Value
it gives me another error
could not set the list property .invalid property array idex
in this line
VBA Code:
 .List(Z, 0) = (inarr(i, 1))
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
55,574
Office Version
  1. 365
Platform
  1. Windows

ADVERTISEMENT

It needs to be
VBA Code:
With Sheets("SH" & j)
   inarr = .Range("A3:D" & .[a65536].End(3).Row).Value
End With
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
55,574
Office Version
  1. 365
Platform
  1. Windows

ADVERTISEMENT

Not on your test file it doesn't, unless you have changed it.
 

abdelfattah

Well-known Member
Joined
May 3, 2019
Messages
985
Office Version
  1. 2010
this is what I have
VBA Code:
Private Sub UserForm_Initialize()
Dim s As Long
    For s = 1 To Sheets.Count
        ComboBox1.AddItem Sheets(s).Name
    Next

  Dim ws As Worksheet, a, i As Long, j As Long

ListBox1.ColumnWidths = "100;85;85;80;50"
ListBox1.ColumnCount = 4
Z = 1
For j = 1 To 4
  With Sheets("SH" & j)
   inarr = .Range("A3:D" & .[a65536].End(3).Row).Value
End With
  With ListBox1
    For i = 1 To UBound(inarr)
      .AddItem
   
      .List(Z, 0) = (inarr(i, 1))
      .List(Z, 1) = (inarr(i, 2))
      .List(Z, 2) = (inarr(i, 3))
      .List(Z, 3) = (inarr(i, 4))
      Z = Z + 1
    Next i
  End With
Next j
End Sub
and the sheets name sh1,sh2,sh3
despite all of this the error still shows
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
55,574
Office Version
  1. 365
Platform
  1. Windows
Why have you put the loop back to 4?
 

abdelfattah

Well-known Member
Joined
May 3, 2019
Messages
985
Office Version
  1. 2010
I have four sheet the firs sheet is different about name starts from the second sheet to four sheet based on names sh1,2,3 it loops from 1 to 4 , is it problem? what I should do form 2 to 4 or what !
 

Watch MrExcel Video

Forum statistics

Threads
1,127,650
Messages
5,626,084
Members
416,161
Latest member
David1966Lewis

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top