VBA Cell Value used in Worksheet("")

ChloeDage22

New Member
Joined
Sep 28, 2020
Messages
6
Office Version
  1. 365
This is my code. Im trying to refer to a cell A1 which holds the name of the worksheet DATA. And i want that cell to be referenced in my code. Could anyone help me as to why this is not working
VBA Code:
Sub CommandButton1_Click()
a = Worksheets(Range("A1")).Cells(Rows.Count, 1).End(xlUp).Row

    For i = 2 To a
        If Worksheets(Range("A1")).Cells(i, 6).Value = "NHOLOHAN" Then
            Worksheets(Range("A1")).Rows(i).Copy
           
           Worksheets("DAYS").Activate
            b = Worksheets("DAYS").Cells(Rows.Count, 1).End(xlUp).Row
                Worksheets("DAYS").Cells(b + 1, 1).Select
            ActiveSheet.Paste
          
                    Worksheets(Range("A1")).Activate
End If
 
Last edited by a moderator:

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
Do you have at least a Next and an End Sub line after that?
 
Upvote 0
This is my code. Im trying to refer to a cell A1 which holds the name of the worksheet DATA. And i want that cell to be referenced in my code. Could anyone help me as to why this is not working
Rich (BB code):
Sub CommandButton1_Click()
a = Worksheets(Range("A1").Value).Cells(Rows.Count, 1).End(xlUp).Row

    For i = 2 To a
        If Worksheets(Range("A1").Value).Cells(i, 6).Value = "NHOLOHAN" Then
            Worksheets(Range("A1").Value).Rows(i).Copy
         
           Worksheets("DAYS").Activate
            b = Worksheets("DAYS").Cells(Rows.Count, 1).End(xlUp).Row
                Worksheets("DAYS").Cells(b + 1, 1).Select
            ActiveSheet.Paste
        
                    Worksheets(Range("A1").Value).Activate
End If
Try adding all of the ".Value" property designations that I show in red above.
 
Upvote 0
You might also consider a non-looping variation of your macro like this:

VBA Code:
Sub test1()
    On Error Resume Next
    With Worksheets(Range("A1").Value)
        .Range("F:F").AutoFilter Field:=1, Criteria1:="NHOLOHAN"
        .Range("A2:A10000").SpecialCells(xlCellTypeVisible).EntireRow.Copy Sheets("DAYS").Cells(Rows.Count, "A").End(xlUp).Offset(1)
        .Range("F:F").AutoFilter
    End With
    
End Sub
 
Upvote 0
I'm now getting this error '9' Subscript out of range for this line. I'm not quite sure why

Sub CommandButton1_Click()
a = Worksheets(Range("A1").Value).Cells(Rows.Count, 1).End(xlUp).Row

For i = 3 To a
If Worksheets(Range("A1").Value).Cells(i, 6).Value = "NHOLOHAN" Then
Worksheets(Range("A1").Value).Rows(i).Copy

Worksheets(Range("G1").Value).Activate
b = Worksheets(Range("G1").Value).Cells(Rows.Count, 1).End(xlUp).Row
Worksheets(Range("G1").Value).Cells(b + 1, 1).Select
ActiveSheet.Paste

Worksheets(Range("A1").Value).Activate
End If
Next

For i = 3 To a
If Worksheets(Range("A1").Value).Cells(i, 6).Value = "JODENTHAL" Then
Worksheets(Range("A1").Value).Rows(i).Copy

Worksheets(Range("G1").Value).Activate
b = Worksheets(Range("G1").Value).Cells(Rows.Count, 1).End(xlUp).Row
Worksheets(Range("G1").Value).Cells(b + 1, 1).Select
ActiveSheet.Paste



Worksheets(Range("A1").Value).Activate
End If
 
Upvote 0

Forum statistics

Threads
1,214,798
Messages
6,121,635
Members
449,043
Latest member
farhansadik

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