Help using InStr? Referencing a different sheet.

Coyotex3

Well-known Member
Joined
Dec 12, 2021
Messages
507
Office Version
  1. 365
Platform
  1. Windows
Hi Again, hoping someone can help me figure this one out.

On Sheet 1 I have the following:

Book2
ABC
1Account TypeNotesTotal
2Outstanding100 1053,500.00
3Outstanding330 3457,000.00
4Outstanding200 2058,950.00
5Outstanding550 5504,650.00
Sheet1


On Sheet 2 I have the following:

Book2
AB
1DescriptionAmount
2Account 100 105
3Vehicles
4
5Acc. 200 205
6Boats
7
8
9Acct 330 345
10Planes
11
12
13Acc 450 550
14Trains
Sheet2


I've been working on this code
VBA Code:
Dim WB1 As Workbook, WS1 As Worksheet, WS2 As Worksheet
Set WB1 = ThisWorkbook
Set WS1 = WB1.Worksheets("Sheet1")
Set WS2 = WB1.Worksheets("Sheet2")
For Each c1 In WS2.Range("A2", Range("A" & Rows.Count).End(xlUp))
    If InStr(1, c1.Value, "100 105") > 0 Then c1.Offset.End(xlToLeft).Offset(, 1).Value = WS1.Range("C2").Value2
    If InStr(1, c1.Value, "200 205") > 0 Then c1.Offset.End(xlToLeft).Offset(, 1).Value = WS1.Range("C3").Value2
    If InStr(1, c1.Value, "330 345") > 0 Then c1.Offset.End(xlToLeft).Offset(, 1).Value = WS1.Range("C4").Value2
    If InStr(1, c1.Value, "450 550") > 0 Then c1.Offset.End(xlToLeft).Offset(, 1).Value = WS1.Range("C5").Value2
Next
End Sub

To get to this:
Book2
AB
1DescriptionAmount
2Account 100 1053500
3Vehicles
4
5Acc. 200 2057000
6Boats
7
8
9Acct 330 3458950
10Planes
11
12
13Acc 450 5504650
14Trains
Sheet2


My issue is that "Sheet1" Column "B" might not always be in that order. I would like for the code to compare "Sheet1" Column "A" and Sheet2 Column "B", and if there is a match, give me the desired output from Column "C".

Note: This Code isn't complete. Cell B13 should be left empty in the final outcome since Sheet2 Cell A13 does not match any of the notes on Sheet1 Column B. Also, Cellls B5 & B9 have the wrong amounts. Cell B5 should have 8950 and Cell B9 should have 7000. I have not been able to figure out how to expand on this.

Hope this makes sense.

Thank you.
 
Last edited:

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
Hi,

With the exception of cell A2, do you have a constant step of 4 in between in the "Acct xxx xxx" cells with consistently the right 7 characters representing your account number
 
Upvote 0
Hi, try this..
VBA Code:
Sub test()
    Dim lst, rng As Range, i, dic As Object, acc

    With Worksheets("Sheet1")
        lst = .Range("B2:C" & .Cells(Rows.Count, 2).End(3).Row).Value
    End With

    Set dic = CreateObject("Scripting.Dictionary")

    For i = 1 To UBound(lst)
        dic.Item(lst(i, 1)) = lst(i, 2)
    Next i

    With Worksheets("Sheet2")
        Set rng = .Range("A2:B" & .Cells(Rows.Count, 1).End(3).Row)
    End With

    With CreateObject("VbScript.Regexp")
        .Pattern = "[A.]*([\d]{3}\s[\d]{3})"
        For i = 1 To rng.Rows.Count
            If .test(rng.Cells(i, 1).Value) Then
                acc = .Execute(rng.Cells(i, 1).Value)(0)
                If dic.exists(acc) Then rng.Cells(i, 2).Value = dic(acc)
            End If
        Next i
    End With
End Sub
 
Upvote 0
Solution
Hi,

With the exception of cell A2, do you have a constant step of 4 in between in the "Acct xxx xxx" cells with consistently the right 7 characters representing your account number
Hi James, from what I observed there doesn't seem to be a pattern before the account number, but the account number itself will always follow the same pattern.
 
Upvote 0
Hi, try this..
VBA Code:
Sub test()
    Dim lst, rng As Range, i, dic As Object, acc

    With Worksheets("Sheet1")
        lst = .Range("B2:C" & .Cells(Rows.Count, 2).End(3).Row).Value
    End With

    Set dic = CreateObject("Scripting.Dictionary")

    For i = 1 To UBound(lst)
        dic.Item(lst(i, 1)) = lst(i, 2)
    Next i

    With Worksheets("Sheet2")
        Set rng = .Range("A2:B" & .Cells(Rows.Count, 1).End(3).Row)
    End With

    With CreateObject("VbScript.Regexp")
        .Pattern = "[A.]*([\d]{3}\s[\d]{3})"
        For i = 1 To rng.Rows.Count
            If .test(rng.Cells(i, 1).Value) Then
                acc = .Execute(rng.Cells(i, 1).Value)(0)
                If dic.exists(acc) Then rng.Cells(i, 2).Value = dic(acc)
            End If
        Next i
    End With
End Sub
Thank you so much! This works!

If the pattern changes, do I just change this line?
VBA Code:
.Pattern = "[A.]*([\d]{3}\s[\d]{3})"
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,286
Members
452,631
Latest member
a_potato

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