Please help to change code?

Andries

Board Regular
Joined
Feb 3, 2011
Messages
127
Hi there

How can I change this code so that it would not keep on going to Sheets("DAY 2"). but rather look at the first or next available blank sheet.


Sheets("POPULATE SEATS").Select
Cells.Select
Selection.Copy
Sheets("DAY 2").Select
Cells.Select
ActiveSheet.Paste
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
This one adds a new sheet + copies the sheet "POPULATE SEATS" every time you run the macro:
Code:
Sub AddSheet()
Dim WS As Worksheet
Set WS = Sheets.Add 'Adds a new worksheet
With WS
    .Name = "Day " & Worksheets.Count  'Names the new worksheet
    Sheets("POPULATE SEATS").Cells.Copy .Cells(1, 1)    'Copies the everything from "POPULATE SEATS"
End With
End Sub
 
Upvote 0
Hi there

How can I change this code so that it would not keep on going to Sheets("DAY 2"). but rather look at the first or next available blank sheet.


Sheets("POPULATE SEATS").Select
Cells.Select
Selection.Copy
Sheets("DAY 2").Select
Cells.Select
ActiveSheet.Paste

You could maybe paste the data to a new sheet

Code:
Sub test()
    Dim fs, ts
    Application.ScreenUpdating = False
    Set fs = Sheets("POPULATE SEATS")
    Set ts = Sheets.Add(after:=fs)
    fs.Cells.Copy ts.Cells
End Sub
 
Upvote 0
Hi Misca

Thanks for your help. I also need to insert these 2 ranges down below in the code you gave me.

Sheets("data2").Range("A2:S2")
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy

Sheets("data2").Select
Range("DT3:EG3").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
 
Upvote 0
Hi
I also need to insert these 2 ranges down below in the code at the bottom. I forgot to ask it in my first posting.


Sheets("data2").Range("A2:S2")
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy

Sheets("data2").Select
Range("DT3:EG3").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy[/QUOTE]

Sub AddSheet()
Dim WS As Worksheet
Set WS = Sheets.Add 'Adds a new worksheet
With WS
.Name = "Day " & Worksheets.Count 'Names the new worksheet
Sheets("POPULATE SEATS").Cells.Copy .Cells(1, 1) 'Copies the everything from "POPULATE SEATS"
End With
End Sub
 
Upvote 0
Where do you want to paste the two additional ranges? Starting from the first empty cell in Columns A & DT after all the data on the new sheet?

And what happens if the first empty cells are on different rows? Should the code copy them to the same row anyway? And is there supposed to be same number of rows in both ranges?
 
Upvote 0
Hi

Sorry I left it out.

Sheets("data2").Range("A2:S2") this must be copied to the new sheet that was created and paste in .Range("DA:DS")

and

Sheets("data2").Range("DT3:EG3") this must also be copied to the new sheet that was created and paste in .Range("DT:EG")

And what happens if the first empty cells are on different rows?
the data always starts in the same row and cell

Should the code copy them to the same row anyway?
Yes please

And is there supposed to be same number of rows in both ranges?
the number of rows are always variable
 
Upvote 0
Try this. It should be quite easy to modify if it doesn't do exactly what you're after:
Code:
Sub AddSheet()

Dim WS As Worksheet
Dim R As Long
Dim i As Integer

Set WS = Sheets.Add 'Adds a new worksheet


With WS
    .Name = "Day " & Worksheets.Count  'Names the new worksheet
    Sheets("POPULATE SEATS").Cells.Copy .Cells(1, 1)    'Copies the everything from "POPULATE SEATS"

    For i = 1 To 2  'Copies the additional ranges from Data2:
        R = Sheets("Data2").Range(Choose(i, "A", "DT") & .Rows.Count).End(xlUp).Row 'The rows to copy (from the bottom up)
        Sheets("Data2").Range(Choose(i, "A2:S", "DT3:EG") & R).Copy .Range(Choose(i, "DA1", "DT1"))
    Next i

End With

End Sub
 
Upvote 0

Forum statistics

Threads
1,224,586
Messages
6,179,723
Members
452,939
Latest member
WCrawford

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