How to edit values in the range of cells from User defined Function?

ps1234

New Member
Joined
Oct 4, 2021
Messages
6
Office Version
  1. 365
So, I want to write into range of cells based on information read from the sheet cells. I have uploaded a mini-sheet and code below to show what I have going. The goal is to get below output

startVal = 63 (Value in B3)
length = 4 (Value in B4)

=PrintData(Sheet!$B$3,Sheet1!$B$4,"Sheet1!$B$7") this formula in B7, ideally in B6

output should be :
63
64
65
66

Public Function PrintData(a, b As Range, c As String) As String
StartValue = CInt(a.Value)
Length = CInt(b.Value)
outRange = Range(c)
PrintData = CStr(StartValue) 'this should fill outRange(1,1).Value)

For i = 2 To Length
outRange(i, 1).Value = CStr(StartValue + i) ' some data in place of data from POST RestApi
Next

End Function
 

Attachments

  • sheet.png
    sheet.png
    13.2 KB · Views: 18
  • vba.png
    vba.png
    26.5 KB · Views: 18

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
For V2016 and UDF in 2016 formula needs to be entered with Ctrl-Shift-Enter not just Enter.

delme1.xlsx
ABCDE
1
2
3Start64
4Length4
5
6V365V2016UDF
7Output646464
8656565
9666666
10676767
11
Sheet1
Cell Formulas
RangeFormula
B7:B10B7=SEQUENCE(B4,1,B3,1)
C7:C10C7=ROW(INDIRECT("1:" & B4))+B3-1
D7:D10D7=SequenceVBA(B3,B4)
Dynamic array formulas.


VBA Code:
Function SequenceVBA(Start As Long, Count As Long)
    Dim i As Long
    ReDim A(1 To Count, 1 To 1)
    For i = 1 To Count
        A(i, 1) = Start + i - 1
    Next
    SequenceVBA = A
End Function
 
Upvote 0
Thanks for the reply. Is it possible to not select the output cells range manually, instead the vba code calculate the range internally based on length specified in B4.
So, say if I make the length 10 in B4, vba prints number from 64 to 73 in B6-B15.

In real project, I am getting an array of data from a Rest-Api, I need to dynamically display the data starting from StartCellAddress which in this case would be B6.

Thank you so much for your help!
 
Upvote 0
The Function SequenceVBA above does that. It returns an array that can be used in another VBA code or entered onto the worksheet. Since it returns an array, if it is enetered into the worksheet in Excel 2016 it needs to be entered as Ctrl-Shift-Enter not just Enter. If you change B4 to 10 in the sheet I posted above you will see the arrays adjust themselves to be length 10.
 
Upvote 0
Solution
Ok I see it work now. I have excel 365 version, I guess it formula directly. If I do ctrl-Shift-Enter, I only get one cell populated, if however I enter formula using just enter, it works as expected. thanks so much!
 
Upvote 0
Your profile indicates that you have 2016 version. You can update it to reflect the correct version.
 
Upvote 0
One more question, when I write this formula into a cell from vba I get an '@' before SequenceVBA and that is causing to print only one row, If I manually delete '@' everything works. Any idea where am I going wrong?
outputcell = Sheet1!$D$25
Range(outputcell).Formula = "=SequenceVBA(B3,B4)"

In cell D25 I see formula
=@SequenceVba(B3,B4)

If I click in the cell D25, delete '@' and press enter all the 6 rows with accending order numbers show up.
 
Upvote 0
Please update your Account details to show that you are using 365, rather than 2016.

You need to use Formula2, rather than Formula
But as you have 365, why are you not using the formula JGordon11 showed in cell B7 on post#2
 
Upvote 0
Formula2 did the trick! Reason not using Cell B7 solution because eventually the sequential output data will be replaced by some random data received from an http request. So far I have tested the below code and it all holds good. The next step is to add http-request, parsed-json data in to string data array and then replace it by DataStr() below.

Thanks all for your help, I think I got it from here. Yo guys Rock!

Function SequenceVBA(StartTime As String, EndTime As String)
Dim i, count As Long
Dim DataStr() As String

' StartTime and EndTime to be used in http request body
count = 10
ReDim DataStr(count)

For i = 0 To Count - 1
DataStr(i) = CStr(Rnd())
Next

ReDim a(1 To count, 1 To 1)
For i = 1 To count
a(i, 1) = DataStr(i - 1)
Next
SequenceVBA = a
End Function
 
Upvote 0
Glad we could help & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,788
Messages
6,121,600
Members
449,038
Latest member
Arbind kumar

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