Consulting

Results 1 to 3 of 3

Thread: Better to user ReDim ?

  1. #1
    VBAX Regular
    Joined
    Aug 2006
    Location
    New York City
    Posts
    23
    Location

    Better to user ReDim ?

    Hello, the following seems like I could use ReDim statement. I am looping/counting through a vertical range and stopping when the value in the cell to the left = a particular time, then using that count I set the range. (This is part of a bigger loop) The challenge is the Count will vary with each iteration. Is there any way using redim to assess the number in range?

    [VBA]Dim TestOffset As Long, TimeT As Long ' I've reformatted time as integer

    Do Until ActiveCell(1, 0).Value > (TimeT + TestOffset) - 1
    ActiveCell.Offset(1, 0).Select
    Count = Count + 1
    Loop
    ActiveCell.Offset(-Count, 0).Select
    Set rng1 = Range(ActiveCell, ActiveCell.Offset(Count))[/VBA]

    Many thanks.......RO

  2. #2
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    Hi there,

    ReDim is for array's, which you are not using here. You could use an array here. You could also not use the Select statement. I can't see all of your code, but maybe you could post a real world example of what you are trying to do along with the full code. Maybe we could get you some working code.

  3. #3
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Something like
    [vba]Sub SetRange()
    Dim TestOffset As Long, TimeT As Long ' I've reformatted time as integer
    Dim rng1 As Range, cel as Range
    For Each cel In Range(ActiveCell, ActiveCell.End(xlDown))
    If cel > (TimeT + TestOffset) - 1 Then Exit For
    Next
    Set rng1 = Range(ActiveCell, cel)
    rng1.Select
    End Sub[/vba]
    or to check the column to the left for values (if that's what you mean)
    [VBA]
    For Each cel In Range(ActiveCell, ActiveCell.End(xlDown))
    If cel.Offset(, -1) > (TimeT + TestOffset) - 1 Then Exit For
    Next

    [/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •