For-Next loop is not working for multi-cell selection

mdd16

Board Regular
Joined
Jan 11, 2011
Messages
84
Office Version
  1. 365
Platform
  1. Windows
Hello there,

I am puzzled why my for next loop isn't working in the code pasted below. I am trying to convert the cells in selected range to indian number format where there is comma different from rest of the world.

Normal Format for number in 5 digits or less
Lacs Format for number in 7 or 8 digits
Crores Format for number in 9 digits or more

The code "nFmt_India" is working fine for individual cell but the when I apply to a larger selection, it does not work correctly.

Please help..

VBA Code:
Sub nFmt_Normal()
 For Each c In Selection.Cells
    c.NumberFormat = "###,###,###"
Next c
End Sub

Sub nFormat_Lacs() ' 6 or 7 digits
    Selection.NumberFormat = "###\,##\,###"
End Sub

Sub nFormat_Cr() '8 digits or more
    Selection.NumberFormat = "###\,##\,##\,###"
End Sub

Sub nFmt_India()
Dim c As Range

 For Each c In Selection.Cells
        If c.value < 100000 Then ' 5 digits or less
            nFmt_Normal
        ElseIf c.value >= 100000 And c.value < 10000000 Then ' 6 or 7 digits
            nFormat_Lacs
        ElseIf c.value >= 10000000 Then '8 digits or more
            nFormat_Cr
        End If
    Next c
End Sub
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
You don't need separate subs.
VBA Code:
Sub nFmt_India()
Dim c As Range

 For Each c In Selection.Cells
        If c.Value < 100000 Then ' 5 digits or less
            c.NumberFormat = "###,###,###"
        ElseIf c.Value >= 100000 And c.Value < 10000000 Then ' 6 or 7 digits
            c.NumberFormat = "###\,##\,###"
        ElseIf c.Value >= 10000000 Then '8 digits or more
            c.NumberFormat = "###\,##\,##\,###"
        End If
    Next c
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,223,243
Messages
6,170,971
Members
452,371
Latest member
Frana

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