How can I alter this working code to allow user to select multiple columns?

Mr Denove

Active Member
Joined
Jun 8, 2007
Messages
435
Code:
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")
   mySheets = Array(1, 3)
   Set wsO = Worksheets(mySheets(iws))
   myColumns = Array("Deptoff", "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
                        
            For Each xCell In wsO.Cells(1, myPosition).Range("A2:A" & LR)
                xCell.Value = CDec(xCell.Value)
                xCell.NumberFormat = "0"
            Next xCell
            myPosition = 0
         End If

      Next i
    
    Next iws

  


   Set wsO = Nothing
   Application.ScreenUpdating = True
   
End Sub

I was able to create this working code and would now like to make it more adapatable by prompting the user to click on a column (or columns) to allow the number formatting to be done by VBA.
I am not able to create a personal workbook (at work) and therefore was thinking along the lines of paste the worksheet to be formatted into Sheet 2 then run the code.

Im struggling to find code that allows multiple selections to be selected by the user.

Any help or direction as always greatly appreciated.

Thanks in advance.

PS This code works a treat so please feel free to use and abuse at your leisure.
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,168,029
Messages
5,856,933
Members
431,840
Latest member
simplysoph

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
Top