VBA loops

tpthatsme

New Member
Joined
Jun 2, 2016
Messages
17
Hello all,

I have a macro that works, but I am trying to make it a little more efficient so that when changes happen, it will be easier to update in the future. The main idea is that I call for a file to open, do some stuff and then save and close it. The problem is that I have to do this with 16 files. I don't want to have 16 blocks of code if I can loop it with some small changes.

Here is an example of the file names:
Blue Hearts.csv
Blue Spades.csv
Blue Diamonds.csv
Blue Clubs.csv

Red Hearts.csv
Red Spades.csv
...and so on where the first word in the file name is a list like Blue, Red, Green, Yellow and the second word is a list like Hearts, Spades, Diamonds, Clubs.

Is there a way to do something like this?

For i= blue to yellow
For j= hearts to clubs
open file i+j.csv
do something great
save file i+j.csv
next
next

Above is just a way to explain what I am looking for, I know there is nothing correct about it as VBA.

Thanks!
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
this code shows you how to generate the file names:
Code:
Sub test()
 colr = Array("Blue", "Red", "Green", "Yellow")
 suit = Array("Clubs", "Diamonds", "Hearts", "Spades")
  For i = 0 To 3
   For j = 0 To 3
    fname = colr(i) & " " & suit(j) & ".csv"
    MsgBox fname
   Next j
  Next i


End Sub
 
Upvote 0

Forum statistics

Threads
1,224,821
Messages
6,181,163
Members
453,021
Latest member
Justyna P

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