Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Possible to create an IF THEN macro in Excel? 1

Status
Not open for further replies.

ScottNeth

Technical User
Mar 11, 2002
44
US
I have a consistant error on 280 spreadsheets that I'm really hoping I can write a macro for instead of doing them each by hand. I have a part number and a serial number that both need to be entered. The part numbers are consistant with the serial numbers, so I'm trying to find a way to write a script that says something similar to "IF (L3)=3EC15195 THEN (M3)=VACTEHWBAA" (there are six of these statements I would need to create).
 
I would use a Function

Put this in a module with the appropriate changes

Function SelectType(CellRef As String) As String
On Error GoTo Err_SelectType
Select Case CellRef
Case "3EC15195"
SelectType = "VACTEHWBAA"
Case "3EC15196"
SelectType = "VACTEHWBAB"
Case "3EC15197"
SelectType = "VACTEHWBAC"
Case "3EC15198"
SelectType = "VACTEHWBAD"
Case "3EC15199"
SelectType = "VACTEHWBAE"
Case Else
SelectType = "No reference"
End Select


Exit_SelectType:
Exit Function

Err_SelectType:
SelectType = "Data error"
Resume Exit_SelectType
End Function

Then got the insert Function as normal and select User Defined from the list of options. You should see SelectType so select that as normal and it will work like a standard function. Sandy
 
Do you want these as a custom function or do you want the macro to go to each sheet and output the associated serial number depending on the part number? You were not real clear on what you wanted to do.

As far as coding, you should probably use the Select Case statement instead of a bunch of If statements. This code will go through each worksheet in the workbook, grab the part nummber (L3) and put the correct serial number in cell M#.

Code:
Sub UpdateSerialNumbers()
    Dim ws As Worksheet
    Dim sPartNo As String
    Dim sSerialNo As String
    
    For Each ws In ActiveWorkbook.Worksheets
        sPartNo = ws.Range("L3").Text
        If sPartNo <> &quot;&quot; Then
            Select Case sPartNo
                Case &quot;3EC15195&quot;: sSerialNo = &quot;VACTEHWBAA&quot;
                Case &quot;?????&quot;: sSerialNo = &quot;????&quot;
                'and so on
                Case Else
                    sSerialNo = &quot;UNKNOWN&quot;
            End Select
            ws.Range(&quot;M3&quot;) = sSerialNo
        End If
    Next ws
    
    MsgBox &quot;Done&quot;
End Sub

&quot;IF (L3)=3EC15195 THEN (M3)=VACTEHWBAA&quot;
 
ScottNeth,

Sometimes I like to answer questions by not answering the question asked...

You don't necessarily need to do VBA in order to achieve what you're looking for. You can easily do this with a lookup function:

Create table (2 x 6) containing your results. Name this range &quot;my_table&quot; or whatever. Then, in the cell where you'd like your corrected SN, evaluating the part number in cell A1 enter this function:

Code:
=IF(ISNA(VLOOKUP(A1,my_table,2,FALSE)),&quot;No reference&quot;,VLOOKUP(A1,my_table,2,FALSE))

Then just do a fill down for the 280 parts you need to check. No VBA required.
 
Yeah, but your VLOOKUP suggestion beats hardcoding the values in my example.
 
You have to be a little bit wary of VLOOKUP because it can be quite easy to set up to pick up the nearest value when no exact match is found. I think it's one of these things were everyone has their own preferred method. Personally I preferred code to nested IFs. Sandy
 
B827: the last argument of the VLOOKUP controls whether it requires an exact match or the nearest value. Anyway, you are correct, everyone has their own preferred method.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top