vba help - number format multiple columns

Mallesh23

Well-known Member
Joined
Feb 4, 2009
Messages
976
Office Version
  1. 2010
Platform
  1. Windows
Hi Team
Below code help me in converting numbers in text to Number format.

I want to pass Column no in this line:=> ws.Range("A:A,C:C,F:F,I:I,L:L")).Areas , what will be code?..


VBA Code:
Dim rA As Range
Dim ws As Worksheet
Set ws = ThisWorkbook.Worksheets("Sheet1")

For Each rA In Intersect(ws.UsedRange, ws.Range("A:A,C:C,F:F,I:I,L:L")).Areas
  With rA
    .NumberFormat = "General"
    .Value = .Value
Next rA
  End With


Thanks
mg
 
Last edited:

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
Hi Mallesh23,

Try this:

VBA Code:
Option Explicit
Sub Macro1()

    Dim rA As Range
    Dim ws As Worksheet
    
    Application.ScreenUpdating = False
    
    Set ws = ThisWorkbook.Worksheets("Sheet1")
    
    For Each rA In Intersect(ws.UsedRange, ws.Range("A:A,C:C,F:F,I:I,L:L")).Areas
        With rA
            .NumberFormat = "General"
            .Value = .Value
        End With
    Next rA
    
    Application.ScreenUpdating = True

End Sub

Regards,

Robert
 
Upvote 0
Hi Robert,

Thanks for your help, I want to pass Column no.

I want to replace this line :=> ws.Range("A:A,C:C,F:F,I:I,L:L")).Areas

Something like :=> ws.Columns(1,3,6,12)).Areas


Thanks
mg
 
Upvote 0
Maybe this (non-looping):

VBA Code:
Option Explicit
Sub Macro1()

    Dim ws As Worksheet
  
    Application.ScreenUpdating = False
  
    Set ws = ThisWorkbook.Worksheets("Sheet1")
  
    With Union(ws.Columns(1), ws.Columns(3), ws.Columns(6), ws.Columns(12))
        .NumberFormat = "General"
        .Value = .Value
    End With
  
    Application.ScreenUpdating = True

End Sub

Or based on your original code:

VBA Code:
Option Explicit
Sub Macro2()

    Dim rA As Range
    Dim ws As Worksheet
   
    Application.ScreenUpdating = False
   
    Set ws = ThisWorkbook.Worksheets("Sheet1")
   
    For Each rA In Intersect(ws.UsedRange, Union(ws.Columns(1), ws.Columns(3), ws.Columns(6), ws.Columns(12))).Areas
        With rA
            .NumberFormat = "General"
            .Value = .Value
        End With
    Next rA
   
    Application.ScreenUpdating = True

End Sub
 
Upvote 0
Hi Trebor,

Thanks for your help, it worked, I modified small things as per my requirements.
One more help required , Creating a function which will convert text to number for single column and multiple column.
as per Parameter passed.

Below is my attempted code. It converts Multiple Columns how to pass single column.

VBA Code:
Sub test()

Dim Multi_Column As Variant
Multi_Column = Array(1, 3, 5)

Dim ws As Worksheet
Application.ScreenUpdating = False
Set ws = ThisWorkbook.Worksheets("Sheet1")

formatText ws, Multi_Column

End Sub


VBA Code:
Sub formatText(ByVal ws As Worksheet, Optional ByVal Multi_Column As Variant)

    Dim rA As Range
    Dim i As Long
   
    For i = LBound(Multi_Column) To UBound(Multi_Column)
        For Each rA In Intersect(ws.UsedRange, ws.Columns(Multi_Column(i))).Areas
            With rA
                .NumberFormat = "General"
                .Value = .Value
            End With
        Next rA
   Next i
    Application.ScreenUpdating = True

MsgBox "Macro successful"


End Sub
 
Upvote 0
Hi Trebor76,

Perfect ! Thanks for your help, it worked ! ?


Thanks
mg
 
Upvote 0

Forum statistics

Threads
1,214,919
Messages
6,122,260
Members
449,075
Latest member
staticfluids

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