vba copy range to new workbook

hajiali

Well-known Member
Joined
Sep 8, 2018
Messages
624
Office Version
  1. 2016
Platform
  1. Windows
Hello all,

Im needing a macro when pressed to Copy range("T:AD") to new sheet in "A1" and to copy the sheet the amount of time as the number of days in the month that's in "AD1"

Ex. if AD1=12/1/2020 then to copy the sheet 31 times as there are 31 days in December.
with each New sheet to change the value in "K1" to next day. and rename the sheet to value of "K1" with the "DD" format.

any help is greatly appreciate.
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
See if you can use this:

VBA Code:
Sub t()
Dim ct As Long, sh As Worksheet, dt As Date
With ActiveSheet
Set sh = Sheets(.Range("A1").Value)
ct = Day(Application.EoMonth(Range("AD1"), 0))
dt = .Range("K1").Value
    For i = 1 To ct
        dt = dt + 1
        Sheets.Add After:=Sheets(Sheets.Count)
        With sh
            Intersect(.UsedRange, .Columns("T:AD")).Copy ActiveSheet.Range("A1")
        End With
        Sheets(Sheets.Count).Name = Format(dt, "DD-MM-YYYY") 'Not sure what you want here.
    Next
End With
End Sub
 
Last edited:
Upvote 0
Im getting a error on the following line

VBA Code:
Set sh = Sheets(.Range("A1").Value)

Error is "run-time error '9': Subscript out of range
 
Upvote 0
I think I got confused about which sheet you want to copy from and which you want to paste to. Try this.

VBA Code:
Sub t()
Dim ct As Long, sh As Worksheet, dt As Date
Set sh = ActiveSheet
ct = Day(Application.EoMonth(sh.Range("AD1"), 0))
dt = sh.Range("K1").Value
    For i = 1 To ct
        dt = dt + 1
        Sheets.Add After:=Sheets(Sheets.Count)
        With sh
            Intersect(.UsedRange, .Columns("T:AD")).Copy ActiveSheet.Range("A1")
        End With
        Sheets(Sheets.Count).Name = Format(dt, "DD-MM-YYYY") 'Not sure what you want here.
    Next
End Sub
 
Upvote 0
That fixed that error. few more fixes. the value of each new sheet in K1 is 12/1/2020 and does not add the 1 day to each new sheet. I would also like the function to auto column width if to each new sheet.
 
Upvote 0
I was able to get the auto width function added to the code just can not seem to get the Add 1 day to each new sheet.
 
Upvote 0
This adds the Autofit for columns A:K. My test setup indicates that the day is being added to the value in K1.. It produces sheet names in the format "DD-MM--YYYY". If you want something different you can modify the line with the comment on it.

VBA Code:
Sub t()
Dim ct As Long, sh As Worksheet, dt As Date
Set sh = ActiveSheet
ct = Day(Application.EoMonth(sh.Range("AD1"), 0))
dt = sh.Range("K1").Value
    For i = 1 To ct
        dt = dt + 1
        Sheets.Add After:=Sheets(Sheets.Count)
        With sh
            Intersect(.UsedRange, .Columns("T:AD")).Copy ActiveSheet.Range("A1")
        End With
        Sheets(Sheets.Count).Name = Format(dt, "DD-MM-YYYY") 'Not sure what you want here.
        Sheets(Sheets.Count).Coulumns("A:K").AutoFit
    Next
End Sub
 
Upvote 0
VBA Code:
Sub ExportEL()
Dim ct As Long, sh As Worksheet, dt As Date
Set sh = ActiveSheet
ct = Day(Application.EoMonth(sh.Range("AD1"), 0))
dt = sh.Range("K1").Value
    For i = 1 To ct
        dt = dt + 1
        Sheets.Add After:=Sheets(Sheets.Count)
        With sh
            Intersect(.UsedRange, .Columns("T:AH")).Copy ActiveSheet.Range("A1")
        End With
        Sheets(Sheets.Count).Name = Format(dt, "DD")
            Sheets("Ramp SUP").Columns("T:AD").Copy
            Range("A1").Select
           Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
        SkipBlanks:=False, Transpose:=False
        ActiveSheet.Columns("L:O").Hidden = True
    Next
End Sub

See above adjustments that I have made. AD1 still has the date 12/1/2020 when T:AH is copied to new sheet in A1 the date is in K1 on each new sheets however with my model it does not add the day to K1 of each sheet. It does name each sheet as 01,02,03,... etc up to the end of the month however in all sheets "02" to all last sheet "31" value in K1 is 12/1/2020. The value of K1 in sheet "02" should be 12/2/2020, Value in K1 in sheet "03" should be 12/3/2020..etc.
 
Upvote 0
Is the value in K1 a date data type or is it string data type.? If it is date data type, the code should be adding a day to each new sheet name. If it is formatted as text then it might not add the day.. But in my test, it definitely added a day to each sheet because the value in K1 was a date data type.
 
Upvote 0
New Exception Log Layout.xlsm
AD
112/1/2020
Ramp SUP


here is the format of that cell

1605648973861.png


Should it be something different?
 
Upvote 0

Forum statistics

Threads
1,215,066
Messages
6,122,948
Members
449,095
Latest member
nmaske

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