Range with no values

AlexanderBB

Well-known Member
Joined
Jul 1, 2009
Messages
2,052
Office Version
  1. 2019
  2. 2010
Platform
  1. Windows
I can't find the exact thread here where I was given
VBA Code:
Set cr = .Range(.Range("Y" & Row), .Range("CQ" & Row).End(xlToLeft))
This catches all values in row from Y to CP, ignoring blanks. I've been using it and getting perfect results until a situation
where nothing was in that row. Say it's row 53, I get
$X$53:$Y$53
The X column is wrong. And I'm unsure how best to correct, and still rely on that instruction.
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
See if this give you some ideas.

Rich (BB code):
    Set sht = ActiveSheet
    
    Dim LastCol As Long
    With sht
        LastCol = .Range("CQ" & Row).End(xlToLeft).Column
        If LastCol >= Columns("Y").Column Then
            Set cr = .Range(.Range("Y" & Row), .Range("CQ" & Row).End(xlToLeft))
            ' OR you could use
            ' Set cr = .Range(.Range("Y" & Row), .Cells(Row, LastCol))
        Else
            ' What do you want it to do if there is nothing there
            MsgBox "No data in Row for nominated range"
        End If
    End With
 
Upvote 0
That's very helpful and has given me a solution. LastCol is really all I need.
It would be better as chars, so I'm using = Left(Cells(1, Val(intNumber)).Address(0, 0), 2 + (Val(intNumber) <= 26))
Where intNumber is your LastCol.
Unless there's something better?
 
Upvote 0
Thanks Alex, Split was my first thought but now I have
VBA Code:
 LastCol = (.Range("CQ" & index).End(xlToLeft).Column)
        If LastCol > 24 Then
            For Each ITEM In .Range("Y" & index, colLetter(LastCol) & index)
                Debug.Print ITEM & Space(1);
            Next
        End If
and don't need to define a range.
ColLetter changes a number to letters. I don't know how to use integers in .Range()
 
Upvote 0

Forum statistics

Threads
1,223,230
Messages
6,170,883
Members
452,364
Latest member
springate

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