counter

cpra3617

Board Regular
Joined
May 24, 2007
Messages
62
I would like a macro that, each time it is called, cuts the subsequent row (beginning at row 2) and inserts it into row 1.

So far I have:
Code:
Public zCount As Integer

Sub insertRow()
zCount = zCount + 1
Rows(zCount).Select
    Selection.Cut
    Rows("1:1").Select
    ActiveSheet.Paste
End Sub

I would like the counter to be reset the first time it encounters a blank row. So, if zCount =6 and row 6 is blank, I would like the zCount to be set to 1 again.
Can someone help with this?
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
maybe
Code:
If worksheetfunction.countblank(range(zcount & ":" & zcount)) = 256 then zcount = 1
 
Upvote 0
My first post was not accurate. What I really need this sub to do is to select only cells A thru I of the row in question and insert it into row A. So each time the sub was called it would insert the next row down into row A until it encountered a blank cell at which point the counter, zCount, would be set back to zero. 1st time it is called it would insert A thru I of row 2 above row 1, second time row 3 above row 1 etc.

Can someone help with the modification of the below code:

<code>
Public zCount As Integer

Sub insertRow()
zCount = zCount + 1
Rows(zCount).Select
Selection.Cut
Rows("1:1").Select
Selection.Insert Shift:=xlDown
End Sub
</code>
 
Upvote 0
Not sure how to make it not select the entire row, but to make it start over maybe something like:

<code>
Public zCount As Long

Sub insertRow()
Dim lastRow As Long
lastRow = Cells(Rows.Count, 1).End(xlUp).Row

If zCount <> lastRow Then
zCount = zCount + 1
Rows(zCount).Select
Selection.Cut
Rows("1:1").Select
Selection.Insert Shift:=xlDown

Else
zCount = 1
End If
End Sub
</code>
 
Upvote 0
Thanks.

This line
<code>
Selection.Insert Shift:=xlDown
</code>

gives an error saying the paste areas may be different. It works when I do it manually, but w/in the macro it fails. Why??
 
Upvote 0
yes, I get the same error now that I try it. It may be because "zCount" does not span an equivalent range as "1:1"??
 
Upvote 0
There is Absolutely a better way, but try:
<code>
Public zCount As Variant

Sub insertRow()
Dim lastRow As Variant
lastRow = Cells(Rows.Count, 1).End(xlUp).Row

Rows("1:1").Select
Selection.Insert Shift:=xlDown

If zCount <> lastRow Then
zCount = zCount + 1

Rows(zCount).Cut Destination:=Range("A1")
Rows(zCount).Delete

Else
zCount = 1
End If
End Sub
</code>
 
Upvote 0
Rows(zCount).Cut Destination:=Range("A1")

Isn't there a way to modify the above line to paste zCount into the row 1 and at the same time insert shift down so I don't overwrite what is currently in row 1?

Then I wouldn't have to use:
Rows("1:1").Insert Shift:=xlDown
 
Upvote 0

Forum statistics

Threads
1,214,561
Messages
6,120,245
Members
448,952
Latest member
kjurney

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