Solved

Input text into excel using random generation to 10,000 cells

Posted on 2014-02-02
5
259 Views
Last Modified: 2014-02-20
I am working on creating an excel workbook that creates 10 worksheets and randomly add's numbers to a group of 10,000 cells.  I am stuck on getting the random part to each cell.  Individually it is working, but as a group it is not.  Any help would be greatly appreciated!

Here is the code I have so far:

$Excel01 = New-Object -ComObject Excel.Application
$Excel01.Visible = $True
$Excel01.SheetsInNewWorkbook = 10
$Workbook01 = $Excel01.Workbooks.Add()
$Worksheet01 = $Workbook01.Sheets.Item(1)
$Worksheet01.Cells.Item(1,1) = "This Works!"
$Worksheet01.Activate()

Open in new window

0
Comment
Question by:tomtom9898
[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
  • 2
5 Comments
 
LVL 18

Accepted Solution

by:
Steven Harris earned 500 total points
ID: 39828349
Have you looked into a for loop?  I haven't tested, but will probably be slow to process from PS to XL.

$Excel01 = New-Object -ComObject Excel.Application
$Excel01.Visible = $True
$Excel01.SheetsInNewWorkbook = 10
$Workbook01 = $Excel01.Workbooks.Add()
$Worksheet01 = $Workbook01.Sheets.Item(1)
for ($row = 1; $row -lt 11; $row++)
{
    $Worksheet01.Cells.Item($row,1) = "X"
}

Open in new window

0
 

Author Comment

by:tomtom9898
ID: 39828402
Let me try yours real quick, but I think this would not do 10,000 cells of random data.  If it helps, I want to Take the letters "PS" and add a random number to it between 1 and 10,000 so I have 10,000 different codes all Starting with PSXXXX
0
 
LVL 18

Expert Comment

by:Steven Harris
ID: 39828500
In one sheet, or across all sheets? I may have been on the wrong track :(

Just for grins - This will loop through all sheets and then put an X in a specific range (A1:E10) for each sheet.

$Excel01 = New-Object -ComObject Excel.Application
$Excel01.Visible = $True
$Excel01.SheetsInNewWorkbook = 10
$Workbook01 = $Excel01.Workbooks.Add()

$Worksheets = $Workbook01.WorkSheets.Item($WorkSheetNum)
foreach($worksheet in $Workbook01.Worksheets){
for ($i = 1; $i -lt 11; $i++){
    $Worksheet.Cells.Item($i,1) = "X"
    $Worksheet.Cells.Item($i,2) = "X"
    $Worksheet.Cells.Item($i,3) = "X"
    $Worksheet.Cells.Item($i,4) = "X"
    $Worksheet.Cells.Item($i,5) = "X"}}

Open in new window


I am sure we can use the same logic to add the differential numbers as well.
0
 
LVL 2

Expert Comment

by:c_kedar
ID: 39854390
Use Get-Random cmdlet to generate random number in PowerShell.
help Get-Random

NAME
    Get-Random
SYNOPSIS
    Gets a random number, or selects objects randomly from a collection.
SYNTAX
    Get-Random [-InputObject] <Object[]> [-Count <int>] [-SetSeed <int>] [<CommonParameters>]

Open in new window

Use them as you want.

It is not clear where you want to put random numbers in certain cells in each sheet or you want to put them in random cells spread across these sheets.

But once you have random numbers you can use them as per need.

Let me know if you need more help.
0
 

Author Closing Comment

by:tomtom9898
ID: 39873196
This worked, thanks so much for the help!
0

Featured Post

Free eBook: Backup on AWS

Everything you need to know about backup and disaster recovery with AWS, for FREE!

Question has a verified solution.

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

You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
A recent project that involved parsing Tableau Desktop and Server log files to extract reusable user queries for use in other systems. I chose to use PowerShell to gather the data, and SharePoint to present it...
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

749 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