Trying to get an exact match in my search

PGNewbie

New Member
Joined
Feb 6, 2020
Messages
41
Office Version
  1. 365
Platform
  1. Windows
I have been working on comparing two columns in one workbook and comparing it with two columns in another "workbook2". My code runs fine but I have found a glitch.
Because I am working with Ip addresses in one of the columns if the ip address is 10.10.0.1 and another ip address is 10.10.0.10, the results for the ip address 10.10.0.1 do not get written to Column B. I will give an example below

HostNameIp addressSerial Number
sfdgsthgfhMaster10.10.0.1SerialNumber1
sfdgsthgfh10.10.0.1SerialNumber2
aghjutreMaster10.10.0.10SerialNumber3
aghjutreSlave10.10.0.10SerialNumber4


Row two column B should have an entry for the slave but it does not show up. My criteria looks at column C and D in order to make it a unique and each unique pairing has a name to it

I've noticed in the FInd feature when I enter 10.10.0.1, it will give me a list for all addresses that start with 10.10.0.1, such as 10.10.0.11, 10.10.0.17, etc.

I have thousands of entries and searching for each ip that may be having this issue will take a long time. Is there anyway for the code to make an exact match?

VBA Code:
Sub InsertDeviceName_NewBook()

Dim w1 As Worksheet, w2 As Worksheet, wsnew As Worksheet
      Dim wbnew As Workbook
      Dim c As Range, FR As Variant
      Dim d As Range
      Dim e As Range 'rng1 As Range, rng2 As Range
      Dim lr1 As Long, lr2 As Long
      Dim x As Long, i As Long, lr As Long
      Dim rng1 As Variant, rng2 As Variant
      Dim vals()
      

      Application.ScreenUpdating = False
      
      
      Set w2 = Workbooks("book2.xlsx").ActiveSheet
      Set w1 = Workbooks("book1.xlsx").ActiveSheet
      
      
      
     w1.Range("B:D").Copy
     Set wbnew = Workbooks.Add
     Columns("A:A").Select
     ActiveSheet.Paste
     Application.CutCopyMode = False
     ActiveSheet.Name = w1.Name
     Set wsnew = wbnew.ActiveSheet
     lr1 = wsnew.Cells(Rows.count, 1).End(xlUp).Row
     lr2 = w2.Cells(Rows.count, 1).End(xlUp).Row
    
    
     wsnew.Sort.SortFields.Add2 Key:=Range("B1"), _
        SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
    With wsnew.Sort
        .SetRange Range("A1:C" & lr1)
        .Header = xlGuess
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
 
   End With
    
   Columns("B:B").Insert Shift:=xlToRight, _
      CopyOrigin:=xlFormatFromLeftOrAbove
      
      Range("B1").Select
      ActiveCell.FormulaR1C1 = "Device Name"
      

    rng1 = wsnew.Range("C2", wsnew.Range("D" & wsnew.Cells(Rows.count, 1).End(xlUp).Row)).Value
    rng2 = w2.Range("B2", w2.Range("D" & w2.Cells(Rows.count, 1).End(xlUp).Row)).Value


   ReDim vals(1 To UBound(rng1, 1), 1)
   For x = 1 To UBound(rng1, 1)
      For i = 1 To UBound(rng2)
          If rng1(x, 1) = rng2(i, 2) Then
              If rng1(x, 2) = rng2(i, 3) Then
                  vals(x, 0) = rng2(i, 1)
                  GoTo Nextone
              End If
          End If
      Next i
  Nextone:
  Next x

  wsnew.Range("B2").Resize(UBound(vals, 1), 1).Value = vals

  Application.ScreenUpdating = True

  End Sub
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
My code runs fine but I have found a glitch.

I am not clear what you mean by glitch?

10.10.0.1 is not an exact match for 10.10.0.10.

Your code is looking for exact matches.

What do you want your code to do?
 
Upvote 0

Forum statistics

Threads
1,224,522
Messages
6,179,292
Members
452,902
Latest member
Knuddeluff

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top