Macro help

Jemma Atkinson

Well-known Member
Joined
Jul 7, 2008
Messages
509
Hi, i need a macro that will copy pfolio and Totals <>0 to sheet2 range A12, (i dont require the headers to be copied), the totals column is not always in Column B it varies, so the macro needs to factor this in. When copying the totals over i need the code to round the totals to 2dp, if there is a 0.00 in sheet 2 then clear cell and move one cell up.

Excel Workbook
AB
9pfolioTotal
10KFGEEE34
11SDFSFF0
12ERWER15
13REGERG0
Sheet1



Excel Workbook
AB
11pfolioTotal
12KFGEEE34
13ERWER15
Sheet2
 
Hey Mark, i think there is With missing as i will be running this macro from Sheet 2


Code:
Sub MoveRowsToNewSheet()
'========================================================================
' COPIES ALL ROWS WHERE COLUMN WITH "Total" IN ROW 9 HAS VALUE NOT EQUAL TO 0
' FROM SHEET1 to A12 AND BELOW ON SHEET2
'========================================================================
    Dim LRMain As Long, LRNew As Long, i As Long, TotalCol As Long
 
    LRMain = Worksheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Row
    TotalCol = Range("A9:AA9").Find("Total", Range("a9"), xlValues, xlWhole, xlByColumns, xlNext).Column
    With Worksheets("Sheet1")
        For i = 10 To LRMain Step 1
            Fred = Cells(i, TotalCol).Value
            If Cells(i, TotalCol).Value <> 0 Then
                LRNew = Worksheets("Sheet2").Range("A" & Rows.Count).End(xlUp).Row
                If LRNew < 12 Then LRNew = 12
                Cells(i, 1).Copy Worksheets("Sheet2").Range("A" & LRNew + 1)
                Cells(i, TotalCol).Copy Worksheets("Sheet2").Range("b" & LRNew + 1)
            End If
        Next i
    End With
End Sub

The code above should now do what you want, I think

Please let me know how you get on

Mark:)
 
Upvote 0

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
Try
Code:
Sub MoveRowsToNewSheet()
'========================================================================
' COPIES ALL ROWS WHERE COLUMN WITH "Total" IN ROW 9 HAS VALUE NOT EQUAL TO 0
' FROM SHEET1 to A12 AND BELOW ON SHEET2
'========================================================================
    Dim LRMain As Long, LRNew As Long, i As Long, TotalCol As Long
    Worksheets("Sheet1").Select
    LRMain = Worksheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Row
    TotalCol = Worksheets("Sheet1").Range("A9:AA9").Find("Total", Range("a9"), xlValues, xlWhole, xlByColumns, xlNext).Column
    With Worksheets("Sheet1")
        For i = 10 To LRMain Step 1
            If Cells(i, TotalCol).Value <> 0 Then
                LRNew = Worksheets("Sheet2").Range("A" & Rows.Count).End(xlUp).Row
                If LRNew < 12 Then LRNew = 12
                Cells(i, 1).Copy Worksheets("Sheet2").Range("A" & LRNew + 1)
                Cells(i, TotalCol).Copy Worksheets("Sheet2").Range("b" & LRNew + 1)
            End If
        Next i
    End With
    Worksheets("Sheet2").Select
    Range("A" & Rows.Count).End(xlUp).Select
End Sub
 
Upvote 0
Thanks Mark, works like a charm :)

Try
Code:
Sub MoveRowsToNewSheet()
'========================================================================
' COPIES ALL ROWS WHERE COLUMN WITH "Total" IN ROW 9 HAS VALUE NOT EQUAL TO 0
' FROM SHEET1 to A12 AND BELOW ON SHEET2
'========================================================================
    Dim LRMain As Long, LRNew As Long, i As Long, TotalCol As Long
    Worksheets("Sheet1").Select
    LRMain = Worksheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Row
    TotalCol = Worksheets("Sheet1").Range("A9:AA9").Find("Total", Range("a9"), xlValues, xlWhole, xlByColumns, xlNext).Column
    With Worksheets("Sheet1")
        For i = 10 To LRMain Step 1
            If Cells(i, TotalCol).Value <> 0 Then
                LRNew = Worksheets("Sheet2").Range("A" & Rows.Count).End(xlUp).Row
                If LRNew < 12 Then LRNew = 12
                Cells(i, 1).Copy Worksheets("Sheet2").Range("A" & LRNew + 1)
                Cells(i, TotalCol).Copy Worksheets("Sheet2").Range("b" & LRNew + 1)
            End If
        Next i
    End With
    Worksheets("Sheet2").Select
    Range("A" & Rows.Count).End(xlUp).Select
End Sub
 
Upvote 0
My pleasure!:)

Little bit more of a challenge than I anticipated as to start with you hadn't mentioned specifically and I hadn't noticed about the importance as it turned out of "Total" being in row 9

I also hadn't considered that you were going to run it from Sheet2

Worth then considering being very, very detailed and specific about what you are hoping for (bullet points?), as it does make it easier to assess whether you have the skills to help. If I am honest I wasn't sure I did have in this case - I am by no means an expert!

Still. Got there in the end! And very good learning for me!

All the best

Mark:)
 
Upvote 0
Thanks Mark, just one last thing, in your code how can we paste special values so when the code copies to Sheet 2 the formulas can be copied over.


My pleasure!:)

Little bit more of a challenge than I anticipated as to start with you hadn't mentioned specifically and I hadn't noticed about the importance as it turned out of "Total" being in row 9

I also hadn't considered that you were going to run it from Sheet2

Worth then considering being very, very detailed and specific about what you are hoping for (bullet points?), as it does make it easier to assess whether you have the skills to help. If I am honest I wasn't sure I did have in this case - I am by no means an expert!

Still. Got there in the end! And very good learning for me!

All the best

Mark:)
 
Upvote 0
Code:
Sub MoveRowsToNewSheet()
'========================================================================
' COPIES ALL ROWS WHERE COLUMN WITH "Total" IN ROW 9 HAS VALUE NOT EQUAL TO 0
' FROM SHEET1 to A12 AND BELOW ON SHEET2
'========================================================================
    Dim LRMain As Long, LRNew As Long, i As Long, TotalCol As Long
    Worksheets("Sheet1").Select
    LRMain = Worksheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Row
    TotalCol = Worksheets("Sheet1").Range("A9:AA9").Find("Total", Range("a9"), xlValues, xlWhole, xlByColumns, xlNext).Column
    With Worksheets("Sheet1")
        For i = 10 To LRMain Step 1
            If Cells(i, TotalCol).Value <> 0 Then
                LRNew = Worksheets("Sheet2").Range("A" & Rows.Count).End(xlUp).Row
                If LRNew < 12 Then LRNew = 12
    'Cells(i, 1).Copy Worksheets("Sheet2").Range("A" & LRNew + 1)
                Cells(i, 1).Copy
                Sheets("Sheet2").Range("A" & LRNew + 1).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
                                                                                                              :=False, Transpose:=False
    'Cells(i, TotalCol).Copy Worksheets("Sheet2").Range("b" & LRNew + 1)
                Cells(i, TotalCol).Copy
                Sheets("Sheet2").Range("b" & LRNew + 1).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
                                                                                                              :=False, Transpose:=False
            End If
        Next i
    End With
    Worksheets("Sheet2").Select
    Range("A" & Rows.Count).End(xlUp).Select
End Sub

Any good??

Mark:)
 
Upvote 0
Thanks Mark, this works.

Code:
Sub MoveRowsToNewSheet()
'========================================================================
' COPIES ALL ROWS WHERE COLUMN WITH "Total" IN ROW 9 HAS VALUE NOT EQUAL TO 0
' FROM SHEET1 to A12 AND BELOW ON SHEET2
'========================================================================
    Dim LRMain As Long, LRNew As Long, i As Long, TotalCol As Long
    Worksheets("Sheet1").Select
    LRMain = Worksheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Row
    TotalCol = Worksheets("Sheet1").Range("A9:AA9").Find("Total", Range("a9"), xlValues, xlWhole, xlByColumns, xlNext).Column
    With Worksheets("Sheet1")
        For i = 10 To LRMain Step 1
            If Cells(i, TotalCol).Value <> 0 Then
                LRNew = Worksheets("Sheet2").Range("A" & Rows.Count).End(xlUp).Row
                If LRNew < 12 Then LRNew = 12
    'Cells(i, 1).Copy Worksheets("Sheet2").Range("A" & LRNew + 1)
                Cells(i, 1).Copy
                Sheets("Sheet2").Range("A" & LRNew + 1).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
                                                                                                              :=False, Transpose:=False
    'Cells(i, TotalCol).Copy Worksheets("Sheet2").Range("b" & LRNew + 1)
                Cells(i, TotalCol).Copy
                Sheets("Sheet2").Range("b" & LRNew + 1).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
                                                                                                              :=False, Transpose:=False
            End If
        Next i
    End With
    Worksheets("Sheet2").Select
    Range("A" & Rows.Count).End(xlUp).Select
End Sub

Any good??

Mark:)
 
Upvote 0

Forum statistics

Threads
1,215,064
Messages
6,122,937
Members
449,094
Latest member
teemeren

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