Worksheet_Change event to convert percentages to whole numbers

umpirerob

New Member
Joined
Jan 29, 2013
Messages
13
I have a page for data entry that I want to automatically convert any data that is a percentage to a whole number (50% to 50 - not to 0.50). I am in the Sheet code (not the workbook) and I can't seem to get this to work. Data will likely be pasted by the user in bulk to a range (B3:H32) though data may not cover the entire range. I want the conversion to happen seamlessly to the user as my userbase will exceed 1,000 users, many of whom are not Excel savvy. When I enter data as a percent in the range on the sheet (using B column only at the moment for testing), nothing happens. Right now, the loop only covers the B column, but ideally, it will cover the entire range. Any ideas of what I'm doing wrong?

Code:
Private Sub Worksheet_Change(ByVal Target As Range)


    If Not Intersect(Target, Range("B3:H32")) Is Nothing Then
    
        For Each c In Range("B3:B32")
            
            If c.NumberFormat = "0.00%" Then
             Application.EnableEvents = False
              c.NumberFormat = general
              c.Value = c.Value * 100
              Application.EnableEvents = True
            End If
        Next
    End If
End Sub
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
Perhaps instead of

If c.NumberFormat Like "0.00%" Then
use
If c.NumberFormat Like "*%" Then

This way if the default format is ANY percentage variant it will update it as you require.

Also, it appears this way of processing the data is going to be slow (especially as the Intersection range grows)

Are all cell formats going to be 'General'? If so, it may be easier to do that portion all at once.
 
Upvote 0
Perhaps instead of

If c.NumberFormat Like "0.00%" Then
use
If c.NumberFormat Like "*%" Then

This way if the default format is ANY percentage variant it will update it as you require.

Also, it appears this way of processing the data is going to be slow (especially as the Intersection range grows)

Are all cell formats going to be 'General'? If so, it may be easier to do that portion all at once.

No, that didn't do it. My range will always be this size. I planned on doing each column separately because not all columns will always have data, but I suppose I could do both Rows and Columns independently. I'd be happy if I could just get it to work on a single cell at this point, but it doesn't.
 
Upvote 0
I don't see why it wouldn't be working then. I have run it on my computer and it does exactly what you are expecting.

If you debug it when it runs, and step through with F8, see if you see anything unexpected (i.e. wrong sheet, wrong cell references, etc)
 
Upvote 0
It won't let me step through... makes me choose a macro rather than this code. I've learned VBA for Excel from places like this, so I really have no idea how to use the editor. ;) What I know is that when I hit the run button (green arrow), it prompts me to choose a macro. All my other macros are global in the workbook, but after some research, I found this had to be put in the sheet code. So there it is, apparently good code (as it's working for you), doing absolutely nothing to my data. :mad:

I threw the code into Sheet 1 of a blank workbook, put data in column B and nothing. :(
 
Last edited:
Upvote 0
You can force the macro to halt by setting a breakpoint on a line of code.
To do this, put your cursor on a line of code and hit F9.

For your purposes, I would just put the break on the 'Private Sub Worksheet_Change(ByVal Target As Range)' line.
You will know it is there because there will be a red circle and the line will by highlighted in red.

Then when the Worksheet_Change event is triggered you will instantly be in Break Mode and can use F8 to step through line by line.

If it doesn't hit the break then the Worksheet_Change event isn't being triggered (wrong sheet?)

Unfortunately, it is impossible for me to figure out what the problem is because everything with your code appears to be correct.
 
Upvote 0
When it gets to my IF statement within the For Each loop, it goes straight to my End IF and runs through until the loop is complete. It's like it's not recognizing the formatting, doesn't see anything that makes the If c.NumberFormat = "*%" statement true, so it never runs the format change at all.

I'm running this in a whole new sheet (only 1 sheet in the workbook) with the code in the sheet and only a few data points - B3:B8 - all formatted as %.
 
Upvote 0
When it gets to my IF statement within the For Each loop, it goes straight to my End IF and runs through until the loop is complete. It's like it's not recognizing the formatting, doesn't see anything that makes the If c.NumberFormat = "*%" statement true, so it never runs the format change at all.

I'm running this in a whole new sheet (only 1 sheet in the workbook) with the code in the sheet and only a few data points - B3:B8 - all formatted as %.

OK, the breakpoint thing again, except this time, copy/paste the entire range you are looking at B3:B49 with percentages.
When you step through the code and get to the c.NumberFormat = ... line, hover your mouse over c.NumberFormat and see what it says that cell's NumberFormat actually is. That will likely be the key to determining what is going wrong.

EDIT: actually, I just read that you already have all cells as %'s so just do the hover part when that line is reached (highlighted yellow)
 
Upvote 0

Forum statistics

Threads
1,223,230
Messages
6,170,883
Members
452,364
Latest member
springate

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