hard macro

lealeadupont

New Member
Joined
Feb 1, 2022
Messages
16
Office Version
  1. 365
Platform
  1. MacOS
can someone help me please ?

Write 2 Macros (not record): - The first one - name it “DeleteRows”. The Macro has to delete every second line/row. The original Table has 30 rows – after running the macro only 15 will remain. -
The second one – name it "MyCopy". The macro will copy from sheet Question4a, range A1:A30 into Question4b range A1:A30.

and the table is a list with phrases
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
Try...
VBA Code:
Sub DeleteRows()

    Dim LastRow As Long, wRng As Range, iRow As Long
    LastRow = Cells.Find("*", , xlValues, , xlByRows, xlPrevious).Row

    For iRow = LastRow To 1 Step -2
        If wRng Is Nothing Then
            Set wRng = Rows(iRow)
        Else
            Set wRng = Union(wRng, Rows(iRow))
        End If
    Next iRow

    If Not wRng Is Nothing Then
        wRng.Delete
    End If

End Sub


Sub MyCopy()
Sheets("Question4a").Range("A1:A30").Copy Sheets("Question4b").Range("A1")
End Sub
 
Upvote 0
Try...
VBA Code:
Sub DeleteRows()

    Dim LastRow As Long, wRng As Range, iRow As Long
    LastRow = Cells.Find("*", , xlValues, , xlByRows, xlPrevious).Row

    For iRow = LastRow To 1 Step -2
        If wRng Is Nothing Then
            Set wRng = Rows(iRow)
        Else
            Set wRng = Union(wRng, Rows(iRow))
        End If
    Next iRow

    If Not wRng Is Nothing Then
        wRng.Delete
    End If

End Sub


Sub MyCopy()
Sheets("Question4a").Range("A1:A30").Copy Sheets("Question4b").Range("A1")
End Sub
You're just amazing thank you sooo muchhh
 
Upvote 0
You're welcome. Thinking about it you are probably better off with the code below for the deletion

VBA Code:
Sub DeleteRows()

    Dim LastRow As Long, wRng As Range, iRow As Long
    LastRow = Cells.Find("*", , xlValues, , xlByRows, xlPrevious).Row

    For iRow = 2 To LastRow Step 2
        If wRng Is Nothing Then
            Set wRng = Rows(iRow)
        Else
            Set wRng = Union(wRng, Rows(iRow))
        End If
    Next iRow

    If Not wRng Is Nothing Then
        wRng.Delete
    End If

End Sub
 
Upvote 0

Forum statistics

Threads
1,214,983
Messages
6,122,591
Members
449,089
Latest member
Motoracer88

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