Consulting

Results 1 to 8 of 8

Thread: Replacing multiple numerical values

  1. #1
    VBAX Regular
    Joined
    Aug 2017
    Posts
    57
    Location

    Arrow Replacing multiple numerical values

    I would like to replace the values in sheet1 from the corresponding values in Column B of sheet2 in the attached excel file.
    Can I get a little help please?

    I found the VBA code, but I think it needs a little adjustment.

    Thank you!
    Attached Files Attached Files

  2. #2
    VBAX Expert
    Joined
    Sep 2016
    Posts
    788
    Location
    lookat:=xlWhole

  3. #3
    VBAX Regular
    Joined
    Aug 2017
    Posts
    57
    Location
    Thanks, Mana.

    I did the following change :

    Sub Test()
    
        Dim Sh1 As Worksheet
        Dim Sh2 As Worksheet
       
        Set Sh1 = Sheets(1)
        Set Sh2 = Sheets(2)
        For Each cel In Sh2.Columns(1).SpecialCells(2)
            With Sh1.Cells
                Set c = .Find(cel, lookat:=xlWhole)
                Do
                    If Not c Is Nothing Then
                        c.Value = Replace(c, cel, cel.Offset(, 1))
                    End If
                    Set c = .FindNext(c)
                Loop Until c Is Nothing
            End With
        Next
    End Sub
    but I still have problem with single digit numbers.

  4. #4
    VBAX Expert
    Joined
    Sep 2016
    Posts
    788
    Location
    c.Value = Replace(c, cel, cel.Offset(, 1) & "@")
    .Replace "@", "", xlPart

  5. #5
    VBAX Regular
    Joined
    Aug 2017
    Posts
    57
    Location
    I'm sorry, I didn't understand what you meant.

    I tried this but this doesn't work :
     c.Value = .Replace("@", "", xlPart)

  6. #6
    VBAX Expert
    Joined
    Sep 2016
    Posts
    788
    Location
    1 ---> 3@ --->3
    2 ---> 5@ --->5

        With Sh1.Cells
            For Each cel In Sh2.Columns(1).SpecialCells(2)
                Set c = .Find(cel, lookat:=xlWhole)
                Do
                    If Not c Is Nothing Then
                        c.Value = Replace(c, cel, cel.Offset(, 1) & "@")
                    End If
                    Set c = .FindNext(c)
                Loop Until c Is Nothing
            Next
            .Replace "@", "", xlPart
        End With
    End Sub

  7. #7
    VBAX Expert
    Joined
    Sep 2016
    Posts
    788
    Location
    Sub Test2()
        Dim Sh1 As Worksheet
        Dim Sh2 As Worksheet
        
        Set Sh1 = Sheets(1)
        Set Sh2 = Sheets(2)
        
        With Sh1.Cells
            For Each cel In Sh2.Columns(1).SpecialCells(2)
                .Replace cel, cel.Offset(, 1) & "@", xlWhole
            Next
            .Replace "@", "", xlPart
        End With
    End Sub

  8. #8
    VBAX Regular
    Joined
    Aug 2017
    Posts
    57
    Location
    Thanks, Mana. It works for all numbers now.

Tags for this Thread

Posting Permissions

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