Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

powershell shell/excel populating numbers

Posted on 2016-08-30
4
Medium Priority
?
35 Views
Last Modified: 2016-09-20
I have some code below with excel that runs but need help editing to  populate certain info. For example this updates another column with information in sequential order This part "$DEPT+ ([math]::floor($i++/$NUMBER)+1)"

I would like to be able to hard code this as for example: "Miami" and let this increment from Miami001 to a certain number like Miami300. If its possible The last thing is if I want do is exclude certain numbers "which I know might be very hard" For example if I ran the script and it starts at Miami001 to Miami300 and want to prevent Miami29 or Miami57 would this be able to exclude. Please let me know if anyone can help, thanks

[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic') | Out-Null
$NUMBER = [Microsoft.VisualBasic.Interaction]::InputBox("Please enter a number per batch?")

[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic') | Out-Null
$DIR = [Microsoft.VisualBasic.Interaction]::InputBox("Please enter the following. The complete path, file and the extension which should be an CSV")

[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic') | Out-Null
$DEPT = [Microsoft.VisualBasic.Interaction]::InputBox("Please enter a unique name of the batches")


$excel = New-Object -ComObject excel.application
$excel.visible=$true
$wb = $excel.WorkBooks.open("$DIR")
$ws = $wb.Worksheets.Item(1)
$row = $ws.Range("2:2")
$i = 0
while ($row.Cells.Item(1,1).value2)
{
  $row.Cells.Item(1,2).value2 = $DEPT+ ([math]::floor($i++/$NUMBER)+1)
  $row = $row.Offset(1,0)
}
$wb.Save()
$excel.Quit()
Remove-Variable excel, wb, ws, rowcode]

Open in new window

0
Comment
Question by:techdrive
  • 2
4 Comments
 
LVL 46

Expert Comment

by:aikimark
ID: 41777921
You should be able to format the numeric value with leading zeroes
0
 

Author Comment

by:techdrive
ID: 41778883
I do not understand what you are saying. Can you provide an example?
0
 
LVL 46

Accepted Solution

by:
aikimark earned 2000 total points (awarded by participants)
ID: 41778898
Test this:
$row.Cells.Item(1,2).value2 = $DEPT+ ([math]::floor($i++/$NUMBER)+1).ToString("000")

Open in new window

0

Featured Post

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

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

This article describes a serious pitfall that can happen when deleting shapes using VBA.
In this post we will be converting StringData saved within a text file into a hash table. This can be further used in a PowerShell script for replacing settings that are dynamic in nature from environment to environment.
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

963 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