How my Sub can write N values to a worksheet and redefine a range?

JenniferMurphy

Well-known Member
Joined
Jul 23, 2011
Messages
2,525
Office Version
  1. 365
Platform
  1. Windows
I am working on a sub that will generate a list of random numbers of variable length. I want it to write those values to the calling sheet starting at a named cell and then (re)define that range.

For example: I have assigned the name "Hdr" to D5 and "Count" to D4. Count (D4) currently contains "10". I have a button control that will call my Sub named "DoIt". DoIt will read the value in Count (10) and then generate 10 random numbers. Now I want DoIt to save those 10 values in D6:D15 (offset(Hdr,1,0):eek:ffset(Hdr,Count+1,0). Then I want it to assign the name "NewData" to D6:D15.

Can someone help me with the code to do that?

Thanks
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
Try:

VBA Code:
'If MyRandoms have dimensions (1 to N)
With Range("Hdr").Offset(1).Resize(N)
    .Value = Application.Transpose(MyRandoms)
    .Name = "NewData"
End With

'If MyRandoms have dimensions(1 to N, 1 to 1)
With Range("Hdr").Offset(1).Resize(N)
    .Value = MyRandoms
    .Name = "NewData"
End With

EDIT: Sorry, it looks like your Hdr is a VBA range variable? In which case, just replace Range("Hdr") with Hdr.
 
Last edited:
Upvote 0
Solution
Try:

VBA Code:
'If MyRandoms have dimensions (1 to N)
With Range("Hdr").Offset(1).Resize(N)
    .Value = Application.Transpose(MyRandoms)
    .Name = "NewData"
End With

'If MyRandoms have dimensions(1 to N, 1 to 1)
With Range("Hdr").Offset(1).Resize(N)
    .Value = MyRandoms
    .Name = "NewData"
End With

EDIT: Sorry, it looks like your Hdr is a VBA range variable? In which case, just replace Range("Hdr") with Hdr.

Stephen,

This is really helpful. Thanks.

Here's my code that seems to work. Please let me know if you have any comments or suggestions:

VBA Code:
Const rnDataOutHdr As String = "NormDataHdr"
Const rnData As String = "NormData"
Dim N As Long   'Size of dataset
N = InputBox("Next N", "Test", 0)
If N = 0 Then Exit Sub
Dim testdata() As Variant
testdata = Application.RandArray(N, 1, 1, 10, True)
With Range(rnDataOutHdr).Offset(1).Resize(N)
  .Value = testdata
  .Name = rnData
End With
 
Upvote 0
Try:

VBA Code:
'If MyRandoms have dimensions (1 to N)
With Range("Hdr").Offset(1).Resize(N)
    .Value = Application.Transpose(MyRandoms)
    .Name = "NewData"
End With

'If MyRandoms have dimensions(1 to N, 1 to 1)
With Range("Hdr").Offset(1).Resize(N)
    .Value = MyRandoms
    .Name = "NewData"
End With

EDIT: Sorry, it looks like your Hdr is a VBA range variable? In which case, just replace Range("Hdr") with Hdr.
This is working great. Thanks
 
Upvote 0
I have another problem. The range in the sheet that I am updating (NormData) contains the Y values for a chart. But the chart isn't getting updated until the Sub exits. I added the DoEvents statement to force this to happen and the MsgBox statement so I could see it, but it isn't working. It still does not update the chart until the sub exits, then the chart is updated.

Can someone tell me what I am doing wrong? Thanks

VBA Code:
'Declarations
Const rnDataOutHdr As String = "NormDataHdr"  'The cell above the data range
Const rnData As String = "NormData"           'The data range
Dim N As Long                                 'Size of dataset
Dim testdata() As Variant                     'The new data to go in the data range

'Code
Do
  N = InputBox("Next N", "Test", 0)
  If N = 0 Then Exit Sub
  testdata = Application.RandArray(N, 1, 1, 10, True)   'Generate test data
  With Range(rnDataOutHdr).Offset(1).Resize(N)          'Update chart Y values
    .Value = testdata
    .Name = rnData
  End With
  DoEvents                                              'Allow chart to refresh
  Msgbox "See new chart"
Loop
 
Upvote 0
Try replacing your DoEvents with a little time loop:

VBA Code:
Dim t As Double

'....

t = Timer
Do While Timer < t + 0.1
    DoEvents
Loop

By the way, there is a specific Chart.Refresh method in VBA, which you'd think would be sufficient to update the graph. But it's not, even with a DoEvents added.
 
Upvote 0
Try replacing your DoEvents with a little time loop:

VBA Code:
Dim t As Double

'....

t = Timer
Do While Timer < t + 0.1
    DoEvents
Loop
That seems to be working. Thanks

Do I just leave the previous post as the solution or change that to this one?

By the way, there is a specific Chart.Refresh method in VBA, which you'd think would be sufficient to update the graph. But it's not, even with a DoEvents added.

(sigh) Just one of the millions of things in M$FT code that don't quite work right.
 
Upvote 0

Forum statistics

Threads
1,214,382
Messages
6,119,194
Members
448,874
Latest member
Lancelots

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top