PC Review


Reply
Thread Tools Rate Thread

Creating a table based on user input

 
 
matt3542
Guest
Posts: n/a
 
      15th Aug 2008
Hi Forum,

As always I would be very grateful if anyone can help with writing some
Excel VBA code that would allow me to complete the following task;

I have 2 input boxes that prompt the user to specify the value of two
variables (x = the required number of columns & y = the required number of
rows for a table of data. I would then like to create a loop to compile a x
by y size table with each cell having a random value in it where the range of
random numbers is between 1 and the total number of cells. I am really new to
VBA but have been told the code would involve the statement
ActiveCell.FormulaR1C1 = Int((TOTAL NUMBER OF CELLS * Rnd) + 1) but I am
struggling to make it work. I'd be really appreciative if anyone is willing
to take the time to help with the code for this loop.

Best wishes
Matt




 
Reply With Quote
 
 
 
 
Dave Peterson
Guest
Posts: n/a
 
      15th Aug 2008
Did you want to make sure that the table of random numbers had no duplicates?

I'm guessing that duplicates should not be allowed.

If that's true, visit J.E. McGimpsey's site and grab a copy of his =RandInt()
function.
http://www.mcgimpsey.com/excel/udfs/randint.html

Then add that to your project.

Then create a sub that uses that function:

Option Explicit
Sub testme01()

Dim myRng As Range

Set myRng = Nothing
On Error Resume Next
Set myRng = Application.InputBox(_
Prompt:="Please select a range for the table", _
Type:=8)
On Error GoTo 0

If myRng Is Nothing Then
Exit Sub 'user hit cancel
End If

With myRng
.FormulaArray = "=randint()"
.Value = .Value
End With

End Sub

If you're new to macros:

Debra Dalgleish has some notes how to implement macros here:
http://www.contextures.com/xlvba01.html

David McRitchie has an intro to macros:
http://www.mvps.org/dmcritchie/excel/getstarted.htm

Ron de Bruin's intro to macros:
http://www.rondebruin.nl/code.htm

(General, Regular and Standard modules all describe the same thing.)

matt3542 wrote:
>
> Hi Forum,
>
> As always I would be very grateful if anyone can help with writing some
> Excel VBA code that would allow me to complete the following task;
>
> I have 2 input boxes that prompt the user to specify the value of two
> variables (x = the required number of columns & y = the required number of
> rows for a table of data. I would then like to create a loop to compile a x
> by y size table with each cell having a random value in it where the range of
> random numbers is between 1 and the total number of cells. I am really new to
> VBA but have been told the code would involve the statement
> ActiveCell.FormulaR1C1 = Int((TOTAL NUMBER OF CELLS * Rnd) + 1) but I am
> struggling to make it work. I'd be really appreciative if anyone is willing
> to take the time to help with the code for this loop.
>
> Best wishes
> Matt
>
>


--

Dave Peterson
 
Reply With Quote
 
matt3542
Guest
Posts: n/a
 
      15th Aug 2008
Dear Dave,

Thankyou so much for taking the time to help me, I found your reply
extremely helpful and it well exceeded my expectations. I am overwhelmed by
your selfless generosity in sharing your expertise and in pointing me in the
right direction towards learning materials that will undoubtedly help to
further my understanding.

Have a great weekend and thankyou again
Matt

"Dave Peterson" wrote:

> Did you want to make sure that the table of random numbers had no duplicates?
>
> I'm guessing that duplicates should not be allowed.
>
> If that's true, visit J.E. McGimpsey's site and grab a copy of his =RandInt()
> function.
> http://www.mcgimpsey.com/excel/udfs/randint.html
>
> Then add that to your project.
>
> Then create a sub that uses that function:
>
> Option Explicit
> Sub testme01()
>
> Dim myRng As Range
>
> Set myRng = Nothing
> On Error Resume Next
> Set myRng = Application.InputBox(_
> Prompt:="Please select a range for the table", _
> Type:=8)
> On Error GoTo 0
>
> If myRng Is Nothing Then
> Exit Sub 'user hit cancel
> End If
>
> With myRng
> .FormulaArray = "=randint()"
> .Value = .Value
> End With
>
> End Sub
>
> If you're new to macros:
>
> Debra Dalgleish has some notes how to implement macros here:
> http://www.contextures.com/xlvba01.html
>
> David McRitchie has an intro to macros:
> http://www.mvps.org/dmcritchie/excel/getstarted.htm
>
> Ron de Bruin's intro to macros:
> http://www.rondebruin.nl/code.htm
>
> (General, Regular and Standard modules all describe the same thing.)
>
> matt3542 wrote:
> >
> > Hi Forum,
> >
> > As always I would be very grateful if anyone can help with writing some
> > Excel VBA code that would allow me to complete the following task;
> >
> > I have 2 input boxes that prompt the user to specify the value of two
> > variables (x = the required number of columns & y = the required number of
> > rows for a table of data. I would then like to create a loop to compile a x
> > by y size table with each cell having a random value in it where the range of
> > random numbers is between 1 and the total number of cells. I am really new to
> > VBA but have been told the code would involve the statement
> > ActiveCell.FormulaR1C1 = Int((TOTAL NUMBER OF CELLS * Rnd) + 1) but I am
> > struggling to make it work. I'd be really appreciative if anyone is willing
> > to take the time to help with the code for this loop.
> >
> > Best wishes
> > Matt
> >
> >

>
> --
>
> Dave Peterson
>

 
Reply With Quote
 
matt3542
Guest
Posts: n/a
 
      15th Aug 2008
Ps I forgot to mention the most important thing..the solution worked perfectly
Thanks
Matt

"Dave Peterson" wrote:

> Did you want to make sure that the table of random numbers had no duplicates?
>
> I'm guessing that duplicates should not be allowed.
>
> If that's true, visit J.E. McGimpsey's site and grab a copy of his =RandInt()
> function.
> http://www.mcgimpsey.com/excel/udfs/randint.html
>
> Then add that to your project.
>
> Then create a sub that uses that function:
>
> Option Explicit
> Sub testme01()
>
> Dim myRng As Range
>
> Set myRng = Nothing
> On Error Resume Next
> Set myRng = Application.InputBox(_
> Prompt:="Please select a range for the table", _
> Type:=8)
> On Error GoTo 0
>
> If myRng Is Nothing Then
> Exit Sub 'user hit cancel
> End If
>
> With myRng
> .FormulaArray = "=randint()"
> .Value = .Value
> End With
>
> End Sub
>
> If you're new to macros:
>
> Debra Dalgleish has some notes how to implement macros here:
> http://www.contextures.com/xlvba01.html
>
> David McRitchie has an intro to macros:
> http://www.mvps.org/dmcritchie/excel/getstarted.htm
>
> Ron de Bruin's intro to macros:
> http://www.rondebruin.nl/code.htm
>
> (General, Regular and Standard modules all describe the same thing.)
>
> matt3542 wrote:
> >
> > Hi Forum,
> >
> > As always I would be very grateful if anyone can help with writing some
> > Excel VBA code that would allow me to complete the following task;
> >
> > I have 2 input boxes that prompt the user to specify the value of two
> > variables (x = the required number of columns & y = the required number of
> > rows for a table of data. I would then like to create a loop to compile a x
> > by y size table with each cell having a random value in it where the range of
> > random numbers is between 1 and the total number of cells. I am really new to
> > VBA but have been told the code would involve the statement
> > ActiveCell.FormulaR1C1 = Int((TOTAL NUMBER OF CELLS * Rnd) + 1) but I am
> > struggling to make it work. I'd be really appreciative if anyone is willing
> > to take the time to help with the code for this loop.
> >
> > Best wishes
> > Matt
> >
> >

>
> --
>
> Dave Peterson
>

 
Reply With Quote
 
Dave Peterson
Guest
Posts: n/a
 
      15th Aug 2008
Glad you got it working!

matt3542 wrote:
>
> Ps I forgot to mention the most important thing..the solution worked perfectly
> Thanks
> Matt
>
> "Dave Peterson" wrote:
>
> > Did you want to make sure that the table of random numbers had no duplicates?
> >
> > I'm guessing that duplicates should not be allowed.
> >
> > If that's true, visit J.E. McGimpsey's site and grab a copy of his =RandInt()
> > function.
> > http://www.mcgimpsey.com/excel/udfs/randint.html
> >
> > Then add that to your project.
> >
> > Then create a sub that uses that function:
> >
> > Option Explicit
> > Sub testme01()
> >
> > Dim myRng As Range
> >
> > Set myRng = Nothing
> > On Error Resume Next
> > Set myRng = Application.InputBox(_
> > Prompt:="Please select a range for the table", _
> > Type:=8)
> > On Error GoTo 0
> >
> > If myRng Is Nothing Then
> > Exit Sub 'user hit cancel
> > End If
> >
> > With myRng
> > .FormulaArray = "=randint()"
> > .Value = .Value
> > End With
> >
> > End Sub
> >
> > If you're new to macros:
> >
> > Debra Dalgleish has some notes how to implement macros here:
> > http://www.contextures.com/xlvba01.html
> >
> > David McRitchie has an intro to macros:
> > http://www.mvps.org/dmcritchie/excel/getstarted.htm
> >
> > Ron de Bruin's intro to macros:
> > http://www.rondebruin.nl/code.htm
> >
> > (General, Regular and Standard modules all describe the same thing.)
> >
> > matt3542 wrote:
> > >
> > > Hi Forum,
> > >
> > > As always I would be very grateful if anyone can help with writing some
> > > Excel VBA code that would allow me to complete the following task;
> > >
> > > I have 2 input boxes that prompt the user to specify the value of two
> > > variables (x = the required number of columns & y = the required number of
> > > rows for a table of data. I would then like to create a loop to compile a x
> > > by y size table with each cell having a random value in it where the range of
> > > random numbers is between 1 and the total number of cells. I am really new to
> > > VBA but have been told the code would involve the statement
> > > ActiveCell.FormulaR1C1 = Int((TOTAL NUMBER OF CELLS * Rnd) + 1) but I am
> > > struggling to make it work. I'd be really appreciative if anyone is willing
> > > to take the time to help with the code for this loop.
> > >
> > > Best wishes
> > > Matt
> > >
> > >

> >
> > --
> >
> > Dave Peterson
> >


--

Dave Peterson
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Creating controls dynamically based on user input mohaaron@gmail.com Microsoft ASP .NET 4 1st Nov 2007 08:21 AM
Creating Multiple New Records based on User Input =?Utf-8?B?VENJUE0=?= Microsoft Access VBA Modules 1 11th Jul 2007 12:28 AM
Automaticly Creating Hyperlink based on user input =?Utf-8?B?VGltIE1jRA==?= Microsoft Access Form Coding 4 20th Jan 2006 04:41 PM
creating a user input form based on many to many relationship =?Utf-8?B?cm9nZXIxYW5pY2tuYW1l?= Microsoft Access 1 29th Nov 2003 01:12 AM
creating a user input form based on many to many relationship =?Utf-8?B?cm9nZXIxYW5pY2tuYW1l?= Microsoft Access Forms 0 28th Nov 2003 03:06 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:43 PM.