+ Reply to Thread
Results 1 to 8 of 8

Find and replace based on condition macro

Hybrid View

  1. #1
    Registered User
    Join Date
    09-23-2010
    Location
    US
    MS-Off Ver
    Excel 2007
    Posts
    23

    Find and replace based on condition macro

    Hi all,

    Query I would like to build:

    I have a list of names in column A.

    I have a second list of names in column B.

    If a cell in column A is empty, replace that cell with the value from column B (right next to it).

    example:

    John Bob
    Jill Jackie
    Mary Matt
    (emptycell) Jason
    Bill Frank

    After macro:
    John Bob
    Jill Jackie
    Mary Matt
    Jason Jason
    Bill Frank

    Please help.
    Thank you!

  2. #2
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,565

    Re: Find and replace based on condition macro

    Hi contra76,

    Here's one possible solution:

    Sub Macro1()
    
        Dim lngLastRow As Long
        Dim rngCell As Range, _
            rngMyRange As Range
        
        lngLastRow = Cells(Rows.Count, "A").End(xlUp).Row
        Set rngMyRange = Range("A2:A" & lngLastRow) 'Assumes the data starts from Row 2. Change to suit.
            
        Application.ScreenUpdating = False
        
        For Each rngCell In rngMyRange
            If Len(rngCell.Value) = 0 Then
                rngCell.Value = rngCell.Offset(0, 1).Value
            End If
        Next rngCell
        
        Application.ScreenUpdating = True
        
    End Sub
    HTH

    Robert
    ____________________________________________
    Please ensure you mark your thread as Solved once it is. Click here to see how
    If this post helps, please don't forget to say thanks by clicking the star icon in the bottom left-hand corner of my post

  3. #3
    Registered User
    Join Date
    09-23-2010
    Location
    US
    MS-Off Ver
    Excel 2007
    Posts
    23

    Re: Find and replace based on condition macro

    Thanks Robert!

    One problem though.

    The last couple of cells in column A are empty. Thus the macro does not select those last few and they are left empty once the macro is done. Advice?

    Other than that, this is exactly what I'm looking for.

  4. #4
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Find and replace based on condition macro

    Here's an approach with no loop:

    Sub ReplaceBlanks()
    Dim RNG As Range
    On Error GoTo ErrorExit
    
    Set RNG = Columns("A:A").SpecialCells(xlCellTypeBlanks)
    RNG.Value = RNG.Offset(, 1).Value
    
    ErrorExit:
    End Sub
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  5. #5
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,565

    Re: Find and replace based on condition macro

    Hi contra76,

    I set the 'lngLastRow' variable based on the last row in Column A where it seems in this case it should have been Column B. In any case, try the following which finds the last row regardless of what Column it resides in:

    Sub Macro1()
    
        Dim lngLastRow As Long
        Dim rngCell As Range, _
            rngMyRange As Range
                
        'Find the last row by searching backwards through all the rows.
        lngLastRow = ActiveSheet.Cells.Find(What:="*", _
                                            After:=[A1], _
                                            SearchOrder:=xlByRows, _
                                            SearchDirection:=xlPrevious).Row
        
        Set rngMyRange = Range("A2:A" & lngLastRow) 'Assumes the data starts from Row 2. Change to suit.
            
        Application.ScreenUpdating = False
        
        For Each rngCell In rngMyRange
            If Len(rngCell.Value) = 0 Then
                rngCell.Value = rngCell.Offset(0, 1).Value
            End If
        Next rngCell
        
        Application.ScreenUpdating = True
        
    End Sub

    Also, have you tried JBeaucaire's clever solution as for large datasets it is the way to go.

    Regards,

    Robert

  6. #6
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Find and replace based on condition macro

    Did you see/try the post #3 solution? You posted at the same moment I did, so you may have missed it.

  7. #7
    Registered User
    Join Date
    09-23-2010
    Location
    US
    MS-Off Ver
    Excel 2007
    Posts
    23

    Re: Find and replace based on condition macro

    I actually saw it JB and should've posted results back w regards to that solution.

    I tried it but it actually ended up misaligning the copy and paste function. I'm not sure how to explain it but it wasn't copying and pasting correctly. I'm actualy very interested in seeing you're version run. If you have time to think of another method I would love to give it a shot.

    Thanks!

  8. #8
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Find and replace based on condition macro

    Based on what you've supplied, I would use the macro as I provided it. If you're results are unexpected you should provide a sample workbook with data showing what you start with, what you get from the macro, and what you wanted instead, all in a workbook.

    Click GO ADVANCED and use the paperclip icon to post up a desensitized copy of your workbook.

+ 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