VBA: Copy Range of Cells until Null

hamistasty

Board Regular
Joined
May 17, 2011
Messages
208
I want to copy a list of cells from sheet "schedule", column A row 9 down until there is a null value, and then paste them into a new sheet "index".

Code:
Sheets("Index").Range("C13") = Sheets("Schedule").Range("A" & Rows.Count).End(xlUp).Row

the above code doesn't work and now I'm stuck.

tia
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Try this...

Code:
Sheets("Schedule").Range("A1:A" & Range("A" & Rows.Count).End(xlUp).Row).Copy Destination:=Sheets("Index").Range("C13")
 
Upvote 0
Code:
With Sheets("Schedule")
    .Range("A1", .Range("A" & Rows.Count).End(xlUp)).Copy Destination:=Sheets("Index").Range("C13")
End With
 
Upvote 0
Thanks, that works great. However, when I use:

Code:
With Sheets("Schedule")
       .Range("O9", .Range("O" & Rows.Count).End(xlUp)).Copy Destination:=Sheets("Index (2)").Range("G13")
End With

Sometimes there will be no value in cell O9, or there will be a break between null values (ie. O9 will have a value, O10 will not, then O11 will.)

So what I want it to do now, is refer to A9 down (until it ends) and just copy O9 to Ox with x being the count of A9 down.

any help?
 
Upvote 0
What am I doing wrong here wrt the above:

Code:
x = Application.WorksheetFunction.CountA(Sheets("Schedule").Range("A9:A" & Rows.Count))
 
With Sheets("Schedule")
    .Range("A9", .Range("A" & Rows.Count).End(xlUp)).Copy Destination:=Sheets("Index (2)").Range("C13")
    .Range("O9", .Range("O" & x)).Copy Destination:=Sheets("Index (2)").Range("G13")
End With
 
Upvote 0
<font face=Courier New><SPAN style="color:#00007F">With</SPAN> Sheets("Schedule")<br>    .Range("A9", .Range("A9").End(xlDown)).Offset(, 14).Copy Destination:=Sheets("Index (2)").Range("G13")<br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN></FONT>
 
Upvote 0
Thanks! perfect.

How do I make it so this works where G13 is a merged cell with H13 but O9 down is one column (ie single cells being copied over to a merged cell)

Code:
x = Application.WorksheetFunction.CountA(Sheets("Schedule").Range("A9:A" & Rows.Count))
    Dim myRow As Long, newRowCount As Long
    myRow = 13
    newRowCount = x - 1
    With Sheets("Index (2)").Rows(myRow)
        .Copy
        .Offset(1).Resize(newRowCount).Insert
    End With
With Sheets("Schedule")
    .Range("A9", .Range("A" & Rows.Count).End(xlUp)).Copy Destination:=Sheets("Index (2)").Range("C13")
    .Range("A9", .Range("A9").End(xlDown)).Offset(, 14).Copy Destination:=Sheets("Index (2)").Range("G13")
End With
 
Upvote 0
Merged cells are considered evil and vile. Use center across selection instead. This is a Horizontal cell formatting option.

Unmerge the two cells and use the code below.

<font face=Courier New>Sheets("Index (2)").Range("G13:H13").ClearContents<br><SPAN style="color:#00007F">With</SPAN> Sheets("Schedule")<br>    .Range("A9", .Range("A9").End(xlDown)).Offset(, 14).Copy Destination:=Sheets("Index (2)").Range("G13")<br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN><br>Sheets("Index (2)").Range("G13:H13").HorizontalAlignment = xlCenterAcrossSelection</FONT>
 
Upvote 0

Forum statistics

Threads
1,224,616
Messages
6,179,912
Members
452,949
Latest member
beartooth91

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