+ Reply to Thread
Results 1 to 13 of 13

Select range size determined by value in cell

  1. #1
    Registered User
    Join Date
    12-17-2013
    Location
    South Africa
    MS-Off Ver
    Excel 2010
    Posts
    39

    Select range size determined by value in cell

    Hi,

    I require a VBA code which selects a range of cells and fills it with a formula. The number of cells to select can be found in cell C1. The value in cell C1 will change every month.

    For example, this month the value will be 87. Therefore i would like to select 87 cells from B3 downward. I then want to fill in the selected Cells with the following formula =RANDBETWEEN(1,$C$1).

    Can someone help me with code which can do this?

    Thank you

  2. #2
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Select range size determined by value in cell

    Maybe:

    Please Login or Register  to view this content.

  3. #3
    Forum Expert XOR LX's Avatar
    Join Date
    04-18-2013
    Location
    Turin, Italy
    MS-Off Ver
    Office 365
    Posts
    7,742

    Re: Select range size determined by value in cell

    Hi,

    I think that should be:

    Please Login or Register  to view this content.
    etc.

    Regards
    Click * below if this answer helped

    Advanced Excel Techniques: http://excelxor.com/

  4. #4
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Select range size determined by value in cell

    Try this.
    Please Login or Register  to view this content.
    If posting code please use code tags, see here.

  5. #5
    Registered User
    Join Date
    12-17-2013
    Location
    South Africa
    MS-Off Ver
    Excel 2010
    Posts
    39

    Re: Select range size determined by value in cell

    Thanks for the reply. This works.

    However, I'm obviously lacking the some brain capacity today, but when i add this code to existing code i get the 'run time 1004' error.

    Code:

    ActiveWorkbook.Sheets("Spot Buy Analysis").Activate
    Range("B3").Resize(Range("C1").Value).Formula = "=RANDBETWEEN(1, $C$1)"


    What am i missing?

  6. #6
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Select range size determined by value in cell

    Which worksheet(s) are B3 and C1 on?

    Does this work?

    Please Login or Register  to view this content.

  7. #7
    Registered User
    Join Date
    12-17-2013
    Location
    South Africa
    MS-Off Ver
    Excel 2010
    Posts
    39

    Re: Select range size determined by value in cell

    Cells B3 and C1 are in the "Spot Buy Analysis" worksheet.

    With the above With, End With - i still get the 1004 error

  8. #8
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Select range size determined by value in cell

    If there is a valid value in C1 on that sheet then the code should work, assuming the sheet isn't protected of course

    What's the actual error message?

  9. #9
    Registered User
    Join Date
    12-17-2013
    Location
    South Africa
    MS-Off Ver
    Excel 2010
    Posts
    39

    Re: Select range size determined by value in cell

    The value of 87 is in C1. Im pretty confused.

    Error:
    Run-time error '1004'
    Application-defined or object-defined error

  10. #10
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Select range size determined by value in cell

    What code did you add this to?

  11. #11
    Registered User
    Join Date
    12-17-2013
    Location
    South Africa
    MS-Off Ver
    Excel 2010
    Posts
    39

    Re: Select range size determined by value in cell

    My Code:

    Private Sub CommandButton2_Click()

    ActiveWorkbook.Sheets("Master").Activate
    ActiveSheet.Cells(2, 1).Select
    ActiveSheet.Range("A2:A150000").Select
    Selection.FormulaR1C1 = "=Sheet1!RC"
    ActiveSheet.Cells(2, 2).Select
    ActiveSheet.Range("B2:B150000").Select
    Selection.FormulaR1C1 = "=VALUE(LEFT(Sheet1!RC[2],6))"
    ActiveWindow.SmallScroll Down:=-12
    ActiveSheet.Cells(2, 3).Select
    ActiveSheet.Range("C2:C150000").Select
    Selection.FormulaR1C1 = "=VALUE(Sheet1!RC[2])"
    ActiveSheet.Cells(2, 4).Select
    ActiveSheet.Range("D2:D150000").Select
    Selection.FormulaR1C1 = "=VALUE(Sheet1!RC[2])"
    ActiveWindow.SmallScroll Down:=-6
    ActiveSheet.Cells(2, 5).Select
    ActiveSheet.Range("E2:E150000").Select
    Selection.FormulaR1C1 = "=Sheet1!RC[11]"
    ActiveWindow.SmallScroll Down:=-6
    ActiveSheet.Cells(2, 6).Select
    ActiveSheet.Range("F2:F150000").Select
    Selection.FormulaR1C1 = _
    "=IF(Master!RC[-4]=IFERROR(VLOOKUP(Master!RC[-4],'OEM-Agent to OEM'!R3C1:R178C10,1,FALSE),""""),""OEM"",IF(Master!RC[-4]=IFERROR(VLOOKUP(Master!RC[-4],'rotables suppliers'!R3C1:R70C10,1,FALSE),""""),""Rotables"",IF(Master!RC[-2]=IFERROR(VLOOKUP(Master!RC[-2],'Contracted Suppliers'!R4C1:R137C6,1,FALSE),""""),""Contracted"",""No"")))"
    ThisWorkbook.RefreshAll




    ActiveWorkbook.Sheets("Instructions").Activate

    ActiveSheet.Cells(20, 4).Select
    Selection.FormulaR1C1 = _
    "=IFERROR(COUNT(Sheet1!R[-18]C[12]:R[149980]C[12]),"""")"
    ActiveSheet.Cells(20, 5).Select
    Selection.FormulaR1C1 = _
    "=IFERROR(SUM(Sheet1!R[-18]C[11]:R[149980]C[11]),"""")"

    ActiveWorkbook.Sheets("Spot Buy Analysis").Activate
    Range("B3").Resize(Range("C1").Value).Formula = "=RANDBETWEEN(1, $C$1)"

    End Sub

  12. #12
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Select range size determined by value in cell

    Can you upload an example workbook?

    Click on GO ADVANCED and use the paperclip icon to open the upload window.

    PS Can you add code tags?

  13. #13
    Registered User
    Join Date
    12-17-2013
    Location
    South Africa
    MS-Off Ver
    Excel 2010
    Posts
    39

    Re: Select range size determined by value in cell

    Unfortunately the workbook is too large to upload. I will try and delete some data and upload a smaller version if i can.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Using code to select a cell directly below a range of an unknown size
    By LaffyAffy13 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 07-01-2013, 11:44 AM
  2. Select range of given size
    By vizzkid in forum Excel General
    Replies: 3
    Last Post: 04-19-2013, 10:12 AM
  3. [SOLVED] Construct a formula on a pre-determined cell for a pre-determined duration of cells
    By Shaun Gemiver in forum Excel Formulas & Functions
    Replies: 8
    Last Post: 01-03-2013, 11:14 AM
  4. Replies: 2
    Last Post: 12-16-2011, 05:57 PM
  5. Select Row determined by Variable
    By Pergo in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-02-2011, 12:23 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1