0
   Range("A1").Select
   Selection.End(xlDown).Select
   ActiveCell.Offset(0, 3).Select

   Dim Q As String
   
   Q = ActiveCell.Address
   
   Range(" Q:D10438").Select
   Selection.ClearContents

The Variable Q is taking the value of the active cell, but i am unable to include it in range

braX
  • 11,506
  • 5
  • 20
  • 33
  • 1
    If you're trying to find the last row in column A, [see this approach](https://stackoverflow.com/questions/11169445/error-in-finding-last-used-cell-in-excel-with-vba). – BigBen Dec 04 '20 at 20:53

2 Answers2

0

The variable cannot be included in the quotes. Also you should avoid using Select. A simpler version would be:

   Dim Q As String
   Q = Range("A1").End(xlDown).Offset(0, 3).Address
   Range(Q & ":D10438").ClearContents
Darrell H
  • 1,876
  • 1
  • 9
  • 14
0

Range Object

  • How to avoid using Select

  • When defining a range from two cells, the Range object 'allows' us to use cell range objects or cell range addresses or a mix of them:

    Dim rng as range
    Set rng = Range("A1","D4")
    Set rng = Range(Range("A1"), Range("D4"))
    ' Mix
    Set rng = Range("A1", Range("D4"))
    Set rng = Range(Range("A1"), "D4")
    

    to get the range:

    $A$1:$D$4
    
  • We can apply the second 'mix solution' to the question:

    ' First cell range
    Range("A1").End(xlDown).Offset(0, 3)
    ' Second cell address
    "D10438"
    

    to get the result:

    Range(Range("A1").End(xlDown).Offset(0, 3), "D10438").ClearContents
    
VBasic2008
  • 44,888
  • 5
  • 17
  • 28