Help with macro to loop through cells and save a file

The Ruff Report

New Member
Joined
Jun 17, 2023
Messages
21
Office Version
  1. 365
Platform
  1. Windows
It's been awhile since I've created this macro and I'm having trouble.

I have a workbook "Sales" with a worksheet "Names" that has 100 names starting in cell A1. I want to copy the name in A1, paste into a new workbook (it updates a bunch of figures), and save that workbook under that name. I have the code for that fine.

I'm having trouble returning to the workbook "Sales" and the worksheet "Names" and performing the same operation for the name in A2, A3, A4.....until I reach A100

Any help is appreciated.
 

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.
I actually found an older file with my macro:

Dim X As Integer
For X = 1 To 100

Sheets("Names").Select
Range("A" & X).Select
Selection.Copy

(rest of macro)

Next X
 
Upvote 0
Solution
Hi there,

There is rarely a need to use select when working data. Try this where the values are assigned to an array and then the code loops through each item in it:

Option Explicit
VBA Code:
Sub TheRuffReport()

    Dim arrMyData As Variant, varItem As Variant
    
    Application.ScreenUpdating = False
    
    arrMyData = ThisWorkbook.Sheets("Names").Range("A2:A100")
    
    For Each varItem In arrMyData
        'Your cuurent macro here where 'varItem' needs to go into cell A1 of the workbook where it will be saved as 'varItem'
    Next varItem
    
    Application.ScreenUpdating = False

End Sub

Regards,

Robert
 
Upvote 0
I'm not great at VBA but good enough to significantly improve some processes at work. I'm discovering how useless select is in all my macros. Duh!
 
Upvote 0

Forum statistics

Threads
1,224,809
Messages
6,181,076
Members
453,020
Latest member
mattg2448

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