Consulting

Results 1 to 4 of 4

Thread: Find Empty Cell in Range From Bottom Up

  1. #1
    VBAX Mentor
    Joined
    Jan 2006
    Posts
    323
    Location

    Find Empty Cell in Range From Bottom Up

    Good evening,

    I have a chart on a spreadsheet in which I have 5 columns (G17:G42), (I17:I42), (K17:K41), (M17:M42), (O17:O42) that I want to populate. I am able to populate them from top to bottom, over to the right, top to bottom, over to the right, top to bottom and so on. What I am trying to do is now is populate the chart from bottom to top, over to the left, bottom to top, over to the left and so on.

    This is a snippet of the code I am using to do it from top to bottom (first column) which works
    For Each m In Range("$G$17:$G$42")
          LastEntry = "Empty"
          If m.Value = "" Then
               Exit For
             Else
               LastEntry = "Full"
          End If
       Next m
    I cannot figure out how to modify it to go bottom to top. I've tried to modify it multiple ways but can't figure it out.

    Attached is a shortened spreadsheet with what I'm doing. Sheet 1 is the bottom to top routine I can't figure out. Sheet 2 is the top to bottom routine that works.

    Thank you in advance.
    Gary
    Attached Files Attached Files

  2. #2
    Moderator VBAX Master georgiboy's Avatar
    Joined
    Mar 2008
    Location
    Kent, England
    Posts
    1,194
    Location
    You will need to make some backwards loops and call the cell through the use of 'Cell' and not 'Range', see below to loop through your range backwards. (P.S. Do not use merged cells unless your life depends on it)

    Sub test()    
        Dim x As Long, y As Long
        
        For y = 15 To 7 Step -2
            For x = 42 To 17 Step -1
                Cells(x, y).Select
            Next x
        Next y
    End Sub
    You could use something like the above to create a function to write to the range backwards, you could then just call the function each time you wish to write to your backwards tables. Function example below:
    Sub test()    
        RevCell 42.6666
    End Sub
    
    
    Function RevCell(str)
        Dim x As Long, y As Long
        
        For y = 15 To 7 Step -2
            For x = 42 To 17 Step -1
                If Cells(x, y) = vbNullString Then
                    Cells(x, y) = str
                    Exit Function
                End If
            Next x
        Next y
    End Function
    Hope this helps
    Last edited by georgiboy; 05-03-2022 at 10:46 PM.
    Click here for a guide on how to add code tags
    Click here for a guide on how to mark a thread as solved
    Click here for a guide on how to upload a file with your post (you may need to click 'Go Advanced')

    Excel 365, Version 2403, Build 17425.20146

  3. #3
    VBAX Mentor
    Joined
    Jan 2006
    Posts
    323
    Location
    @ georgiboy,

    I modified your function to my needs and it works perfectly.

    Thank you very much.

  4. #4
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,057
    Location
    Quote Originally Posted by georgiboy View Post
    (P.S. Do not use merged cells unless your life depends on it)
    This is a function (Merged Cells) within excel which needs to be removed. It causes more problems than it solves.
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

Posting Permissions

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