Macro skipping third row

Foxvh

New Member
Joined
Jul 9, 2020
Messages
14
Office Version
  1. 2010
Platform
  1. Windows
Hi the code below does exactly what i want it to do except it always skips the third row for some reason.

VBA Code:
    Private Sub CommandButton1_Click()
        Dim c As Range
    Dim LastRow As Long
    LastRow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row
        For Each c In ActiveSheet.Range("A1", "A" & LastRow)
            If c.Value <> "" Then
                c.Offset(0, 0).Select
                    Selection.Resize(Selection.Rows.Count, Selection.Columns.Count + 14).Cut _
                        Destination:=Worksheets("Pivot Data").Cells(Rows.Count, 1).End(xlUp).Offset(1, 0)
            End If
        Next c
    End Sub
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
In your Destination you use Rows.Count, but because the ActiveSheet is not Pivot Data, that is the number of rows in the ActiveSheet. So you are possibly overwriting one row's data with another. Try this edit of your code, and see if it produces the desired result. (Always use a copy of of your workbook, just in case.)
VBA Code:
Private Sub CommandButton1_Click()
    Dim c As Range
    Dim LastRow As Long
    LastRow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row
    For Each c In ActiveSheet.Range("A1", "A" & LastRow)
        If c.Value <> "" Then
            With Worksheets("Pivot Data")
                c.Resize(, 15).Cut Destination:=.Cells(.Rows.Count, 1).End(xlUp).Offset(1, 0)
            End With
        End If
    Next c
End Sub
 
Upvote 0
How about
VBA Code:
Private Sub CommandButton1_Click()
   Dim c As Range
   Dim LastRow As Long
   LastRow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row
   For Each c In ActiveSheet.Range("A1:A" & LastRow)
      If c.Value <> "" Then
         c.Resize(, 15).Copy = Worksheets("Pivot Data").Cells(Rows.Count, 1).End(xlUp).Offset(1, 0)
         c.Resize(, 15).Clear
      End If
   Next c
End Sub
 
Upvote 0
Sorry i should have added a bit more detail.
by skipping the third row i mean when i run the code it cut&pastes row 2,4,5,6+ and for some reason it skips over the 3rd row in the activesheet when cutting the data.

1595505038709.png


Hopefully that image helps.
 
Upvote 0
Have you tried, either of the codes provided?
 
Upvote 0
I tried both the first code still leaves the third row (i change the For Each Line to "A2' as i have headers for that worksheet.
the second code has an (Object Required) on the below line
(c.Resize(, 15).Copy = Worksheets("Pivot Data").Cells(Rows.Count, 1).End(xlUp).Offset(1, 0)
 
Upvote 0
Oops, on my code, you need to remove the = sign on the line that fails.
 
Upvote 0
Did you copy the code, or re-type it? Use the copy icon in the top-right of the post to copy the code, then paste it into the appropriate place. Otherwise, you might miss typing something.
 
Upvote 0
Thanks Fluff your code works perfectly for what i need.
 
Upvote 0

Forum statistics

Threads
1,215,494
Messages
6,125,139
Members
449,207
Latest member
VictorSiwiide

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