+ Reply to Thread
Results 1 to 8 of 8

More help on Find and Replace from one sheet to the next

Hybrid View

  1. #1
    Registered User
    Join Date
    06-26-2019
    Location
    California, US
    MS-Off Ver
    Office 365
    Posts
    36

    More help on Find and Replace from one sheet to the next

    I'm trying to find and replace cells by looking at a value from one sheet, and replacing it with a value from another sheet.
    I'm currently modifying a code from my last thread by jolivanes (thank you!)

    I think I'm running into an issue using this code because some of the cells are combined, so it's not properly searching down the column. But I'm not sure.

    The code should:
    1. Find a name from (Sheet 1 column A) in (Sheet 2 column E)
    2. If it's found, then replace (Sheet 2 Column K, same row) with the value from (Sheet 1 column B, same row)

    Dim s1 As Worksheet
    Dim s2 As Worksheet
    Dim al As Range
    
    Set s1 = Worksheets("Sheet1")
    Set s2 = Worksheets("Sheet2")
    
            For Each al In s1.Range("A2:A" & s1.Cells(Rows.Count, 1).End(xlUp).Row)
                If WorksheetFunction.CountIf(s2.Range("E2:E" & s2.Cells(Rows.Count, 1).End(xlUp).Row), al.Value) <> 0 Then
                    s2.Range("E2:E" & s2.Cells(Rows.Count, 1).End(xlUp).Row).Find(al.Value, , , 1).Offset(, 4).Value = al.Offset(, 1).Value
                End If
            Next al
    I've attached a sample worksheet to give you an idea of how the values might look.
    Attached Files Attached Files

  2. #2
    Valued Forum Contributor
    Join Date
    07-14-2017
    Location
    Poland
    MS-Off Ver
    Office 2010
    Posts
    528

    Re: More help on Find and Replace from one sheet to the next

    Test my solution.
    HTML Code: 
    Attached Files Attached Files
    Best Regards,
    Maras.

  3. #3
    Registered User
    Join Date
    06-26-2019
    Location
    California, US
    MS-Off Ver
    Office 365
    Posts
    36

    Re: More help on Find and Replace from one sheet to the next

    Quote Originally Posted by maras_mak View Post
    Test my solution.
    Thanks Mara. The solution works on the sample sheet but didn't work on my actual one. The only thing I changed was all instances of (s2) to (s4) and Set s4 as Worksheets("Sheet4")

    I get a "Runtime Error '9': Subscript out of range" on the highlighted line.
        Set d = CreateObject("scripting.dictionary")
        Set s1 = Worksheets("Sheet1")
        Set s2 = Worksheets("Sheet2")
        With s1
            lr = .Cells(Rows.Count, "A").End(xlUp).Row
            a1() = .Range("A1:B" & lr).Value
            If IsArray(a1) Then
                With s2
                    lr = .Cells(Rows.Count, "E").End(xlUp).Row
                    If lr > 1 Then
                        a2 = .Range("E1:K" & lr).Value
                        For i = 1 To UBound(a1)
                            d(a1(i, 1)) = a1(i, 2)
                        Next
                        For i = 1 To UBound(a2)
                            If d.exists(a2(i, 1)) Then _
                                        a2(i, 7) = d.Item(a2(i, 1))
                        Next
                    End If
                    .[K1].Resize(UBound(a2)) = Application.Index(a2, 0, 7)
                End With
            End If
        End With
        Set s1 = Nothing
        Set s2 = Nothing
    I forgot to mention that the values in Sheet 1 may have random blanks or missing values in it. I'm not sure if that will cause the code to fail.

  4. #4
    Valued Forum Contributor
    Join Date
    07-14-2017
    Location
    Poland
    MS-Off Ver
    Office 2010
    Posts
    528

    Re: More help on Find and Replace from one sheet to the next

    Attach a small sample Excel workbook (10-20 rows of data).
    Make sure that your sample data are truly REPRESENTATIVE of your real data.
    The use of unrepresentative data is very frustrating and can lead to long delays in reaching a solution.

  5. #5
    Registered User
    Join Date
    06-26-2019
    Location
    California, US
    MS-Off Ver
    Office 365
    Posts
    36

    Re: More help on Find and Replace from one sheet to the next

    Ok here's a better sample workbook.

    Thank you very much!
    Attached Files Attached Files

  6. #6
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,565

    Re: More help on Find and Replace from one sheet to the next

    You could try this (use from either sheet)
    Sub Maybe()
    Dim sh1 As Worksheet, sh2 As Worksheet, i As Long
    Set sh1 = Worksheets("Sheet1")
    Set sh2 = Worksheets("Sheet4")
    For i = 2 To sh1.Cells(Rows.Count, 1).End(xlUp).Row
        If WorksheetFunction.CountIf(sh2.Range("E4:E" & sh2.Cells(Rows.Count, 5).End(xlUp).Row), sh1.Cells(i, 1).Value) <> 0 Then
            sh2.Range("E4:E" & sh2.Cells(Rows.Count, 5).End(xlUp).Row).Find(sh1.Cells(i, 1).Value, , , 1).Offset(, 6).Value = sh1.Cells(i, 1).Offset(, 1).Value
        End If
    Next i
    End Sub

  7. #7
    Valued Forum Contributor
    Join Date
    07-14-2017
    Location
    Poland
    MS-Off Ver
    Office 2010
    Posts
    528

    Re: More help on Find and Replace from one sheet to the next

    Haaaa, this is a completely different data layout and it should not work well !!!
    Maybe it will be fine now.
    Sub Test()
        Dim lr As Integer, i As Integer, j As Integer
        Dim s1 As Worksheet
        Dim s2 As Worksheet
        Dim d As Object
        Dim a1(), a2(), af()
        
        Set d = CreateObject("scripting.dictionary")
        Set s1 = Worksheets("Sheet1")
        Set s2 = Worksheets("Sheet4")
        With s1
            lr = .Cells(Rows.Count, "A").End(xlUp).Row
            a1() = .Range("A2:B" & lr).Value
            If IsArray(a1) Then
                With s2
                    lr = .UsedRange.Rows.Count
                    If lr > 5 Then
                        a2 = .Range("B6:E" & lr).Value
                        ReDim af(1 To UBound(a2), 1 To 1)
                        For i = 1 To UBound(a1)
                            d(a1(i, 1)) = a1(i, 2)
                        Next
                        For i = 1 To UBound(a2)
                            For j = 1 To UBound(a2, 2)
                                If d.exists(a2(i, j)) Then af(i, 1) = d.Item(a2(i, j))
                            Next
                        Next
                    End If
                    .[K6].Resize(UBound(a2)) = af
                End With
            End If
        End With
        Set s1 = Nothing
        Set s2 = Nothing
        Set d = Nothing
    End Sub
    Attached Files Attached Files

  8. #8
    Registered User
    Join Date
    06-26-2019
    Location
    California, US
    MS-Off Ver
    Office 365
    Posts
    36

    Re: More help on Find and Replace from one sheet to the next

    Thank you! The solution worked perfectly!

+ 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] Find and replace from one sheet to another.
    By Newbuilder in forum Excel Programming / VBA / Macros
    Replies: 14
    Last Post: 08-26-2017, 11:09 AM
  2. Multiple Find/Replace on a specified sheet and outputtingin a separe sheet
    By scfn in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-24-2013, 07:27 AM
  3. Multiple find and replace from Glossary sheet to the active sheet
    By Chandru71 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-22-2011, 12:34 PM
  4. how to Find and Replace using different excel sheet?
    By rythem_of_the_nig in forum Excel General
    Replies: 1
    Last Post: 08-30-2011, 06:38 AM
  5. Find/Replace on a protected sheet
    By LAF in forum Excel General
    Replies: 1
    Last Post: 07-10-2009, 09:32 PM
  6. Find/Replace on a protected sheet
    By LAF in forum Excel General
    Replies: 1
    Last Post: 06-23-2009, 07:39 AM
  7. trying to find #REF! in a formula to replace with a sheet name
    By funkymonkUK in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 06-28-2006, 06:25 PM

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