+ Reply to Thread
Results 1 to 13 of 13

VBA - Trying to use For / Next Loop macro to go to next dynamic cell range in column 'C'

Hybrid View

  1. #1
    Registered User
    Join Date
    08-27-2012
    Location
    USA
    MS-Off Ver
    Excel 2007
    Posts
    12

    VBA - Trying to use For / Next Loop macro to go to next dynamic cell range in column 'C'

    Currently I have the macro selecting the first active cell range within column C, looks at the value in the first cell, then copies the value to all the remaining cells in that range.
    The problem I'm having is how to properly go to the next cell range to perform the same task for the next active cell range...and so on until end of active cells in column 'C'.
    I've tried using a for loop, but am having no luck(removed from code to remove confusion).
    Any help would be appreciated.

    Thanks in advance.


    Sub Macro4()
        
        Range("C1").Select 
        Selection.End(xlDown).Select 
        
        ActiveCell.CurrentRegion.Rows(2).Select
        
        Range(Selection, Selection.End(xlDown)).Select
        
        Selection.FormulaR1C1 = "=R[-1]C"
                
    End Sub
    Attached Files Attached Files

  2. #2
    Forum Expert
    Join Date
    12-15-2009
    Location
    Chicago, IL
    MS-Off Ver
    Microsoft Office 365
    Posts
    3,177

    Re: VBA - Trying to use For / Next Loop macro to go to next dynamic cell range in column '

    At work. But I wrote something that is going to give you all the combinations. You can treat it as a template

    Sub Test()
    Dim i As Long, j As Long, LRi As Long, LRj As Long, x As Long
    
    LRi = Cells(Rows.Count, 1).End(xlUp).Row
    LRj = Cells(Rows.Count, 2).End(xlUp).Row
    x = 2
    
    For i = 2 To LRi
        For j = 2 To LRj
            Cells(x, 4) = Cells(i, 1)
            Cells(x, 5) = Cells(j, 2)
            x = x + 1
        Next j
    Next i
        
    End Sub
    Attached Files Attached Files

  3. #3
    Registered User
    Join Date
    08-27-2012
    Location
    USA
    MS-Off Ver
    Excel 2007
    Posts
    12

    Re: VBA - Trying to use For / Next Loop macro to go to next dynamic cell range in column '

    Thanks, but did you run my macro with the Book1.xlsm file?
    It will do to the 1st example cell range what I want to perform for the each cell range thereafter for itself. So if the 1st cell in the 1st cell range is 1 then it changes all the other cells in that one range to 1. Then I want it to look at the next cell range, say it's 1st cell starts with 15, it changes the remaining cells of that region to 15. Then it continues doing this for every active cell region until there are no more active cell regions.
    I don't think I follow how yours would apply, please explain if I just don't understand.

    Thanks

  4. #4
    Forum Expert
    Join Date
    12-15-2009
    Location
    Chicago, IL
    MS-Off Ver
    Microsoft Office 365
    Posts
    3,177

    Re: VBA - Trying to use For / Next Loop macro to go to next dynamic cell range in column '

    I don't understand what your macro is trying to do nor your question so I am just guessing. I don't think I am smart enough to help you with your problem.

  5. #5
    Registered User
    Join Date
    08-27-2012
    Location
    USA
    MS-Off Ver
    Excel 2007
    Posts
    12

    Re: VBA - Trying to use For / Next Loop macro to go to next dynamic cell range in column '

    The macro is looking starting at a set column, in this case column 'C'. It then looks for the next active cell region(a cell range of cells that have stuff in them, numbers, letters, whatever), but without having a designated range (ie: Range(C3:C13)). It then looks at the 1st cell in the region(ie Range(C3:13)) so C3 say is equal to 13, but the remaining cells below C3 are different numbers. The macro changes them all to equal the top cell in their region. So in this case C4:C13 would be changed to equal the value 13 regardless of what number they were originally.

    This whole task within this macro, I want to be able to continue to the next active cell regions within column 'C'. So let's say the next active cell region is C23:C45, it looks at C23 value and changes C24:C45 to equal that value....this continues until there is no more active cell regions within the 'C' column. I can get it to work for the first portion, but I think I need some sort of 'For / Next' loop to go to the next instance. That's what I'm having trouble with.

    I hope this explaination helps.

    Help if you can.
    Thanks

  6. #6
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: VBA - Trying to use For / Next Loop macro to go to next dynamic cell range in column '

    Hello cheapkid,

    This macro will work as long as there are breaks (one or more empty rows) between the data. The last value in the range between blank rows is used to fill the range.
    Sub Macro4()
        
        Dim Rng As Range
        Dim RngEnd As Range
        Dim rngArea As Range
        
            Set Rng = Range("C3")
            Set RngEnd = Cells(Rows.Count, Rng.Column).End(xlUp)
            If RngEnd.Row < Rng.Row Then Exit Sub
            
            Set Rng = Range(Rng, RngEnd)
        
            On Error GoTo ExitSub
            Set Rng = Rng.SpecialCells(xlCellTypeConstants)
            
            For Each rngArea In Rng.Areas
                rngArea.Value = rngArea.Cells(Rng.Rows.Count, 1).Value
            Next rngArea
                    
    ExitSub:
        ' Macro will exit here if the range is empty.
        
    End Sub
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  7. #7
    Registered User
    Join Date
    08-27-2012
    Location
    USA
    MS-Off Ver
    Excel 2007
    Posts
    12

    Re: VBA - Trying to use For / Next Loop macro to go to next dynamic cell range in column '

    Leith Ross,

    Thank you for that bit of code. It does work, but I need it to change the cells in the range to the formula I have in my inital code. I did change the direction to go down.
    So I'd like to be able to adjust just the top cell in each range and have all the numbers in that range change accordingly to the same number/value.(after this macro has run)

    This was what I used but couldn't get it to go any further than the first cell region as I said before. Is there any easy way to incorporate this formula matching portion into your code?

    Any Help would be appreciated
    Thanks

        ActiveCell.CurrentRegion.Rows(2).Select ' Goes to the 2nd cell in the region
        
        Range(Selection, Selection.End(xlDown)).Select ' Selects from the 2nd cell to the bottom of the range
        
        Selection.FormulaR1C1 = "=R[-1]C" 'Sets those cells that are selected from the above line to equal the formula given
    Last edited by cheapkid1; 09-05-2012 at 12:25 PM.

  8. #8
    Registered User
    Join Date
    08-27-2012
    Location
    USA
    MS-Off Ver
    Excel 2007
    Posts
    12

    Re: VBA - Trying to use For / Next Loop macro to go to next dynamic cell range in column '

    Can anyone help? I still haven't found any similar code that I can use to continue through the same column and proceed to the next cell region in the macro.
    Any help would be much appreciated.

    Thanks

  9. #9
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: VBA - Trying to use For / Next Loop macro to go to next dynamic cell range in column '

    Hello cheapkid1,

    I believe this macro will work for you. It will start with the active cell. The macro will find the first non empty cell going down. It will then determine the current region for that cell. The current region is defined as a range of contiguous cells that are bordered around by at least one empty row or a side of the worksheet.

    The macro does not reduce a multiple column regions to one only one column. So, the column the active cell is in will control the search for the next current region. The formula is copied starting from the second row in the region down to the last cell of the region until the last cell with a value in the column is reached.
    
    Sub FillRegion()
    
        Dim Cell As Range
        Dim r As Long
        Dim Rng As Range
        Dim RngEnd As Range
        
            Set RngEnd = Cells(Rows.Count, ActiveCell.Column).End(xlUp)
            Set Cell = ActiveCell
            
            For r = Cell.Row To RngEnd.Row
                
                If Cell <> "" Then
                    Set Rng = Cell.CurrentRegion
                
                    If Rng.Rows.Count > 1 Then
                        Set Rng = Rng.Offset(1, 0).Resize(RowSize:=Rng.Rows.Count - 1)
                        Rng.FormulaR1C1 = "=R[-1]C"
                        r = r + Rng.Rows.Count - 1
                    End If
                    
                End If
                
                Set Cell = Cells(r, 1)
                
            Next r
            
    End Sub
    Last edited by Leith Ross; 09-14-2012 at 02:23 PM.

  10. #10
    Registered User
    Join Date
    08-27-2012
    Location
    USA
    MS-Off Ver
    Excel 2007
    Posts
    12

    Re: VBA - Trying to use For / Next Loop macro to go to next dynamic cell range in column '

    Thanks Lieth, I will try this over the weekend. I appreciate your post and information. I will post whether or not I got it to function properly.

  11. #11
    Registered User
    Join Date
    08-27-2012
    Location
    USA
    MS-Off Ver
    Excel 2007
    Posts
    12

    Re: VBA - Trying to use For / Next Loop macro to go to next dynamic cell range in column '

    Leith,

    That last bit of code you put up, will work, however it does not to go the next active cell range. It just does the first cell range....

  12. #12
    Registered User
    Join Date
    08-27-2012
    Location
    USA
    MS-Off Ver
    Excel 2007
    Posts
    12

    Re: VBA - Trying to use For / Next Loop macro to go to next dynamic cell range in column '

    Lieth,

    I tried to incorporate your code, and that's just it, I need it to stay within the confines of column C, if this is possible.??
    I played with the code and did some more research and here's where I am currently with the code. It works perfectly, EXCEPT it will not stop after it's run the 'Do' function within column C.
    I put the Message box in there so you can see how it between going to the next active cell region.
    Any thoughts how I can stop the macro from running outside the "C" column?


    
    
    Sub CopyValuetoRange()
    '
    ' CopyValuetoRange Macro
    
    Dim search_range As Range, Block As Range, last_cell As Range
      Dim first_address$
      Set search_range = ActiveSheet.UsedRange
      Set Block = search_range.Find(what:="*", _
        after:=search_range.SpecialCells(xlCellTypeLastCell), _
        LookIn:=xlValues, searchorder:=xlColumns, searchdirection:=xlDown)
      If Block Is Nothing Then Exit Sub
      
      Set Block = Block.CurrentRegion
      first_address$ = Block.Address
      Do
        Block.Select
        Selection.End(xlDown).Select
        ActiveCell.CurrentRegion.Rows(2).Select
        Range(Selection, Selection.End(xlDown)).Select
        Selection.FormulaR1C1 = "=R[-1]C"
            
        MsgBox "Next Block Range"
        Set last_cell = Block.Cells(Block.Rows.Count)
        Set Block = search_range.FindNext(after:=last_cell).CurrentRegion
      Loop Until Block.Address = first_address$
    
    
    End Sub

  13. #13
    Registered User
    Join Date
    08-27-2012
    Location
    USA
    MS-Off Ver
    Excel 2007
    Posts
    12

    Re: VBA - Trying to use For / Next Loop macro to go to next dynamic cell range in column '

    Is there anyone who might be able to help? I think one of the issues is that I'm using region instead of range. So I need a code that will go to each unknown sized range within Column "C" then 'do something'.
    Please help! Thanks in advance.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1