How can I write array to incremental columns (.cells not clear to me)

GeeWhiz7

Board Regular
Joined
Nov 22, 2021
Messages
214
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
Hi,
Below is a nested loop that 1) loops through a list of worksheet names, 2) gets the last row of data from each sheet and 3) is supposed to aggregate each row of data into a DashBoard sheet by transposing it.
I am trying to get each subsequent bit of data to get pasted into the next column, starting at C4 going to D4, E4, etc. I'm not exactly clear why I get a "Range of Object _Worksheet" failed error.
1660573356441.png


VBA Code:
Option Explicit

Sub Dash1()
    Dim ws, Dash As Worksheet
    Dim wsNames As Variant, wsName, nUms, dAtes As Variant
    Dim lr As Long, lc As Long, i As Long, j As Long
   
    wsNames = ThisWorkbook.Worksheets("Comps Sheet Names").Range("C1").CurrentRegion.Value
    Set Dash = Worksheets("DashBoard")
    Dash.Cells.Clear
   
    '****Starting location to paste array values eq. to "C4"
   
    i = 4 'row
    j = 3 'col
   
    For Each wsName In wsNames
        Set ws = ThisWorkbook.Worksheets(wsName)
       
        With ws
            lr = .Cells(Rows.Count, 1).End(xlUp).Row
            lc = .Cells(1, Columns.Count).End(xlToLeft).Column
            nUms = .Range(.Cells(lr, 3), .Cells(lr, lc)).Value
            dAtes = .Range(.Cells(lr, 1), .Cells(lr, lc)).Value

            With Dash  '#####Write nUms array(s) to DashBoard
                '######This doesn't work######
                .Range(i,j).Resize(UBound(nUms, 2), UBound(nUms, 1)).Value = Application.WorksheetFunction.Transpose(nUms)

                '######This doesn't work either######
                .Range(.Cells(i, j)).Resize(UBound(nUms, 2), UBound(nUms, 1)).Value = Application.WorksheetFunction.Transpose(nUms)

                '######But if I change to this, it will write correctly to C4, but I can't increment to the next column (E4) it######
                .Range("C" & i).Resize(UBound(nUms, 2), UBound(nUms, 1)).Value = Application.WorksheetFunction.Transpose(nUms)

            End With
        End With
        i = i + 1
        j = j + 1
    Next wsName

End Sub
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
Untested, but none of these should throw an error...

VBA Code:
Sub Dash1()
    Dim ws, Dash As Worksheet
    Dim wsNames As Variant, wsName, nUms, dAtes As Variant
    Dim lr As Long, lc As Long, i As Long, j As Long
   
    wsNames = ThisWorkbook.Worksheets("Comps Sheet Names").Range("C1").CurrentRegion.Value
    Set Dash = Worksheets("DashBoard")
    Dash.Cells.Clear
   
    '****Starting location to paste array values eq. to "C4"
   
    i = 4 'row
    j = 3 'col
   
    For Each wsName In wsNames
        Set ws = ThisWorkbook.Worksheets(wsName)
       
        With ws
            lr = .Cells(Rows.Count, 1).End(xlUp).Row
            lc = .Cells(1, Columns.Count).End(xlToLeft).Column
            nUms = .Range(.Cells(lr, 3), .Cells(lr, lc)).Value
            dAtes = .Range(.Cells(lr, 1), .Cells(lr, lc)).Value

            With Dash  '#####Write nUms array(s) to DashBoard
                '######This doesn't work######
                .Cells(i, j).Resize(UBound(nUms, 2), UBound(nUms, 1)).Value = Application.WorksheetFunction.Transpose(nUms)

                '######This doesn't work either######
                .Cells(i, j).Resize(UBound(nUms, 2), UBound(nUms, 1)).Value = Application.WorksheetFunction.Transpose(nUms)

                '######But if I change to this, it will write correctly to C4, but I can't increment to the next column (E4) it######
                .Range("C" & i).Resize(UBound(nUms, 2), UBound(nUms, 1)).Value = Application.WorksheetFunction.Transpose(nUms)

            End With
        End With
        i = i + 1
        j = j + 1
    Next wsName

End Sub
 
Upvote 0
Solution
Run this
VBA Code:
Debug.Print .Range(I, J).Address

And you'll get the same error. Using type long as arguments for the range property is not legal. You are conflating it with the way the cell property works.
 
Upvote 0
Untested, but none of these should throw an error...

VBA Code:
Sub Dash1()
    Dim ws, Dash As Worksheet
    Dim wsNames As Variant, wsName, nUms, dAtes As Variant
    Dim lr As Long, lc As Long, i As Long, j As Long
  
    wsNames = ThisWorkbook.Worksheets("Comps Sheet Names").Range("C1").CurrentRegion.Value
    Set Dash = Worksheets("DashBoard")
    Dash.Cells.Clear
  
    '****Starting location to paste array values eq. to "C4"
  
    i = 4 'row
    j = 3 'col
  
    For Each wsName In wsNames
        Set ws = ThisWorkbook.Worksheets(wsName)
      
        With ws
            lr = .Cells(Rows.Count, 1).End(xlUp).Row
            lc = .Cells(1, Columns.Count).End(xlToLeft).Column
            nUms = .Range(.Cells(lr, 3), .Cells(lr, lc)).Value
            dAtes = .Range(.Cells(lr, 1), .Cells(lr, lc)).Value

            With Dash  '#####Write nUms array(s) to DashBoard
                '######This doesn't work######
                .Cells(i, j).Resize(UBound(nUms, 2), UBound(nUms, 1)).Value = Application.WorksheetFunction.Transpose(nUms)

                '######This doesn't work either######
                .Cells(i, j).Resize(UBound(nUms, 2), UBound(nUms, 1)).Value = Application.WorksheetFunction.Transpose(nUms)

                '######But if I change to this, it will write correctly to C4, but I can't increment to the next column (E4) it######
                .Range("C" & i).Resize(UBound(nUms, 2), UBound(nUms, 1)).Value = Application.WorksheetFunction.Transpose(nUms)

            End With
        End With
        i = i + 1
        j = j + 1
    Next wsName

End Sub
Now its real obvious I don't know my .Range/.Cells/.Cell vernacular and uses very much.
But, this works, thank you igold!
 
Upvote 0
You're welcome. Thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,915
Members
452,366
Latest member
TePunaBloke

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