Dump array onto worksheet

krice1974

Active Member
Joined
Jul 3, 2008
Messages
422
Hi all. I searched help and I'm sure this has been answered hundreds of times here but I can't seem to make the given solutions apply. Anyway.

I'd like to "dump" an array onto a worksheet. It's called MyArray, it's varying in length, 7 "columns" wide, and I know the uppermost left cell I'd like to dump to.

Many thanks in advance...

Kevin
 
Greetings Krice,

Hopefully I didn't stray too far from where you wanted to end up. I tried to keep most of the names the same or close, but found I was having trouble with stuff like UGQFinalRowFixed as an object (sheet) and UGQNewRow as a number (long). Hopefully it'll be a decently easy read for you; let me know if not.

I believe I commented the code up pretty well, but post back if I made anything unclear.

Code:
Option Explicit
 
Sub ex_resp()
 
Dim _
lngLastRow          As Long, wksSource           As Worksheet, _
lngRow              As Long, rngDestination      As Range, _
lngUpper            As Long, strRatingType       As String, _
lngLower            As Long, DataArray()         As Variant, _
lngInnerRow         As Long, _
lngUbound           As Long
    
    
    Set wksSource = ThisWorkbook.Worksheets("BriefingAnalystUpsDwnsQuery")
    
    With wksSource
        
        '// Find the last row used as you were doing                        //
        lngLastRow = .Cells(Rows.Count, 1).End(xlUp).Row
        
        '// Initially dimension our array as 8 x 1.  While this seems       //
        '// backwards, this will allow us to keep the values being added    //
        '// preserved as the second dimension resizes (grows)               //
        ReDim DataArray(0 To 7, 0)
        
        '// I started the loop out at 4 rather than 1.  If it did find      //
        '// "Company" in any row less than four, this would cause an error  //
        '// when looking three rows up (as three rows up would be zero or   //
        '// less).                                                          //
        For lngRow = 4 To lngLastRow
            If .Cells(lngRow, 1).Value = "Company" Then
                
                Select Case .Cells(lngRow - 3, 1).Value
                    Case "Upgrades"
                        strRatingType = "Upgrades"
                    Case "Downgrades"
                        strRatingType = "Downgrades"
                    Case "Coverage Initiated"
                        strRatingType = "Initiated"
                    Case "Coverage Reit/Price Tgt Changed*"
                        strRatingType = "TargetChange"
                End Select
                
                '// Since lngRow will be the row we found "Company" on, we  //
                '// should be able to figure the first row to start grabbing//
                '// as well as the bottom of the CurrentRegion.             //
                lngUpper = lngRow + 1
                lngLower = wksSource.Cells(lngUpper, 1).End(xlDown).Row
                
                '// Now since we know where we stopped grabbing data,       //
                '// rather than having our outer loop                       //
                '// (For lngRow = 4 To lngLastRow) start looking only one   //
                '// row below the last "Company", we'll just adjust up      //
                '// lngRow here.                                            //
                lngRow = lngLower + 1
                
                '// ...Back to our CurrentRegion, we'll loop thru and build //
                '// our array.                                              //
                For lngInnerRow = lngUpper To lngLower
                
                    '// We find out what the current upper bound of the     //
                    '// second dimension is...                              //
                    lngUbound = UBound(DataArray(), 2)
                    
                    '// ...and assign values to ea element of the first     //
                    '// dimension at this position;                         //
                    DataArray(0, lngUbound) = .Cells(lngInnerRow, 1).Value
                    DataArray(1, lngUbound) = .Cells(lngInnerRow, 2).Value
                    DataArray(2, lngUbound) = .Cells(lngInnerRow, 3).Value
                    DataArray(3, lngUbound) = .Cells(lngInnerRow, 4).Value
                    DataArray(4, lngUbound) = .Cells(lngInnerRow, 5).Value
                    DataArray(5, lngUbound) = .Cells(lngInnerRow, 6).Value
                    DataArray(6, lngUbound) = .Cells(lngInnerRow, 7).Value
                    
                    '// ...excepting of course the val we needed to snag    //
                    '// from above the "table"/CurrentRegion.               //
                    DataArray(7, lngUbound) = strRatingType
                    
                    '// Now we'll redimension our array for the next loop.  //
                    '// The last trip thru our loop will add an unnecessary //
                    '// element to the second dimension, which we'll strip  //
                    '// when done looping.                                  //
                    ReDim Preserve DataArray(0 To 7, UBound(DataArray(), 2) + 1)
                Next
                
            End If
        Next lngRow
        
    End With
 
    '// Delete the last element from ea of the elements in the first        //
    '// dimension.                                                          //
    ReDim Preserve DataArray(0 To 7, UBound(DataArray(), 2) - 1)
    
    '// We've got our data, so off to the destination sheet.                //
    With ThisWorkbook.Worksheets("BriefingAnalystUpsDwns")
    
        '// So as if theres stuff from a prior day/period, clear.  I left   //
        '// a header row.                                                   //
        .Range("A2:A" & .Cells(Rows.Count, 1).End(xlUp).Row).EntireRow.Delete
        
        '// Rather than loop thru ea value in our, figure out the size that //
        '// the array/range will be once it is transposed, and set a range  //
        '// to the same size.                                               //
        Set rngDestination = .Range(.Cells(2, 1), _
                                    .Cells(UBound(DataArray(), 2) + 2, 8))
        
        '// Wack it into place and size the columns                         //
        rngDestination = Application.WorksheetFunction.Transpose(DataArray)
        .Columns("A:H").EntireColumn.AutoFit
    End With
End Sub

Hope this helps,

Mark
 
Upvote 0

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
Mark, thanks for being so generous with your time. I'll see if I can get that to work today. Thanks again...

Kevin
 
Upvote 0

Forum statistics

Threads
1,215,043
Messages
6,122,812
Members
449,095
Latest member
m_smith_solihull

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