Select the used column range when in a Case Clause

MBM2016

New Member
Joined
Jun 3, 2015
Messages
6
Okay I am having a bit of a creativity/brain freeze issue here :stickouttounge: and cannot come up with a simple solution to really capture the used range on a column. At the moment I am selecting up to the next blank cell and I need to capture the used range.

I simply want to change the format for specific columns when a certain criteria is meet (always in the headers - row 1) - Any suggestions or solutions to my little dilemma here will be very much appreciated.

Code:
Sub DAh_ChangeToNumberFormat()


Application.DisplayAlerts = False
Application.ScreenUpdating = False


Dim rng As Range
Dim r As Range




Set rng = Range([a1], [a1].End(xlToRight))


ActiveSheet.UsedRange.NumberFormat = "@"


For Each r In rng.Cells
    Select Case r.value
    
    '> Number Format 1
    Case "Product", "Total", "Used", "Ship", "Supply", "Week", "Mgs"
        With Range(r, r.End(xlDown))
            .NumberFormat = "0.00"
            .value = .value
        End With
           
    '> Number Format 2
    Case "YOB"
        With Range(r, r.End(xlDown))
            .NumberFormat = "0000"
            .value = .value
        End With
        
    '> Number Format 3
    Case "ID"
        With Range(r, r.End(xlDown))
            .NumberFormat = "0"
            .value = .value
        End With
        
        
    End Select
Next


    
End Sub

If the entire column can be simply selected when a criteria is meet and change the formatting, this is more than okay too.


Thank you in advance!
 

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.
I'm no expert, but that code looks good to me. I made a little change and that did include selecting the entire column. One more variable, a little less code, but it does the same thing.

Code:
Sub DAh_ChangeToNumberFormat()
Application.DisplayAlerts = False
Application.ScreenUpdating = False

Dim rng As Range
Dim r As Range
[COLOR=#ff0000]Dim formatType As String[/COLOR]

Set rng = Range([a1], [a1].End(xlToRight))

ActiveSheet.UsedRange.NumberFormat = "@"

For Each r In rng.Cells
    Select Case r.Value
        '> Number Format 1
        Case "Product", "Total", "Used", "Ship", "Supply", "Week", "Mgs"
                [COLOR=#ff0000]formatType[/COLOR] = "0.00"
        '> Number Format 2
        Case "YOB"
                [COLOR=#ff0000]formatType [/COLOR]= "0000"
        '> Number Format 3
        Case "ID"
                [COLOR=#ff0000]formatType[/COLOR] = "0"
    End Select    
    [COLOR=#ff0000]r.EntireColumn.NumberFormat = formatType[/COLOR]
Next
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,214
Messages
6,170,771
Members
452,353
Latest member
strainu

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