0

Please could u let me know how i can add dynamic starting position and ending position together in a range .

so basically i have Range("A3:D" & lastRow)

i want something like below .

Range("A:D" &firstrow, & lastRow)

Here the firstrow and last row will be specified by me

avik
  • 271
  • 1
  • 4
  • 14
  • 1
    Possible duplicate of [Error in finding last used cell in VBA](https://stackoverflow.com/questions/11169445/error-in-finding-last-used-cell-in-vba) – ifo20 Aug 10 '18 at 21:04
  • Selecting the start and stop of a range is static not dynamic. Why not just use `Range("A5:D10").Select'? – GMalc Aug 11 '18 at 05:42
  • @ifo20, The question is not clear, I don't think the OP is asking how to find the last row. In the last sentence the OP is wanting to `specify` the start and stop rows for his range in columns A:D, which is basically asking how to write `Range("A5:D10")`. – GMalc Aug 11 '18 at 06:19

1 Answers1

2

Did you try something like this:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Dim firstRow As Integer
    Dim lastRow As Integer
    Dim rng As String

    firstRow = 5
    lastRow = 10
    rng = "A" & CStr(firstRow) & ":" & "D" & CStr(lastRow)
    ActiveSheet.Range(rng).Select
End Sub
dcp
  • 54,410
  • 22
  • 144
  • 164
  • can you help me understand why are you using `Private Sub Worksheet_SelectionChange(ByVal Target As Range)` ? – GMalc Aug 11 '18 at 05:34
  • @GMalc - It is just an example. You would use whatever Excel event method makes sense in your scenario. See here: https://msdn.microsoft.com/en-us/vba/excel-vba/articles/worksheet-object-events – dcp Aug 11 '18 at 10:46