VBA - private sub working in background

cmefly

Well-known Member
Joined
May 13, 2003
Messages
683
Hi,

i'm trying to create a macro that works in the background of a worksheet.

I have 2 columns, A and B.

Column A has a pull down menu where the user can choose from 3 letters - A, B, or C.

Column B is an input cell where the user types in numeric value.

What i'm looking for is IF the user chooses A, then the value in column B should be formatted to 2 DECIMAL PLACES.

IF the user chooses B, then the value in column B should be formatted to 4 DECIMAL PLACES.

F the user chooses C, then the value in column B should be formatted to 5 DECIMAL PLACES.

this is what i have but i doesn't work properly b/c it keeps looping.....




Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim z As Integer

For z = 1 To 907 ' number of rows i have

If Range("a" & z).Value = "A" Then
Range("b" & z).Select
Selection.NumberFormat = "0.00"
End If

If Range("a" & z).Value = "B" Then
Range("b" & z).Select
Selection.NumberFormat = "0.0000"
End If

If Range("a" & z).Value = "C" Then
Range("b" & z).Select
Selection.NumberFormat = "0.00000"
End If


Next z

End Sub
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
perhaps use conditional forattibng instead?:
Excel Workbook
AB
23A2.35
24B2.3457
25a2.35
26A2.35
27B2.3457
28C2.34567
29A2.35
30B2.3457
31C2.34567
32A2.35
33B2.3457
34C2.34567
35A2.35
36B2.3457
37C2.34567
Sheet
Cells with Conditional Formatting
CellConditionCell FormatStop If True
B231. / Formula is =$A23="C"Abc
B232. / Formula is =$A23="B"Abc
B233. / Formula is =$A23="A"Abc


You can't see the format in the conditional formatting red box above but you can see its results.
Select the whole range when adding the conditional formatting and enter the formulae as if it only pertained to the active cell (usually the topmost of the selection) and get the $s in the right place.
 
Upvote 0

Forum statistics

Threads
1,224,550
Messages
6,179,459
Members
452,915
Latest member
hannnahheileen

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