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.
 

xtinct

New Member
Joined
Sep 11, 2006
Messages
48
when i run the code, the error occurs at

Code:
End Select

it says

Compile error:
End Select without Select Case
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.

xtinct

New Member
Joined
Sep 11, 2006
Messages
48
i ran the code but my Example2 still remained unchanged.. nothing is updated or changed.. what is wrong?
 

jindon

MrExcel MVP
Joined
Aug 21, 2004
Messages
16,995
Do you want to copy the code again ( i've just added the Msg
Box)
and come back if you get the Msg or Not?
 

xtinct

New Member
Joined
Sep 11, 2006
Messages
48
ran the code but no msgbox appear and Example2 is still the same.. :(
 

jindon

MrExcel MVP
Joined
Aug 21, 2004
Messages
16,995
try
Code:
Sub sample()
Dim ws1 As Worksheet, ws2 As Worksheet, r As Range, c As Range, ff As String, myStatus As String
Set ws1 = Workbooks("Example1.xls").Sheets("Sheet1")
Set ws2 = Workbooks("Example2.xls").Sheets("Sheet1")

For Each r In ws2.Range("b2",ws2.Range("b" & Rows.Count).End(xlUp))
      Set c = ws1.Columns("a").Find(r.Value,,,xlPart)
      If Not c Is Nothing Then
           ff = c.Address : MsgBox c.Address
           Do
                 If UCase(c.Offset(,1).Value) Like "*HIGH" Then              
                          myStatus = "Risk-High" : Exit Do
                 ElseIf UCase(c.Offset(,1).Value) Like "*Med" Then
                          myStatus = "Risk-Med"
                 ElseIf UCase(c.Offset(,1).Value) Like "*Low" Then
                          If myStatus <> "Risk-High" And myStatus <> "Risk-Med" Then
                              myStatus = "Risk-Low"
                          End If
                 End If
                 Set c = ws1.columns("a").FindNext(c)
           Loop Until ff = c.Address
           r.Offset(,6).Value = myStatus : myStatus = Empty
      Else
           MsgBox r.Value & " is not exist"
      End If
Next
Set ws1 = Nothing
Set ws2 = Nothing
End Sub
 

xtinct

New Member
Joined
Sep 11, 2006
Messages
48
ok now it seems ok already.. but Example2's PRD336 is empty.. should have been Risk-Med
 

jindon

MrExcel MVP
Joined
Aug 21, 2004
Messages
16,995
OOps
try
Code:
Sub sample()
Dim ws1 As Worksheet, ws2 As Worksheet, r As Range, c As Range, ff As String, myStatus As String
Set ws1 = Workbooks("Example1.xls").Sheets("Sheet1")
Set ws2 = Workbooks("Example2.xls").Sheets("Sheet1")

For Each r In ws2.Range("b2",ws2.Range("b" & Rows.Count).End(xlUp))
      Set c = ws1.Columns("a").Find(r.Value,,,xlPart)
      If Not c Is Nothing Then
           ff = c.Address : MsgBox c.Address
           Do
                 If Trim(UCase(c.Offset(,1).Value)) Like "*HIGH" Then              
                          myStatus = "Risk-High" : Exit Do
                 ElseIf Trim(UCase(c.Offset(,1).Value)) Like "*MED" Then
                          myStatus = "Risk-Med"
                 ElseIf Trim(UCase(c.Offset(,1).Value)) Like "*LOW" Then
                          If myStatus ="" Then myStatus = "Risk-Low"
                 End If
                 Set c = ws1.columns("a").FindNext(c)
           Loop Until ff = c.Address
           r.Offset(,6).Value = myStatus : myStatus = Empty
      Else
           MsgBox r.Value & " is not exist"
      End If
Next
Set ws1 = Nothing
Set ws2 = Nothing
End Sub
 

Forum statistics

Threads
1,171,088
Messages
5,873,749
Members
432,998
Latest member
RachCur

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
Top