Please help, Subscript out of range error

JohnnyCool

New Member
Joined
Dec 31, 2016
Messages
2
Hi, I'm currently trying to write a VBA code, it looks like this:

Dim currentTopPos1 As Long
Private Sub cmdSaveData1_Click()
Dim settings As Variant
Dim info() As String
Dim index As Long

With Sheets(sheet_settings1)
ReDim info(1 To getLR1(.Name, "A") - 1)
settings = .Range("A2:D" & getLR1(.Name, "D")).Value

For index = LBound(settings, 1) To UBound(settings, 1)
With Me
info(index) = .Controls(settings(index, 1))
End With
Next index
End With

With Sheets(sheet_data1) (*Debug points here*)
.Range("A" & getLR1(.Name, "A") + 1).Resize(, UBound(info)) = info
End With
End Sub


Private Sub UserForm_Activate()
With Me
.ScrollBars = fmScrollBarsVertical
.ScrollHeight = currentTopPos1 * 1.2
End With
End Sub


Private Sub UserForm_Initialize()
Dim settings As Variant
Dim index As Long
Dim label As MSForms.label
Dim textbox As MSForms.textbox

currentTopPos = 20
With Sheets(sheet_settings1)
settings = .Range("A2:D" & getLR1(.Name, "D")).Value

For index = LBound(settings, 1) To UBound(settings, 1)
With Me
Set label = .Controls.Add("forms.label.1")
With label
.Left = UI_LEFT1
.Top = currentTopPos1
.Width = settings(index, 4)
.Height = UI_LINE_HEIGHT1
currentTopPos1 = .Top + .Height + UI_GAP1
.Caption = settings(index, 2)
End With
Set textbox = .Controls.Add("Forms.textbox.1")
With textbox
.Name = settings(index, 1)
.Left = UI_LEFT1
.Top = currentTopPos1
.Width = settings(index, 4)
.Height = settings(index, 3) * UI_LINE_HEIGHT1
If settings(index, 3) > 1 Then .MultiLine = True
currentTopPos1 = .Top + .Height + UI_GAP1
End With
.Height = 300
.Width = 380
.Caption = "Form nhap du lieu"
End With
Next index
End With
With cmdSaveData1
.Top = currentTopPos1
.Left = 310 - cmdSaveData1.Width
End With
End Sub


When I try to click the Save button, the error occurs (Subscript out of range). When I hit Debug, it points me to With Sheets(sheet_data1).

What should I do?

P/s: I followed the guide on this link: https://www.youtube.com/watch?v=arCda8sm524, I tested it myself and it worked, but when I tried to do another sheet with more columns (for the example in the link, it has 7 things in the form, But in MY case, I have 19 things), it just doesn't work.

Please help.
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Oh wait, dang it, I found my own problem.

I named my Sheet Data 1 (with space) while in the line I wrote sheet_data1 (no space)

I guess 1 space is just too much space huh :v
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

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
Back
Top