Range with no values

AlexanderBB

Well-known Member
Joined
Jul 1, 2009
Messages
2,099
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

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
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,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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