How to Run Code only for Selected Columns

EngSantiago

New Member
Joined
Mar 19, 2014
Messages
17
Office Version
  1. 365
Platform
  1. Windows
Hi all,

I currently have a vba macro that creates individual report for based on the data on each column. The macro will run until cell (1,i) is empty.
Code:
Do While Workbooks("Database.xlsm").Sheets("Assemblies_List").Cells(1, i) <> ""

However, I don't always need all of the reports. Therefore, I would like to modify the above code to run the code only for a selected group columns. I tried hiding the some columns but that doesn't work :). What would be the easiest way to accomplish this? Can someone point me into the right direction?

Thanks!
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
Something along the lines of
Code:
   Dim Ary As Variant
   Dim i As Long
   Ary = Array(1, 3, 4, 5, 9, 11)
   For i = 0 To UBound(Ary)
      Workbooks("Database.xlsm").Sheets("Assemblies_List").Cells(1, Ary(i))
      
   Next i
 
Upvote 0
Maybe I can insert a new row on top and use the row as a yes/no (Y/N) field, but again how to I change the code to create the reports only for those columns with a "Y"?
 
Upvote 0
Did you see my suggestion?
 
Upvote 0
Well, I was really unsure how to use your code so I decided to try something else first and actually worked. Basically, I added new top row with Y/N values and then added an If statement after the Do While statement as shown below. This way I can select the reports I want bby changin the cell value to Y, and it will skip columns set to No.
Code:
[COLOR=#333333]Do While Workbooks("Database.xlsm").Sheets("Assemblies_List").Cells(1, i) <> ""
[/COLOR]If Workbooks("Database.xlsm").Sheets("Assemblies_List").Cells(1, i) = "Y" Then
[COLOR=#333333]*here I kept the rest of the code*
End If
[/COLOR]

Hope this helps someone else.
 
Upvote 0
Glad you figured it out & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,215,693
Messages
6,126,240
Members
449,304
Latest member
hagia_sofia

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