New to excel VBA : Need guidance

xtinct

New Member
Joined
Sep 11, 2006
Messages
48
example1.jpg

Example1
example2.jpg

Example2

after filtering for example, all of "PA" eg. "PA1-AA" "PA2-AA" "PA3-AA" using VBA, how do i compare their risk status and copy the highest risk status in Example1 to the respective Example2 Status colum?

Thanks in advance.
 
try this
Code:
Sub test()
Dim dic As Object, a, i As Long, z As String, myStatus, e, x, y
Dim Status1 As Integer, Status2 As Integer, ii As Integer
Set dic = CreateObject("Scripting.Dictionary")
dic.comparemode = vbTextCompare
myStatus = Array("Released","Low","Med","High","EOL")
a = Workbooks("example1.xls").Sheets("Sheet1").Range("a1").CurrentRegion.Value
For i = 2 To UBound(a,1)
    z = Left(a(i,1),InStr(a(i,1),"-")-1)
    If Not dic.exists(z) Then
       For ii = 0 To UBound(myStatus)
         If InStr(1,a(i,2),myStatus(ii),1)>0 Then Status1 = ii + 1 : Exit For
       Next
       For ii = 0 To UBound(myStatus)
         If InStr(1,a(i,3),myStatus(ii),1)>0 Then Status2 = ii + 1 : Exit For
       Next
       dic.add z, WorksheetFunction.Max(Status1,Status2)
    Else
       For ii = 0 To UBound(myStatus)
         If InStr(1,a(i,2),myStatus(ii),1)>0 Then Statsu1 = ii + 1 : Exit For
       Next
       For ii = 0 To UBound(myStatus)
         If InStr(1,a(i,3),myStatsu(ii),1)>0 Then Status2 = ii + 1 : Exit For
       Next
       dic(z) = WorksheetFunction.Max(dic(z),Status1,Status2)
    End If
    Status1 = 0 : Status2 = 0
Next
Erase a : x = dic.keys : y = dic.items : Set dic = Nothing
With Workbooks("example2.xls").Sheets("Sheet1")
    For Each r In .Range("b1",.Range("b" & Rows.Count).End(xlUp))
       For i = 0 To UBound(x)
          If InStr(x(i),r.Value) = 1 Then
          r.Offset(,6).Value = myStatus(y(i)-1)
          Exit For
          End If
       Next
   Next
End With
End Sub
 
Upvote 0

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
While you are in VB Editor
1) [View]-[LocalWindow]
2) Click somewhere on the code
3) as you hit F8, code will execute line by line

then you can see all the variables in the local window to check how it works...
 
Upvote 0
what i dont understand is that it works perfectly on the example.. but when i try it on my actual data, it ran until the 6th product in the Product Name of Example2.xls and stop and shows an error..
 
Upvote 0
maybe i can send u my actual data? but do you have any suggestion on how i can send them to you? is there any file host website that i can use to upload the file?
 
Upvote 0
After your PM
You need to adjust the line of
Code:
z = Left(a(i,1),6)
If you have example2.xls code have more/less than 1st 6 characters of example1.xls
Code:
Sub test()
Dim dic As Object, a, i As Long, z As String, myStatus, e, x, y
Dim Status1 As Integer, Status2 As Integer, ii As Integer
Set dic = CreateObject("Scripting.Dictionary")
dic.comparemode = vbTextCompare
myStatus = Array("Released","Low","Med","High","EOL")
a = Workbooks("example1.xls").Sheets("Sheet1").Range("a1").CurrentRegion.Value
For i = 2 To UBound(a,1)
    z = Left(a(i,1),6) '<- adjust here if needed
    If Not dic.exists(z) Then
       For ii = 0 To UBound(myStatus)
         If InStr(1,a(i,2),myStatus(ii),1)>0 Then Status1 = ii + 1 : Exit For
       Next
       For ii = 0 To UBound(myStatus)
         If InStr(1,a(i,3),myStatus(ii),1)>0 Then Status2 = ii + 1 : Exit For
       Next
       dic.add z, WorksheetFunction.Max(Status1,Status2)
    Else
       For ii = 0 To UBound(myStatus)
         If InStr(1,a(i,2),myStatus(ii),1)>0 Then Statsu1 = ii + 1 : Exit For
       Next
       For ii = 0 To UBound(myStatus)
         If InStr(1,a(i,3),myStatsu(ii),1)>0 Then Status2 = ii + 1 : Exit For
       Next
       dic(z) = WorksheetFunction.Max(dic(z),Status1,Status2)
    End If
    Status1 = 0 : Status2 = 0
Next
Erase a : x = dic.keys : y = dic.items : Set dic = Nothing
With Workbooks("example2.xls").Sheets("Sheet1")
    For Each r In .Range("b1",.Range("b" & Rows.Count).End(xlUp))
       For i = 0 To UBound(x)
          If InStr(x(i),r.Value) = 1 Then
          r.Offset(,6).Value = myStatus(y(i)-1)
          Exit For
          End If
       Next
   Next
End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,771
Messages
6,126,799
Members
449,337
Latest member
BBV123

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