Loop in VBA to restart formula until there is no more names in a list

tcorcoran15

New Member
Joined
Feb 16, 2017
Messages
22
Hello,

I have a very basic VBA code like as follows:

Sub Hello()


'Removes any duplicates
Columns("A:A").Select
ActiveSheet.Range("$A$1:$A$27").RemoveDuplicates Columns:=1, Header:=xlNo

'Copies name from top of CE List into Input tab G12
Sheets("Names").Select
Range("A1").Select
Selection.Copy
Sheets("INPUT").Select
Range("G12").Select
ActiveSheet.Paste


'Copies Update File into Export File as paste value
Sheets("UPDATE FILE").Select
Range("A1:A9").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("EXPORT").Select
Range("A" & Rows.Count).End(xlUp).Offset(1).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False

'Deletes the first line

Sheets("Names").Select
Range("A1").Select
Selection.Delete Shift:=xlUp

End Sub

I have a list full of names in the 'Names' sheet and it then copies the name into the Input sheet and then paste copies the results into another tab called 'Export' at the next available blank cell. I have to keep pressing the button for each name but I would like to make it more automated so that when I press the macro button it does the step and then carries on doing it in a loop until all the names are gone? The cell either with a name in or blank will always be A1 in Names worksheet.

Thanks in advance for any help! :)
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
You can play around with this approach (disclaimer: I did not have a chance to test it)

Code:
Sub Hello()    
   Dim R As Range, Rng As Range

    'Removes any duplicates
    ActiveSheet.Range("$A$1:$A$27").RemoveDuplicates Columns:=1, Header:=xlNo

    With Sheets("Names")
        Set Rng = .Range("A1:A" & .Range("A" & .Rows.Count).End(xlUp).Row)
        For Each R In Rng
            'Copies name from top of CE List into Input tab G12
            R.Copy Sheets("INPUT").Range("G12")

            'Copies Update File into Export File as paste value
            Sheets("UPDATE FILE").Range("A1:A9").Copy
            With Sheets("EXPORT")
                .Range("A" & .Rows.Count).End(xlUp).Offset(1).PasteSpecial Paste:=xlPasteValues
            End With
        Next R

        'If you don't want to delete all the names, then comment out the next line
        Rng.EntireRow.Delete
    End With
End Sub

Btw, please use CODE tags when posting code to make it easier to read
attachment.php
 
Upvote 0

Forum statistics

Threads
1,213,546
Messages
6,114,253
Members
448,556
Latest member
peterhess2002

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