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

subscript out of range to copy to specific sheet

0

Hi guys !

I need  repeat copying name(textbox1) to column B , number(textbot2) to column C for the empty cells . every time I will add data and there are empty cells for columns B,C . I put the result how should be after filling on userform and how should copy to sheet as in columns J,K as examples .
should ignore this case if columns B,C are filled , then just search for next empty empty cells in column B,C and fill them
so now I want looking for sheet name based on matching with selected optionbutton , then will fill empty cells for columns B,C 

the  code shows error subscript out  of  range  in this line 

Sheets("Opt" & s).Item(C).SpecialCells(4) = Controls("TextBox" & C - 1).Text

thanks

Answer
Discuss

Answers

0
Selected Answer

Ali

In the attached revised file, I've rewritten your code (and added explanatory comments). For convenience, I added a "Launch Form" button on worksheet "SH" with this code:

Sub TextBox1_Click()
    UserForm1.Show
End Sub

Firstly at the top level of the Userform code page (so it's "remembered"), I've declared a variable Opt to capture the user's button choice:

Dim Opt As String

Then each Optionbutton has code like this:

Private Sub OptionButton1_Click()
    ' overwrite the variable
    Opt = Me.OptionButton1.Caption
    'set button colour to light green
    Me.CommandButton1.BackColor = 12648384
End Sub

(but the bold numbers change). E.g. if the button labelled "SH" is clicked, Opt will become "SH" and the CommandButton will turn light green.

When the green commandbutton (now "Add Name...") is clicked, this code runs:

Private Sub CommandButton1_Click()
    'do nothing unless a button was clicked
    If Opt = "" Then Exit Sub
    ' with the chosen button (sheet):
    With ThisWorkbook.Worksheets(Opt)
        ' find extent (last row) of column A
        LstRw = .Cells(Rows.Count, 1).End(xlUp).Row
        ' loop down rows
        For n = 2 To LstRw
            ' check if column B is empty then add text
            If IsEmpty(.Cells(n, 2)) Then .Cells(n, 2).Value = Me.TextBox1.Text
            ' check if column C is empty then add text
            If IsEmpty(.Cells(n, 3)) Then .Cells(n, 3).Value = Me.TextBox2.Text
        Next n
    End With

    ' reset variable and button to grey
    Opt = ""
    Me.CommandButton1.BackColor = -2147483633
    'Close form
    Me.Hide
    Unload Me

End Sub

Note that it relies on your labels matching the worksheet names (if not, an error will occur); also that it will fill empty cells separately (e.g. if B is filled but C empty, it will write the value to C). If that is to be avoided, you could write both by replacing that portion with:

For n = 2 To LstRw
            ' check if column B is empty then add text to B and C
            If IsEmpty(.Cells(n, 2)) Then 
                 .Cells(n, 2).Value = Me.TextBox1.Text
                 .Cells(n, 3).Value = Me.TextBox2.Text
            End if 
      Next n
.

Hope this makes sense and works for you. If so, please remember to mark this Answer as Selected.

Discuss

Discussion

Hi John ,
this  is  exactly  what I want  it. thanks  for  your  work . 
Ali M (rep: 28) Apr 10, '23 at 6:21 am
Glad that worked for you. Thanks for selecting my Answer, Ali.
John_Ru (rep: 6142) Apr 10, '23 at 7:35 am
Add to Discussion
0

Hello Ali,

The "subscript out of range" error is telling you that you are trying to reference something or a variable that does not exist in your code. Based on this, review that line and figure out what is being reference that has not been declared.

Cheers   :-)

Discuss

Discussion

Hi Willie,
I  try  setting  like  this 
Set Opt = OptionButton

but  also  gives error object required 
Ali M (rep: 28) Apr 10, '23 at 4:00 am
Ali

That won't work since you don't have an object called "Optionbuttion"- your controls are Optionbutton1 etc. See my Answer for a way to collect the chosen button.
John_Ru (rep: 6142) Apr 10, '23 at 5:30 am
Add to Discussion


Answer the Question

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