I have a function that creates dynamic arrays based on the content of different sheets- each array is of a different size. I would like to paste the contents of each array into the worksheet on which the function was called. Given that each array is of different size, it seems I cannot use the transpose function. Any suggestions?
Here is the function that creates the arrays:
Function FindConstituents(ByRef ys As Worksheet) As String()
Dim a() As String
Dim size As Integer
Dim i As Integer
Dim j As Integer
size = 0
Dim row As Integer
row = 2
ReDim Preserve a(size)
While ys.Cells(row, 4) <> ""
If IsInTable(ys.Cells(row, 4), ys, 2) Then
ReDim Preserve a(size)
a(size) = ys.Cells(row, 4)
size = size + 1
End If
row = row + 1
Wend
size = size + 1
<<Code to paste contents of a() onto a place in the worksheet>>
FindConstituents = a
End Function
You would use Resize, something like to get the results in one column:
ys.Range("G1").Resize(Ubound(a)+1).Value = Application.Transpose(a)
If you do not want to use Transpose then you will need to iterate through a.
Dim i as long
For i = Lbound(a) to Ubound(a)
ys.Range("G1").Offset(i).Value = a(i)
Next i
Edit just realized you probably are calling this from the worksheet. This will need to be called from a sub that does the pasting back to the sheet a function called by a worksheet cannot affect the values of other cells.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.