Solved

How do I format a number cell to Accounting in Excel using Powershell?

Posted on 2010-09-23
2
2,868 Views
Last Modified: 2012-05-10
I have a Powershell script that creates an Excel spreadsheet. I would like to format some of the cells with numbers to Accounting with 0 decimal places and no symbol. How do I do this? For example, how would I change the format to Accounting for the numbers in column 2 on rows 7-12 in my snippet code below?
$Excel = New-Object -Com Excel.Application
$Excel.visible = $False
$Excel.DisplayAlerts = $False
$Wkbk = $Excel.Workbooks.Add()
$Sheet = $Wkbk.Worksheets.Item(1)

$Sheet.Cells.Item(1,1) = "ID Admin"
$Sheet.Cells.Item(2,1) = "Outstanding Task Summary"
$Sheet.Cells.Item(3,1) = "Date: " + $Date
$Sheet.Cells.Item(7,2) = $disableuser_1days
$Sheet.Cells.Item(8,2) = $disableuser_2days
$Sheet.Cells.Item(9,2) = $disableuser_3days
$Sheet.Cells.Item(10,2) = $disableuser_4days
$Sheet.Cells.Item(11,2) = $disableuser_5days
$Sheet.Cells.Item(12,2) = $disableuser_gt5days

$Range = $Sheet.Range("A1:H1")
$Range.MergeCells = $true
$Range = $Sheet.Range("A2:H2")
$Range.MergeCells = $true
$Range = $Sheet.Range("A3:H3")
$Range.MergeCells = $true
$intRow = 7
	Do {
	$Sheet.Cells.Item($intRow,2).HorizontalAlignment = -4108
	$Sheet.Cells.Item($intRow,2).VerticalAlignment = -4160
	$intRow++
	} Until ($intRow -gt 12)
$WorkBook = $Sheet.UsedRange
$WorkBook.EntireColumn.AutoFit()

$Wkbk.SaveAs($SLAReportXLFile,1)
$Excel.Quit()

Open in new window

0
Comment
Question by:reason4xistence
[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
2 Comments
 
LVL 85

Accepted Solution

by:
Rory Archibald earned 500 total points
ID: 33744350
I suspect:
$Sheet.Range("B7:B12").NumberFormat = "_(* #,##0_);_(* (#,##0);_(* ""-""_);_(@_)"
0
 

Author Closing Comment

by:reason4xistence
ID: 33744823
Wow! I am going to look up NumberFormat. That doesn't look like normal Powershell regular expressions. At any rate, it works perfectly. Thanks so much, rorya!
0

Featured Post

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

617 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