Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Format cells programmatically

Posted on 2014-02-04
4
Medium Priority
?
181 Views
Last Modified: 2014-02-04
I have a range of cells in an Excel worksheet, columns B through Z and rows 2 through 100. I need to set the format of each cell in that range to numeric, no decimals and negative numbers represented by parenthesis instead of a minus sign. How do I do it programmatically?  I already have a macro that does some other stuff. I just need to add a routine to it to accomplish what I just described. Thank you.
0
Comment
Question by:dbfromnewjersey
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 15

Assisted Solution

by:unknown_routine
unknown_routine earned 300 total points
ID: 39832745
Here is the code:

Range("B2:Z100").Select
Selection.NumberFormat = "0_);(0)"
0
 
LVL 81

Assisted Solution

by:byundt
byundt earned 300 total points
ID: 39832758
It's a one-line macro:
Sub Formatter()
Range("B2:Z100").NumberFormat = "#;(#);0;@"
End Sub

Open in new window

0
 
LVL 18

Accepted Solution

by:
Steven Harris earned 1400 total points
ID: 39832766
In line with the last question, and run the same way:

Sub ChangeNumberFormat()
Dim cell As Range
On Error Resume Next
    For Each cell In Selection
        cell.NumberFormat = "#;(#);0;@"
    Next
End Sub

Open in new window


or if you are wanting to combine the operations for each selection made:

Sub FillEmptyandChangeFormat()
Dim cell As Range
Dim InputValue As String
On Error Resume Next
InputValue = "0"
    For Each cell In Selection
        If IsEmpty(cell) Then
            cell.Value = InputValue
        End If
    Next
    For Each cell In Selection
        cell.NumberFormat = "#;(#);0;@"
    Next
End Sub

Open in new window

0
 

Author Comment

by:dbfromnewjersey
ID: 39832849
Thanks
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
If you need to forecast numbers -- typically for finance -- the Windows and Mac versions of Excel 2016 have a basket of tools to get the job done.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question