Simple VBA code to copy from one sheet and paste in another

ijhoeq

Board Regular
Joined
Jun 20, 2018
Messages
61
Hello,

I'm trying to run this code to filter data from one sheet to another. It does nothing when I run the code. I'm wondering if it has something to do with missing code to activate the workbook or worksheet. Please let me know if you see an error or a better way to do this.

Code:
Private Sub FilterCommandButton_Click()
    
    'Copy all cells in columns A thru G
    Worksheets("Search Results").Range("B3", Range("B" & Rows.Count).End(xlUp)).Copy _
    Worksheets("Filtered Data").Range("A1")
    
    Worksheets("Search Results").Range("AD3", Range("AD" & Rows.Count).End(xlUp)).Copy _
    Worksheets("Filtered Data").Range("B1")
    
    Worksheets("Search Results").Range("AE3", Range("AE" & Rows.Count).End(xlUp)).Copy _
    Worksheets("Filtered Data").Range("C1")
    
    Worksheets("Search Results").Range("W3", Range("W" & Rows.Count).End(xlUp)).Copy _
    Worksheets("Filtered Data").Range("D1")
    
    Worksheets("Search Results").Range("X3", Range("X" & Rows.Count).End(xlUp)).Copy _
    Worksheets("Filtered Data").Range("E1")
    
    Worksheets("Search Results").Range("Y3", Range("Y" & Rows.Count).End(xlUp)).Copy _
    Worksheets("Filtered Data").Range("F1")
    
    Worksheets("Search Results").Range("Z3", Range("Z" & Rows.Count).End(xlUp)).Copy _
    Worksheets("Filtered Data").Range("G1")
    
    'Resize Columns
    Worksheets("Filtered Data").Columns("A").ColumnWidth = 12
    Worksheets("Filtered Data").Columns("B:C").ColumnWidth = 40
    Worksheets("Filtered Data").Columns("D:G").ColumnWidth = 5
    
    'Alignment Center
    Worksheets("Filtered Data").Rows("1").HorizontalAlignment = xlCenter
    Worksheets("Filtered Data").Columns("A").HorizontalAlignment = xlCenter
    Worksheets("Filtered Data").Columns("D:G").HorizontalAlignment = xlCenter

End Sub

Thanks for any help!
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
Try
Code:
Private Sub FilterCommandButton_Click()
    
    'Copy all cells in columns A thru G
    With Worksheets("Search Results")
      .Range("B3", .Range("B" & Rows.Count).End(xlUp)).Copy Worksheets("Filtered Data").Range("A1")
      .Range("AD3", .Range("AD" & Rows.Count).End(xlUp)).Copy Worksheets("Filtered Data").Range("B1")
      .Range("AE3", .Range("AE" & Rows.Count).End(xlUp)).Copy Worksheets("Filtered Data").Range("C1")
      .Range("W3", .Range("W" & Rows.Count).End(xlUp)).Copy Worksheets("Filtered Data").Range("D1")
      .Range("X3", .Range("X" & Rows.Count).End(xlUp)).Copy Worksheets("Filtered Data").Range("E1")
      .Range("Y3", .Range("Y" & Rows.Count).End(xlUp)).Copy Worksheets("Filtered Data").Range("F1")
      .Range("Z3", .Range("Z" & Rows.Count).End(xlUp)).Copy Worksheets("Filtered Data").Range("G1")
    End With
    'Resize Columns
    With Worksheets("Filtered Data")
      .Columns("A").ColumnWidth = 12
      .Columns("B:C").ColumnWidth = 40
      .Columns("D:G").ColumnWidth = 5
      
      'Alignment Center
      .Rows("1").HorizontalAlignment = xlCenter
      .Columns("A").HorizontalAlignment = xlCenter
      .Columns("D:G").HorizontalAlignment = xlCenter
   End With
End Sub
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,214,920
Messages
6,122,279
Members
449,075
Latest member
staticfluids

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