VBA Code numbering Cross Reference

khikha1

New Member
Joined
Jun 10, 2020
Messages
22
Office Version
  1. 365
Platform
  1. Windows
dear all

please need your help with a VBA code to find cross references and number them. i add a colors to explain my need. the code should take an item from column A and look in column B and C if it finds a reference for it it has to search column A again , if it find it or find them ,the code is to number them is column D. ( 1,2, ......) , if there is no Cross reference the cell in column D is to be Empty .
hope the attached picture is clear .
i really appreciate your help with this code as i am in real need for it .

regards
ziad alsayed .
 

Attachments

  • VBA Code.png
    VBA Code.png
    24 KB · Views: 14

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Hello khikha1

You might try this:

Sub NumberCrossReferences()
Dim ws As Worksheet
Dim lastRow As Long
Dim i As Long, j As Long
Dim reference As String
Dim counter As Long
Dim found As Boolean

' Set the worksheet
Set ws = ThisWorkbook.Sheets("Sheet1") ' Change "Sheet1" to your sheet name

' Find the last row in column A
lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row

' Loop through each item in column A
For i = 1 To lastRow
reference = ws.Cells(i, 1).Value
counter = 0
found = False

' Check if the reference exists in columns B or C
For j = 1 To lastRow
If ws.Cells(j, 2).Value = reference Or ws.Cells(j, 3).Value = reference Then
found = True
Exit For
End If
Next j

' If a reference is found, number the cross-references in column D
If found Then
For j = 1 To lastRow
If ws.Cells(j, 1).Value = reference Then
counter = counter + 1
ws.Cells(j, 4).Value = counter
End If
Next j
End If
Next i
End Sub

Some help trying to explain :​

  1. Setting the Worksheet:
    • The code sets the worksheet to "Sheet1". Change "Sheet1" to the name of your sheet if it's different.
  2. Finding the Last Row:
    • The code finds the last row in column A to determine the range of data to process.
  3. Looping Through Each Item in Column A:
    • The code loops through each item in column A and initializes the reference, counter, and found variables.
  4. Checking for References in Columns B and C:
    • For each item in column A, the code checks if the reference exists in columns B or C. If a reference is found, it sets found to True and exits the inner loop.
  5. Numbering the Cross-References in Column D:
    • If a reference is found, the code loops through column A again and numbers the cross-references in column D.
  6. Empty Cells in Column D:
    • If no cross-reference is found, the cell in column D remains empty.


hope this helps
plettieri
 
Upvote 0
Based on your description, a simple formula would suffice (which you could populate with a few lines of VBA code).

D2: =SUM(COUNTIF(A$2:A$9,B2:C2))

(which assumes that an item in column A can not reference itself in B or C)

But why do you have numbers in D4, D7 and D9 when you say:
if there is no Cross reference the cell in column D is to be Empty
And given that A5 is identical to A9, why are their results in column D different?
 
Upvote 0
dea
Hello khikha1

You might try this:

Sub NumberCrossReferences()
Dim ws As Worksheet
Dim lastRow As Long
Dim i As Long, j As Long
Dim reference As String
Dim counter As Long
Dim found As Boolean

' Set the worksheet
Set ws = ThisWorkbook.Sheets("Sheet1") ' Change "Sheet1" to your sheet name

' Find the last row in column A
lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row

' Loop through each item in column A
For i = 1 To lastRow
reference = ws.Cells(i, 1).Value
counter = 0
found = False

' Check if the reference exists in columns B or C
For j = 1 To lastRow
If ws.Cells(j, 2).Value = reference Or ws.Cells(j, 3).Value = reference Then
found = True
Exit For
End If
Next j

' If a reference is found, number the cross-references in column D
If found Then
For j = 1 To lastRow
If ws.Cells(j, 1).Value = reference Then
counter = counter + 1
ws.Cells(j, 4).Value = counter
End If
Next j
End If
Next i
End Sub

Some help trying to explain :​

  1. Setting the Worksheet:
    • The code sets the worksheet to "Sheet1". Change "Sheet1" to the name of your sheet if it's different.
  2. Finding the Last Row:
    • The code finds the last row in column A to determine the range of data to process.
  3. Looping Through Each Item in Column A:
    • The code loops through each item in column A and initializes the reference, counter, and found variables.
  4. Checking for References in Columns B and C:
    • For each item in column A, the code checks if the reference exists in columns B or C. If a reference is found, it sets found to True and exits the inner loop.
  5. Numbering the Cross-References in Column D:
    • If a reference is found, the code loops through column A again and numbers the cross-references in column D.
  6. Empty Cells in Column D:
    • If no cross-reference is found, the cell in column D remains empty.


hope this helps
plettieri
dear plettieri

thank for quick response , the code is numbering them "1" in Column D and not sequencing them (1,2, 3 ......) , will you adjust it please
thanks in advance .

regards
Ziad alsayed
 
Upvote 0
Based on your description, a simple formula would suffice (which you could populate with a few lines of VBA code).

D2: =SUM(COUNTIF(A$2:A$9,B2:C2))

(which assumes that an item in column A can not reference itself in B or C)

But why do you have numbers in D4, D7 and D9 when you say:

And given that A5 is identical to A9, why are their results in column D different?
good day,

numbers in D4, D7 and D9 are for example purpose only . this is how the code should number them. ( 1,2,3.........)
A5 and A9 are identical , not a problem .
regards
Ziad alsayed.
 
Upvote 0
the code is numbering them "1" in Column D and not sequencing them (1,2, 3 ......)
numbers in D4, D7 and D9 are for example purpose only . this is how the code should number them. ( 1,2,3.........)
A5 and A9 are identical , not a problem .

I think you are saying that the screenshot you posted doesn't show the correct results in Column D?

If so, can you please post some sample data, plus the numbers you expect to see in column D?
 
Upvote 0
I think you are saying that the screenshot you posted doesn't show the correct results in Column D?

If so, can you please post some sample data, plus the numbers you expect to see in column D?
dear stephen
your right , my apologies , please find attached what is expected in column D.
 

Attachments

  • 1.png
    1.png
    20.4 KB · Views: 4
Upvote 0
please find attached what is expected in column D.
Thanks. A little different to what was first posted.

Can you please also explain the numbers in column D. It's not obvious to me what counts as 1, 2 or 3?
 
Upvote 0
Thanks. A little different to what was first posted.

Can you please also explain the numbers in column D. It's not obvious to me what counts as 1, 2 or 3?
my data is item numbers for stock, same articles are entered in different items number that is the reason i put the colors to make the expmale clearer, i am trying to number them in Clomn D to unify them .
 
Upvote 0
dea

dear plettieri

thank for quick response , the code is numbering them "1" in Column D and not sequencing them (1,2, 3 ......) , will you adjust it please
thanks in advance .

regards
Ziad alsayed
dear plettieri

please need your assistance
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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