Code to select a sheet based on value cell

dpaton05

Well-known Member
Joined
Aug 14, 2018
Messages
2,375
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I have a sheet that has I13 to contain a month and year for instance July 2018 and sheet names for instance July 2018 and i AM trying to copy the first 4 columns of July 2018 and paste them into a new workbook. What is wrong with this code?


Private Sub cmdCopySheet_Click()
Dim CopyMonth As String
CopyMonth = Range("I13").Value

Worksheets(CopyMonth).Select
Columns("A:D").copy
Workbooks.Add
ActiveSheet.Paste
End Sub
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
If I13 contains a Date, try :
Code:
CopyMonth = Format([I13], "mmmm yyyy")
 
Upvote 0
Thanks but I got it working with this code already:

Code:
Dim CopyMonth As String
    CopyMonth = Range("I13").Value
    
    Worksheets(CopyMonth).Activate
    Worksheets(CopyMonth).Columns("A:D").copy
    Worksheets("home").Activate
    Workbooks.Add
    ActiveSheet.Paste
 
Upvote 0

Forum statistics

Threads
1,223,993
Messages
6,175,842
Members
452,675
Latest member
duongtruc1610

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