ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Array values with UBound (https://www.excelbanter.com/excel-programming/391186-array-values-ubound.html)

[email protected]

Array values with UBound
 
I am having trouble putting my array values into another sheet. I know
that the correct values are in there but i can't get them to output. I
have two variations. One uses UBound and the other just uses my
counter from building the array.

Public Sub DIFFREC2()
Windows("factor.xls").Activate
Dim MRange As Range
Set MRange = Range(Range("F1"), Range("F1").End(xlDown))
Dim cell As Variant
Dim rw As Integer
Dim x As Integer
Dim y As Integer
Dim arrTicker() As String
rw = 0
For Each cell In MRange
If cell.Value = "PAYD" Then
ReDim Preserve arrTicker(4, rw)
arrTicker(0, rw) = cell.Offset(0, -5).Value
arrTicker(1, rw) = cell.Offset(0, -4).Value
arrTicker(2, rw) = cell.Offset(0, -3).Value
arrTicker(3, rw) = cell.Offset(0, -2).Value
rw = rw + 1
End If
Next cell
Windows("TEST.xls").Activate
Sheets("CBOIPRIN").Select
x = 0
For y = 0 To rw - 1
' or y = 0 To UBound(arrTicker) either doesn't work

Cells(x, 1).Value = arrTicker(0, y)
Cells(x, 2).Value = arrTicker(1, y)
Cells(x, 3).Value = arrTicker(2, y)
Cells(x, 4).Value = arrTicker(3, y)
x = x + 1
Next

End Sub


JE McGimpsey

Array values with UBound
 
Cells(x, 1) will error when x = 0 since Cells() is 1-based.

If I understand what you're doing, you could perhaps use:

Public Sub DIFFREC2()
Dim rDest As Range
Dim rCell As Range

Set rDest = Workbooks("TEST.xls").Sheets("CBOIPRIN").Cells(1, 1)
With Workbooks("factor.xls").Sheets(1)
For Each rCell In .Range(.Range("F1"), _
.Range("F1").End(xlDown))
With rCell
If .Text = "PAYD" Then
rDest.Resize(1, 4).Value = _
.Offset(0, -5).Resize(1, 4).Value
Set rDest = rDest.Offset(1, 0)
End If
End With
Next rCell
End With
End Sub

In article .com,
wrote:

I am having trouble putting my array values into another sheet. I know
that the correct values are in there but i can't get them to output. I
have two variations. One uses UBound and the other just uses my
counter from building the array.

Public Sub DIFFREC2()
Windows("factor.xls").Activate
Dim MRange As Range
Set MRange = Range(Range("F1"), Range("F1").End(xlDown))
Dim cell As Variant
Dim rw As Integer
Dim x As Integer
Dim y As Integer
Dim arrTicker() As String
rw = 0
For Each cell In MRange
If cell.Value = "PAYD" Then
ReDim Preserve arrTicker(4, rw)
arrTicker(0, rw) = cell.Offset(0, -5).Value
arrTicker(1, rw) = cell.Offset(0, -4).Value
arrTicker(2, rw) = cell.Offset(0, -3).Value
arrTicker(3, rw) = cell.Offset(0, -2).Value
rw = rw + 1
End If
Next cell
Windows("TEST.xls").Activate
Sheets("CBOIPRIN").Select
x = 0
For y = 0 To rw - 1
' or y = 0 To UBound(arrTicker) either doesn't work

Cells(x, 1).Value = arrTicker(0, y)
Cells(x, 2).Value = arrTicker(1, y)
Cells(x, 3).Value = arrTicker(2, y)
Cells(x, 4).Value = arrTicker(3, y)
x = x + 1
Next

End Sub



All times are GMT +1. The time now is 05:21 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com