Array for Multiple Sheets??

Mr Denove

Active Member
Joined
Jun 8, 2007
Messages
446
Code:
Sub Macro9()
Dim wsO As Worksheet
Dim LR As Long
Dim i As Integer
Dim iws As Integer
Dim myPosition As Long

Application.ScreenUpdating = False

mySheets = Array(Sheet1, Sheet3)

For iws = 0 To UBound(mySheets) - 1

myColumns = Array("TransactionID", "order_id", "account", "amount", "CurrencyAmount", "SupplierID", "UNSPCLV1", "UNSPCLV2", "UNSPCLV3", "UNSPCLV4")
LR = Range("A" & Rows.Count).End(xlUp).Row

    For i = 0 To UBound(myColumns) - 1
        
        myPosition = WorksheetFunction.Match(myColumns(i), wsO.Range("A1:AC1"), 0)
               
        wsO.Cells(1, myPosition).Range("A2:A" & LR).Select

        Selection.NumberFormat = "0"
For Each xCell In Selection
    xCell.Value = CDec(xCell.Value)
 Next xCell
    
    Next i
    
    Next iws
    
Set wsO = Nothing
Application.ScreenUpdating = True

MsgBox "Complete"
End Sub

Im driving myself mad now, the array for the columns was compiling, but as soon as I add in the Array to use multiple sheets it all goes horribly wrong.
And I am not sure what I am doing that is causing the problem.
This is the first of several modules I need to create for a data tidy up and would appreciate if someone can point out the errors of my way.
Thanks in advance.
Stuart
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
At first glance it doesn't look like you have set up the worksheet you are working with. Try
Rich (BB code):
   For iws = 0 To UBound(mySheets) - 1
      Set wsO = mySheets(iws)
      LR = wsO.Range("A" & Rows.Count).End(xlUp).Row

[edit] Or as Charlie says.
 
Last edited:
Upvote 0
Hi both,

Thanks for the replies.
Code now compiles and formats the Sheet1 as expected but although I can see the wsO in the Watch Window move to Sheet3 its and compiles the code its not actually formatting anything.
 
Upvote 0
THis bits weird

wsO.Cells(1, myPosition).Range("A2:A" & LR).Select


try

wsO.Range(wsO.Cells(1,myPosition), wsO.cells(LR,myPosition)).Select
 
Upvote 0
Hi Charles,

Unfortunately I cant test your new line of data as the code is crashing on
Code:
Set wsO = Sheets(mySheets(iws))

It seems so close.
 
Upvote 0
If you place Option Explicit at the top of the code module this will force you to declare all your variables. I cannot emphasise enough how helpful this will be in debugging your code.
Rich (BB code):
Option Explicit
See here on how to make this the default setting: Declaring Variables

Another debugging technique is to step through your code one line at a time by pressing F8.

I have made some amendment to your code. I have only tested it on column header = "order_id".

Always place your declaration at the top of the procedure. Giving your variable meaningful names helps make your code more legible. Failing that, provide a description of what the variable is for, e.g.,

Rich (BB code):
Sub Macro9()
   Dim wsO As Worksheet
   Dim LR As Long
   Dim i As Integer
   Dim iws As Integer
   Dim myPosition As Long
   Dim mySheets As Variant
   Dim myColumns As Variant
   Dim xCell As Range

You only need to set the arrays once, so place them outside the loop. To overcome the problem you are having I have placed quotations around the sheet names. Edit if needed.
Rich (BB code):
   mySheets = Array("Sheet1", "Sheet3")
   myColumns = Array("TransactionID", "order_id", "account", _
                     "amount", "CurrencyAmount", "SupplierID", _
                     "UNSPCLV1", "UNSPCLV2", "UNSPCLV3", "UNSPCLV4")

Always comment your code.
The first loop processes the worksheet array
Rich (BB code):
   'process worksheet array
   For iws = LBound(mySheets) To UBound(mySheets)
      Set wsO = Sheets(mySheets(iws))
      LR = wsO.Range("A" & Rows.Count).End(xlUp).Row
      wsO.Range("A2:A" & LR).NumberFormat = "0"

The inner loop processes the column array:
Rich (BB code):
      'process column header array
      For i = LBound(myColumns) To UBound(myColumns)

I have used LBound and UBound, i.e., lowerBound and upperBound, to avoid having to worry about manual configuration which may result in overflow errors

You are using Worksheet.Function.Match to determine which column you are working with.
The code may not find a match, so we need an error trap.
Rich (BB code):
         'set an error trap in case you don't find the header
         On Error Resume Next
         myPosition = WorksheetFunction.Match(myColumns(i), wsO.Range("A1:AC1"), 0)

We check a match has been found.
Set the format and convert any value
Reset the position marker.
Rich (BB code):
         'check the column header was found
         If myPosition <> 0 Then
            wsO.Cells(1, myPosition).NumberFormat = "0"
            
            For Each xCell In wsO.Cells(1, myPosition).Range("A2:A" & LR)
                xCell.Value = CDec(xCell.Value)
            Next xCell
            myPosition = 0
         End If


I we put all that together we have:
Rich (BB code):
Option Explicit


Sub Macro9()
   Dim wsO As Worksheet
   Dim LR As Long          'last row in column A of the worksheet
   Dim i As Integer        'loop index
   Dim iws As Integer      'worksheet counter
   Dim myPosition As Long
   Dim mySheets As Variant
   Dim myColumns As Variant
   Dim xCell As Range
   
   mySheets = Array("Sheet1", "Sheet3")
   myColumns = Array("TransactionID", "order_id", "account", _
                     "amount", "CurrencyAmount", "SupplierID", _
                     "UNSPCLV1", "UNSPCLV2", "UNSPCLV3", "UNSPCLV4")


   Application.ScreenUpdating = False
   
   'process worksheet array
   For iws = LBound(mySheets) To UBound(mySheets)
      Set wsO = Sheets(mySheets(iws))
      LR = wsO.Range("A" & Rows.Count).End(xlUp).Row
      wsO.Range("A2:A" & LR).NumberFormat = "0"
      
      'process column header array
      For i = LBound(myColumns) To UBound(myColumns)
         
         'set an error trap in case you don't find the header
         On Error Resume Next
         myPosition = WorksheetFunction.Match(myColumns(i), wsO.Range("A1:AC1"), 0)
                
         'check the column header was found
         If myPosition <> 0 Then
            wsO.Cells(1, myPosition).NumberFormat = "0"
            
            For Each xCell In wsO.Cells(1, myPosition).Range("A2:A" & LR)
                xCell.Value = CDec(xCell.Value)
            Next xCell
            myPosition = 0
         End If


      Next i
    
    Next iws


   MsgBox "Complete"


   Set wsO = Nothing
   Application.ScreenUpdating = True
End Sub

As I said, I have only tested this on column header = "order_id".
While testing press F8 to step through the code.
Use Alt+Tab to "flick" between the vba editor and Excel.

Hope this helps,
Bertie
 
Upvote 0
Berie that is awesome thank you for the time and effort and for making it so much clearer to understand each area etc
Much appreciated.

Wee problem though I cant get it to compile beyond
Code:
  Set wsO = Sheets(mySheets(iws))

I will be saving all your info for future reference though.
Stuart
 
Upvote 0
Did you place double quotation around the names in the sheet array?
Do sheets of with these names exist in your workbook?
Rich (BB code):
   mySheets = Array("Sheet1", "Sheet3")

If you want to refer to the first and third worksheets, irrespective of name, you could use:

Rich (BB code):
   'mySheets = Array("Sheet1", "Sheet3")
   mySheets = Array(1, 3)

Then when setting it up

Rich (BB code):
      'Set wsO = Sheets(mySheets(iws))
      Set wsO = Worksheets(mySheets(iws))

In order to define the sheet we are working with we either need the name, or the index position in the tab order.
 
Upvote 0

Forum statistics

Threads
1,214,522
Messages
6,120,019
Members
448,938
Latest member
Aaliya13

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