+ Reply to Thread
Results 1 to 13 of 13

Fill up the current cell with the value of the previous cell is the current cell is empty

Hybrid View

  1. #1
    Registered User
    Join Date
    11-02-2013
    Location
    Cebu, Philippines
    MS-Off Ver
    Excel 2007
    Posts
    21

    Question Fill up the current cell with the value of the previous cell is the current cell is empty

    Hi,

    I wrote the code below. It didn't given an error but wouldn't work either. My goal is to fill up the activecell with the value of the previous cell if the activecell is empty. For example if A2 is banana, A3 is apple and A4 is blank, then the value of A4 should be apple. This process will continue until the lastrow in variable lRow2. I was thining of using do-while-loop but i thought it would be simple to use for-loop.

    Dim lRow2 As Double
        lRow2 = Columns(4).Cells.SpecialCells(xlCellTypeConstants).Count
        
    MsgBox lRow2
        
    For i = 3 To lRow2
        If Cells(lRow2, 1).Value = Empty Then Cells(lRow2, 1).Value = Cells(lRow2 - 1, 1).Value       
    Next i

  2. #2
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Fill up the current cell with the value of the previous cell is the current cell is em

    Most likely the cells are not empty but blank null string.

    Sub FillDown()
    Dim LR As Long
    
    LR = ActiveSheet.UsedRange.Rows.Count
    Application.ScreenUpdating = False
    
    For i = 2 To LR
            If Cells(i, 4).Value = "" Then
                Cells(i, 4).Value = Cells(i - 1, 4).Value
        End If
    Next i
    
    Application.ScreenUpdating = True
    End Sub
    Last edited by AB33; 12-03-2013 at 05:48 AM.

  3. #3
    Forum Contributor
    Join Date
    11-21-2013
    Location
    zimbabwe
    MS-Off Ver
    Excel 2003
    Posts
    125

    Re: Fill up the current cell with the value of the previous cell is the current cell is em

    Select cell A2 (first cell in data range) then run macro as below
    Sub emp()
    Do
        If ActiveCell = "" Then
            ActiveCell = ActiveCell.Offset(-1)
            ActiveCell.Offset(1).Select
        Else
            ActiveCell.Offset(1).Select
        End If
    Loop Until ActiveCell = "stop"
    End Sub

  4. #4
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Fill up the current cell with the value of the previous cell is the current cell is em

    lRow2 = Columns(4).Cells.SpecialCells(xlCellTypeConstants).Count
    Counts the number of non empty cells which is not the same as you want to loop down until the last non empty cell in a column D.

    lRow2 = Range("D" & Rows.Count).End(xlUp).Row
    Should do

  5. #5
    Registered User
    Join Date
    11-02-2013
    Location
    Cebu, Philippines
    MS-Off Ver
    Excel 2007
    Posts
    21

    Re: Fill up the current cell with the value of the previous cell is the current cell is em

    i used a message box to know the value of lrow2 and it is just fine. I've revised my code (below) but still wouldnt work



    Sub fillblanks()
    Dim lr As Double
        lr = Range("D" & Rows.CountLarge).End(xlUp).Row - 1
    
    For i = 2 To lr
        If IsEmpty(Cells(lr, 1).Value) Then Cells(lr, 1).Value = Cells(lr - 1, 1).Value
        Next i
    End Sub
    And here's my data:Attachment 281717


    Quote Originally Posted by AB33 View Post
    lRow2 = Columns(4).Cells.SpecialCells(xlCellTypeConstants).Count
    Counts the number of non empty cells which is not the same as you want to loop down until the last non empty cell in a column D.

    lRow2 = Range("D" & Rows.Count).End(xlUp).Row
    Should do

  6. #6
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Fill up the current cell with the value of the previous cell is the current cell is em

    lr = Range("D" & Rows.CountLarge).End(xlUp).Row - 1
    looks at the last row minus in column D, while your other lines loop through column A

    If IsEmpty(Cells(lr, 1).Value) Then Cells(lr, 1).Value = Cells(lr - 1, 1).Value
    There is a problem with the attachment. I am unable to down load it.

  7. #7
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Fill up the current cell with the value of the previous cell is the current cell is em

    Hi, danex,

    maybe try
    On Error Resume Next
    Range("A2:A" & Cells(Rows.Count, "D").End(xlUp).Row).SpecialCells(xlCellTypeBlanks).FormulaR1C1 = "=R[-1]C"
    With Columns("B:B")
      .Value = .Value
    End With
    If Err <> 0 Then
      MsgBox "No blanks in Column A"
    End If
    Ciao,
    Holger
    Use Code-Tags for showing your code: [code] Your Code here [/code]
    Please mark your question Solved if there has been offered a solution that works fine for you

  8. #8
    Registered User
    Join Date
    11-02-2013
    Location
    Cebu, Philippines
    MS-Off Ver
    Excel 2007
    Posts
    21

    Re: Fill up the current cell with the value of the previous cell is the current cell is em

    Thanks. But I figured out I should have used for-each-in a range. I had no success with do-while and for-1 to n.
    Quote Originally Posted by HaHoBe View Post
    Hi, danex,

    maybe try
    On Error Resume Next
    Range("A2:A" & Cells(Rows.Count, "D").End(xlUp).Row).SpecialCells(xlCellTypeBlanks).FormulaR1C1 = "=R[-1]C"
    With Columns("B:B")
      .Value = .Value
    End With
    If Err <> 0 Then
      MsgBox "No blanks in Column A"
    End If
    Ciao,
    Holger

  9. #9
    Registered User
    Join Date
    11-02-2013
    Location
    Cebu, Philippines
    MS-Off Ver
    Excel 2007
    Posts
    21

    Re: Fill up the current cell with the value of the previous cell is the current cell is em

    i used column only to find the last row with value - just the row number to bused in looping. The attachment is just an image.

    Quote Originally Posted by AB33 View Post
    lr = Range("D" & Rows.CountLarge).End(xlUp).Row - 1
    looks at the last row minus in column D, while your other lines loop through column A

    If IsEmpty(Cells(lr, 1).Value) Then Cells(lr, 1).Value = Cells(lr - 1, 1).Value
    There is a problem with the attachment. I am unable to down load it.

  10. #10
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Fill up the current cell with the value of the previous cell is the current cell is em

    The attachment is just an image.
    Can not even see the image.
    Please refer to my post #2.
    If you had attached a sample, It would have been resolved two days. We now are on the 3rd day.

  11. #11
    Registered User
    Join Date
    11-02-2013
    Location
    Cebu, Philippines
    MS-Off Ver
    Excel 2007
    Posts
    21

    Re: Fill up the current cell with the value of the previous cell is the current cell is em

    Here's the image data.jpg

  12. #12
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Fill up the current cell with the value of the previous cell is the current cell is em

    A Picture is not much help. You can not test a picture.
    To test if column A cells are blank, with out a code:
    CRTL plus G will take you to "go special" then click blank and see if any find any cells highlighted.

  13. #13
    Registered User
    Join Date
    11-02-2013
    Location
    Cebu, Philippines
    MS-Off Ver
    Excel 2007
    Posts
    21

    Re: Fill up the current cell with the value of the previous cell is the current cell is em

    Here is the spreadsheet via google doc https://drive.google.com/file/d/0B1k...it?usp=sharing

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Auto-fill the current date if the previous row was empty
    By lesoies in forum Excel General
    Replies: 5
    Last Post: 09-22-2012, 11:44 AM
  2. Format the previous cell based on the value of the current cell.
    By IamHarish in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 12-12-2011, 05:59 PM
  3. Excel 2007 : Format previous cell based on current cell
    By IamHarish in forum Excel General
    Replies: 5
    Last Post: 12-05-2011, 06:06 PM
  4. Replies: 3
    Last Post: 11-17-2010, 04:28 AM
  5. [SOLVED] formula, move to previous cell when the current cell=0 or empty
    By osama amer in forum Excel General
    Replies: 0
    Last Post: 05-29-2006, 07:25 AM

Tags for this Thread

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