?
Solved

powershell numbering excel fields automatically

Posted on 2015-01-25
5
Medium Priority
?
64 Views
Last Modified: 2015-02-06
Good day folks I have a challenge that I have been given and need some assistance in resolving this issue. I have an excel spreadsheet with the following columns named emailaddress and batchnumbers. What I would like to do is to be able to on a fly be prompted for a batch number. The batch number depending on what it is will uniquely distinguish those group those users to the same batch. For example lets say I want to batch/group the users below every 5 users.

emailaddress                            batchnumber
user1@microsoft.com             uniquebatch1
user2@microsoft.com             uniquebatch1
user3@microsoft.com              uniquebatch1
user4@microsoft.com             uniquebatch1
user5@microsoft.com             uniquebatch1
user12@microsoft.com           uniquebatch2
user31@microsoft.com           uniquebatch2
user14@microsoft.com           uniquebatch2
user18@microsoft.com          uniquebatch2
user23@microsoft.com          uniquebatch2
user33@microsoft.com          uniquebatch3
user40@microsoft.com          uniquebatch3
user19@microsoft.com          uniquebatch3
user2@microsoft.com            uniquebatch3
user31@microsoft.com          uniquebatch3


Or if I wanted to batch 50 users per batch it will automatically be prompted for the amount of users and automatically do this. Any assistance would be great, thanks
0
Comment
Question by:techdrive
[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
  • 3
  • 2
5 Comments
 
LVL 70

Expert Comment

by:Qlemo
ID: 40570131
A simple formula would suffice, so why the need for PS? Not difficult, but slow in PS.
0
 

Author Comment

by:techdrive
ID: 40570295
I would rather have PS that a formula in excel.
0
 
LVL 70

Accepted Solution

by:
Qlemo earned 2000 total points
ID: 40570961
I'm not clear about what you expect to see as batch number, so I will take over what you used as example, and just append a running number.
$batchsize = Read-Host "What is the size of each batch group?"

$excel = New-Object -ComObject excel.application
$excel.visible=$true
$wb = $excel.WorkBooks.open("C:\Temp\EE\Q_28603713.xlsx")
$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 = "uniquebatch"+ ([math]::floor($i++/$batchsize)+1)
  $row = $row.Offset(1,0)
}
$wb.Save()
$excel.Quit()
Remove-Variable excel, wb, ws, row

Open in new window

0
 

Author Comment

by:techdrive
ID: 40571573
Excellent sir
0
 

Author Closing Comment

by:techdrive
ID: 40594542
.
0

Featured Post

Need protection from advanced malware attacks?

Look no further than WatchGuard's Total Security Suite, providing defense in depth against today's most headlining attacks like Petya 2.0 and WannaCry. Keep your organization out of the news with protection from known and unknown threats.

Question has a verified solution.

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

A brief introduction to what I consider to be the best editor for PowerShell.
Auditing domain password hashes is a commonly overlooked but critical requirement to ensuring secure passwords practices are followed. Methods exist to extract hashes directly for a live domain however this article describes a process to extract u…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

743 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