简体   繁体   中英

VBA Change Cell formatting based on value

I wanted to know how to use VBA to automatically format the cell i enter a value in. I actually divided the numbers in 3 categories: percentages, small numbers (-1000 - 1000), and large numbers.

I want percentages to be displayed with 2 decimals and the % sign. Small numbers with 2 decimals as well. And large numbers rounded to nearest integer, with thousands separators.

I have the conditions in VBA, but i want the code to reformat the cell if the cell value changes, and idk how to do that. For instance, if i change a cell with value "50,000", to 60%, then, it should be displayed as "60.00%". What i have so far is code that applies formatting on existing cell values.

 Sub myNumberFormat() Dim cel As Range Dim selectedRange As Range Set selectedRange = Selection For Each cel In selectedRange.Cells If Not CStr(cel.Text) Like "*%*" Then If Not IsEmpty(cel) Then If cel.Value < 1000 And cel.Value > -1000 Then cel.NumberFormat = "_(#,##0.00_);_(-#,##0.00_);_(""-""??_)" Else cel.NumberFormat = "_(#,##0_);_((#,##0);_(""-""??_)" End If End If Else cel.NumberFormat = "0.00%" End If Next cel End Sub

Also idk if this the best code. I am new to VBA and i mostly googled bits and pieces. Would really appreciate some help and tips, cause i am tired of formatting stuffs:). Thanks

A Worksheet Change: Apply Cell Formatting

  • This is a simple example for a single column, in this particular case, the range from A2 to the bottom-most cell in column A . But you can choose any reasonable cell address. In whatever cell you manually change (enter, copy/paste, or write using VBA) its value, the cell will be formatted according to your 'rearranged' procedure ApplyMySpecialNumberFormat .
  • Note that this works automatically, just copy the codes to the appropriate modules and start entering data into the cells of the range to see it work.

Sheet Module eg Sheet1

 Option Explicit Private Sub Worksheet_Change(ByVal Target As Range) ' Define constants. Const FirstCellAddress As String = "A2" ' adjust to any reasonable cell ' Reference the source column range eg 'A2:ALastWorksheetRow'. Dim srg As Range ' With Me.Range(FirstCellAddress) Set srg =.Resize(Me.Rows.Count -.Row + 1) End With ' Reference the cells of the source range that have changed. Dim irg As Range: Set irg = Intersect(srg, Target) If irg Is Nothing Then Exit Sub ' no source range cells changed; exit ' At least one cell was changed: ApplyMySpecialNumberFormat irg End Sub

Standard Module eg Module1

 Option Explicit Sub ApplyMySpecialNumberFormat(ByVal rg As Range) Dim cel As Range For Each cel In rg.Cells If CStr(cel.Text) Like "*%*" Then cel.NumberFormat = "0.00%" Else If VarType(cel) = vbDouble Then ' is a number If cel.Value < 1000 And cel.Value > -1000 Then cel.NumberFormat = "_(#,##0.00_);_(-#,##0.00_);_(""-""??_)" Else cel.NumberFormat = "_(#,##0_);_((#,##0);_(""-""??_)" End If End If End If Next cel End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM