1

My Sheet1 contains data in the top 6 rows x 5 columns.

I added a CommandButton to manipulate this data, but when I try to use Sheet1.UsedRange it is returning a Range that is 121 rows x 44 columns.

I've inspected that cell in Sheet1 and it doesn't contain any data.

Why is Sheet1.UsedRange being weird? Is there a work around?

EDIT: I was using Sheet1.UsedRange in these two ways:

  1. Set rng = Sheet1.UsedRange.Rows(aCell.Row)
  2. For Each C in Sheet1.UsedRange

Is my only hope of reproducing these huge Find statements?

Community
  • 1
  • 1
Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288

2 Answers2

1

Further to the comments, is this what you are trying?

Sub Sample()
    Dim ws As Worksheet
    Dim lCol As Long, rw As Long

    '~~> Change this to the relevant worksheet
    Set ws = ThisWorkbook.Sheets("Sheet1")

    With ws
        rw = 2 '<~~ Known row

        '~~> Find the last column which has data in that row
        lCol = .Cells(rw, .Columns.Count).End(xlToLeft).Column

        '~~> Address of known row which has data
        Debug.Print .Range("A" & rw & ":" & ReturnName(lCol) & rw).Address
    End With
End Sub

'~~> Function to return Column letter from column number
Function ReturnName(ByVal num As Integer) As String
    ReturnName = Split(Cells(, num).Address, "$")(1)
End Function

And if you want to set it as a range then like this

Sub Sample()
    Dim ws As Worksheet
    Dim lCol As Long, rw As Long
    Dim rng As Range
    Dim sAddress As String

    '~~> Change this to the relevant worksheet
    Set ws = ThisWorkbook.Sheets("Sheet1")

    With ws
        rw = 2 '<~~ Known row

        '~~> Find the last column which has data in that row
        lCol = .Cells(rw, .Columns.Count).End(xlToLeft).Column

        '~~> Address of known row which has data
        sAddress = .Range("A" & rw & ":" & ReturnName(lCol) & rw).Address

        '~~> Set Range
        Set rng = .Range(sAddress)
    End With
End Sub

'~~> Function to return Column letter from column number
Function ReturnName(ByVal num As Integer) As String
    ReturnName = Split(Cells(, num).Address, "$")(1)
End Function

Followup From Comments

Sub Sample()
    Dim ws As Worksheet
    Dim lCol As Long, rw As Long
    Dim rng As Range

    '~~> Change this to the relevant worksheet
    Set ws = ThisWorkbook.Sheets("Sheet1")

    With ws
        rw = 2 '<~~ Known row

        '~~> Find the last column which has data in that row
        lCol = .Cells(rw, .Columns.Count).End(xlToLeft).Column

        '~~> Set Range
        Set rng = .Range(.Cells(rw, 1), .Cells(rw, lCol))

        Debug.Print rng.Address

    End With
End Sub
Siddharth Rout
  • 147,039
  • 17
  • 206
  • 250
-1

Excel seems to keep the info about what range already was used, not just the part that is in use right now.

This should do the trick:

Dim oRng as Range
WIth Sheets(1)
    Set oRng = .Range(.Range("A1").End(xlDown), .Range("A1").End(xlToRight))
End With
  • 1
    [This](http://stackoverflow.com/questions/11169445/error-finding-last-used-cell-in-vba) is actually the canonical way to find the last cell (and hence construct a range) in any range :) – David Zemens Oct 30 '14 at 13:26
  • 3
    This is the wrong way to find your range. What if there are blank cells in between ? This will also give you undesired results if only A1 has data. You will end up selecting the entire worksheet cells – Siddharth Rout Oct 30 '14 at 13:53