Error using transpose

AlexanderBB

Well-known Member
Joined
Jul 1, 2009
Messages
1,940
Office Version
  1. 2019
  2. 2016
Platform
  1. Windows

Using a suggestion above I'm trying to put the values in a range into a csv string.
But I get error 5 invalid call on the transpose line.
VBA Code:
Function CROK(excelRow)
 Dim cc As Integer
    Dim cr As Range
    Dim strData As String
    Dim wsf As WorksheetFunction: Set wsf = Application.WorksheetFunction
    cc = 95
    With Worksheets("Sheet1")
        Do
            cc = cc - 1
        Loop Until .Cells(excelRow, cc) > " 'get first populated cell moving from column CP back
      
        Set cr = .Range("Y" & excelRow & ":" & colLetter(cc) & excelRow)
        strData = Join(wsf.Transpose(cr.Value), ",")
        Stop
    End With
End function
What am I doing wrong ?
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
Join function only accepts single D array.
When the array populated from single row with multiple columns (2D array) and need to convert it to a single D array, need to transpose twice.
e.g
Code:
Function CROK(excelRow As Long) As String
    Dim C&, wf As WorksheetFunction
    Set wf = WorksheetFunction
    With Sheets("sheet1")
        C = .Evaluate("max(if(" & excelRow & ":" & excelRow & "<>"""",column(1:1)))")  '<-- find last used column index
        CROK = Join(wf.Transpose(wf.Transpose(.Range("y" & excelRow, .Cells(excelRow, C)))), ",")
        'or could use Evaluate method like
        'CROK = Join(.Evaluate(.Range("y" & excelRow, .Cells(excelRow, C)).Address & "&"""""), ",")
    End With
End Function
 
Upvote 0
Great, thanks Fuji.
The C= line returns the last cell, but data may exist after col CP. I need the last used cell in Y-CP.
Maybe there still a way to use .Evaluate without my loop?
Once all working I was going to handle nothing in Y-CP.
 
Upvote 0
I found you can't join if C is 25 meaning one value only in col Y.
 
Upvote 0
Do you mean like this?

Rich (BB code):
C = .Evaluate("max(if(y" & excelRow & ":cp" & excelRow & "<>"""",column(y:cp)))")  '<-- find last used column index within col.Y:CP
 
Upvote 0
I found you can't join if C is 25 meaning one value only in col Y.
Then
Code:
Function CROK(excelRow As Long) As String
    Dim C&, wf As WorksheetFunction, x
    Set wf = WorksheetFunction
    With Sheets("sheet1")
        C = .Evaluate("max(if(y" & excelRow & ":cp" & excelRow & "<>"""",column(y:cp)))")  '<-- find last used column index
        If C > Columns("y").Column Then
            CROK = Join(wf.Transpose(wf.Transpose(.Range("y" & excelRow, .Cells(excelRow, C)))), ",")
            'or could use Evaluate method like
            CROK = Join(.Evaluate(.Range("y" & excelRow, .Cells(excelRow, C)).Address & "&"""""), ",")
        ElseIf C = Columns("y").Column Then
            CROK = .Range("y" & excelRow)
        End If
    End With
End Function
 
Upvote 0
Yes indeed.. many thanks :)
I like the way you handled c in msg 5
 
Upvote 0
Glad you like it and thank for the feedback.

Yep, there's more than one way to skin a cat.
 
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,120
Members
451,399
Latest member
alchavar

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