Trouble transferring Array back to worksheet - XL10

dmatter26

New Member
Joined
Jul 29, 2013
Messages
5
I have a workbook that is doing something I don't understand. In several places I am using arrays to transfer/modify information, then placing the array back onto the worksheet, and this works perfectly. The following code... doesn't.

(I've cut out unnecessary code like variable declarations - all my integer items are declared as Long)
Code:
    Dim varA As Variant, varB(10000, 1) As Variant
    
    Set wkDest = Sheets("a")
    erB = 0
    
    For lpTerm = 1 To 4
        Set wkTarg = Sheets(lpTerm)
        With wkTarg
            For lpYear = 1 To 3
                colTarg = Range("cal_Col_Y" & lpYear & "Ofst") + 8  ' just results in a column number, eg. 10
                erTarg = .Cells(.Rows.Count, colTarg).End(xlUp).Row  ' last row of data in above column
                varA = .Range(.Cells(Range("cal_Row_TermDataStart"), colTarg), _
                    .Cells(erTarg, colTarg))
' For each year within each term, varA is set to an array of cells starting at a pre-defined constant row, and going to erTarg.
                For i = 1 To UBound(varA, 1)
                    If varA(i, 1) Like "*_*" Then
                        erB = erB + 1
                        varB(erB, 1) = varA(i, 1)
                    End If
                Next i
            Next lpYear
        End With
    Next lpTerm
Debug.Print UBound(varB, 1) & " - " & erB & " - " & UBound(varB, 2)

This seems to work perfectly. All my debugging shows that varB, with my current data, ends up with 245 entries that conform to the Like "*_*" criteria. I can access every individual item by either varB(x) or varB(x,1) depending on whether I declare varB as one or two dimensional. I can do a simple .cells(1,1) = varB(1,1) with no problem. My debug at the end produces the line:

10000 - 245 - 1

10k cells UBound of varB, single column wide, with 245 data points in a single column.

However, every time I try to transfer varB back to the worksheet in one go, it fails. I've tried:

Code:
.range(.cells(1,1),.cells(UBound(varB,1),1)) = varB

.Range("A1").Resize(UBound(varB, 1), 1) = varB

and several other ways. What happens is just null output, a full column of nothing. However, when I expand my worksheet range to two columns, all the data from varB appears - it is shifted down one column and row. So my data starts at .cells(2,2) instead of .cells(1,1). The data is all perfect, I've verified it left and right.... I just can't figure out why it's getting shifted like this. Any help would be greatly appreciated, I feel like I've taken crazy pills.
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Answered it myself... derp. Forgot that my array was addressed as 0-10,000, changed it to 1-10,000 and it's working fine now.
 
Upvote 0

Forum statistics

Threads
1,223,248
Messages
6,171,011
Members
452,374
Latest member
keccles

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