Code to select a sheet based on value cell

dpaton05

Well-known Member
Joined
Aug 14, 2018
Messages
2,352
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

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
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,214,922
Messages
6,122,281
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