problem with macro

paulsolar

Well-known Member
Joined
Aug 21, 2013
Messages
682
Office Version
  1. 365
Hi All

I'm having a strange problem with the below macro. The macro is an adaptation of a macro I found on this site created by Fluff


It has two problems
1
It is copying and pasting the wrong rows from from sheet1 to sheet2. It is copying rows where there is a 0 rather than 1 or over to sheet2 (what is stranger it is copying the correct number of rows and the ones it is copying are the same number of rows apart.
2
If I copy B:D it it actually copies C:E (hence in the code I have used A:C and this gives me B:D which is what I require)

Any ideas where i Have gone wrong??

Kind regards

Paul

VBA Code:
Sub cleanup()
Dim rng As Range
Dim cell As Range
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Set ws1 = Sheet1
Set ws2 = Sheet2

Set rng = ws1.Range("B3:B49")

For Each cell In rng
    If cell.Value >= 1 Then
        cell.Range("A" & ActiveCell.Row & ":C" & ActiveCell.Row).Copy
        If ws2.Range("B16").Value = "" Then
            ws2.Range("B16").PasteSpecial xlPasteValues
        Else
            ws2.Range("B" & Rows.Count).End(xlUp).Offset(1).PasteSpecial xlPasteValues
        End If
    End If
Next cell

End Sub
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
try this:
VBA Code:
Sub cleanup()
Dim rng As Range
Dim cell As Range
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Set ws1 = Sheet1
Set ws2 = Sheet2

Set rng = ws1.Range("B3:B49")

For Each cell In rng
    If cell.Value >= 1 Then
        cell.Range("A" & cell.Row & ":C" & cell.Row).Copy
        If ws2.Range("B16").Value = "" Then
            ws2.Range("B16").PasteSpecial xlPasteValues
        Else
            ws2.Range("B" & Rows.Count).End(xlUp).Offset(1).PasteSpecial xlPasteValues
        End If
    End If
Next cell

End Sub
 
Upvote 0
try this:
VBA Code:
Sub cleanup()
Dim rng As Range
Dim cell As Range
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Set ws1 = Sheet1
Set ws2 = Sheet2

Set rng = ws1.Range("B3:B49")

For Each cell In rng
    If cell.Value >= 1 Then
        cell.Range("A" & cell.Row & ":C" & cell.Row).Copy
        If ws2.Range("B16").Value = "" Then
            ws2.Range("B16").PasteSpecial xlPasteValues
        Else
            ws2.Range("B" & Rows.Count).End(xlUp).Offset(1).PasteSpecial xlPasteValues
        End If
    End If
Next cell

End Sub
Hi

Many thanks for your reply

I'm trying your reply and it seems to be copying rows not even related to the range now?

I cant seem to get my head around the at all now

cheers

Paul
 
Upvote 0
Hello Paul, how about
VBA Code:
Sub cleanup()
Dim rng As Range
Dim cell As Range
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Set ws1 = Sheet1
Set ws2 = Sheet2

Set rng = ws1.Range("B3:B49")

For Each cell In rng
    If cell.Value >= 1 Then
        cell.Resize(, 3).Copy
        If ws2.Range("B16").Value = "" Then
            ws2.Range("B16").PasteSpecial xlPasteValues
        Else
            ws2.Range("B" & Rows.Count).End(xlUp).Offset(1).PasteSpecial xlPasteValues
        End If
    End If
Next cell

End Sub

PS, Good wins for us both at the weekend :) ?
 
Upvote 0
Hello Paul, how about
VBA Code:
Sub cleanup()
Dim rng As Range
Dim cell As Range
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Set ws1 = Sheet1
Set ws2 = Sheet2

Set rng = ws1.Range("B3:B49")

For Each cell In rng
    If cell.Value >= 1 Then
        cell.Resize(, 3).Copy
        If ws2.Range("B16").Value = "" Then
            ws2.Range("B16").PasteSpecial xlPasteValues
        Else
            ws2.Range("B" & Rows.Count).End(xlUp).Offset(1).PasteSpecial xlPasteValues
        End If
    End If
Next cell

End Sub

PS, Good wins for us both at the weekend :) ?
Hi Fluff

As always worked perfectly :love:

I based my code on some code you had written previously but couldn't get it right.

England were superb I hate to say, we just squeezed past a poor Australian team. I think I will throw away my TV for the six nations and cut off any access to the internet, wooden spoon here we come, we'll perhaps not we may beat Italy :eek:
 
Upvote 0
Glad we could help & thanks for the feedback.

I think I will throw away my TV for the six nations
I wouldn't go that far, Wales have a habit of raising their. Especially when they play us.
 
Upvote 0

Forum statistics

Threads
1,215,012
Messages
6,122,682
Members
449,091
Latest member
peppernaut

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