Consulting

Results 1 to 3 of 3

Thread: Can this code be simplified

  1. #1
    VBAX Master
    Joined
    Jun 2006
    Posts
    1,091
    Location

    Can this code be simplified

    This is the code that I have created to determine the company that the report references. It is very straight forward so I do not think I need to explain how it works. However, I am wondering if there is a way to simplify it with elseifs or Select Case or any other way.

    Set findstring = Range("A" & finalrow(ActiveSheet)).Find(What:="Atalanta Co.")
    
        If Not findstring Is Nothing Then
            SaveSetting "BRT", "General", "Current Company", "Atalanta"
            GoTo endSub
        End If
        
        Set findstring = Range("A" & finalrow(ActiveSheet)).Find(What:="Camerican International, Inc.")
    
        If Not findstring Is Nothing Then
            SaveSetting "BRT", "General", "Current Company", "Camerican"
            GoTo endSub
        End If
            
        Set findstring = Range("A" & finalrow(ActiveSheet)).Find(What:="De medici")
    
        If Not findstring Is Nothing Then
            SaveSetting "BRT", "General", "Current Company", "Demedici"
            GoTo endSub
        End If
        
        Set findstring = Range("A" & finalrow(ActiveSheet)).Find(What:="Finica Food Specialties Ltd.")
    
        If Not findstring Is Nothing Then
            SaveSetting "BRT", "General", "Current Company", "Finica"
            GoTo endSub
        End If
    Thank You,
    Daniel Blois
    http://studenthacker.blogspot.com/

  2. #2
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,875
    [vba]Select Case Range("A" & finalrow(ActiveSheet)).Value
    Case "Atalanta Co."
    SaveSetting "BRT", "General", "Current Company", "Atalanta"
    Case "Camerican International, Inc."
    SaveSetting "BRT", "General", "Current Company", "Camerican"
    Case "De medici"
    SaveSetting "BRT", "General", "Current Company", "Demedici"
    Case "Finica Food Specialties Ltd."
    SaveSetting "BRT", "General", "Current Company", "Finica"
    End Select
    [/vba]? (untested)
    If the company's name saved to the app's settings is always just the first word, it could be shorter. I've presumed that finalrow returns just a single number.

    Or..[vba]CoName = "" 'this line not required if you use the Case Else option below.
    Select Case Range("A" & finalrow(ActiveSheet)).Value
    Case "Atalanta Co."
    CoName = "Atalanta"
    Case "Camerican International, Inc."
    CoName = "Camerican"
    Case "De medici"
    CoName = "Demedici"
    Case "Finica Food Specialties Ltd."
    CoName = "Finica"
    'Case Else 'if you use this option you can delete the 'If CoName <> "" Then' part of the line below
    'CoName = "Unrecognised Co.name"
    End Select
    If CoName <> "" Then SaveSetting "BRT", "General", "Current Company", CoName
    [/vba]
    Last edited by p45cal; 07-20-2010 at 10:26 AM.
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  3. #3
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Try
    [VBA]
    Sub Test()
    Dim ToFind
    Dim ToSet
    Dim i As Long
    ToFind = Array("Atalanta Co.", "Camerican International, Inc.", "De medici", "Finica Food Specialties Ltd.")
    ToSet = Array("Atalanta", "Camerican", "Demedici", "Finica")

    For i = 0 To 4
    Set findstring = Range("A" & finalrow(ActiveSheet)).Find(What:=ToFind(i))
    If Not findstring Is Nothing Then
    SaveSetting "BRT", "General", "Current Company", ToSet(i)
    GoTo endSub
    End If
    Next
    End Sub

    [/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •