Select Case Statement

S

sue.hunter

How would this code be written using the Select Case Statement? I
don't know how to handle the portion after "Then". I'm assuming the
Select Case Statement would be a better choice?
Thanks,
Sue


If ListBox1.Value = "QBE (all pillars)" Then
ActiveDocument.AttachedTemplate.AutoTextEntries
("atLogoQBE").Insert _
Where:=.Bookmarks("bmLogo").Range, RichText:=True

ElseIf ListBox1.Value = "Agri" Then
ActiveDocument.AttachedTemplate.AutoTextEntries
("atLogoAgri").Insert _
Where:=.Bookmarks("bmLogo").Range, RichText:=True

ElseIf ListBox1.Value = "Farmers Union" Then
ActiveDocument.AttachedTemplate.AutoTextEntries
("atLogoFUI").Insert _
Where:=.Bookmarks("bmLogo").Range, RichText:=True

ElseIf ListBox1.Value = "General Casualty" Then
ActiveDocument.AttachedTemplate.AutoTextEntries("atLogoGC").Insert
_
Where:=.Bookmarks("bmLogo").Range, RichText:=True

ElseIf ListBox1.Value = "North Pointe" Then
ActiveDocument.AttachedTemplate.AutoTextEntries("atLogoNP").Insert
_
Where:=.Bookmarks("bmLogo").Range, RichText:=True

ElseIf ListBox1.Value = "Unigard" Then
ActiveDocument.AttachedTemplate.AutoTextEntries
("atLogoUIC").Insert _
Where:=.Bookmarks("bmLogo").Range, RichText:=True

End If
 
F

fumei via OfficeKB.com

Yes, Select Case would be better. It always is when you are testing multiple
values for the same thing. As a general best-practice it is good to have a
Case Else to handle possible values that you do NOT have actions for.

Select Case ListBox1.Value ' the ONE thing
' the multiple possible values
Case "QBE (all pillars)"
ActiveDocument.AttachedTemplate _
.AutoTextEntries("atLogoQBE").Insert _
Where:=.Bookmarks("bmLogo").Range, _
RichText:=True
Case "Agri"
ActiveDocument.AttachedTemplate _
.AutoTextEntries("atLogoAgri").Insert _
Where:=.Bookmarks("bmLogo").Range, _
RichText:=True
Case "Farmers Union"
ActiveDocument.AttachedTemplate _
.AutoTextEntries("atLogoFUI").Insert _
Where:=.Bookmarks("bmLogo").Range, _
RichText:=True
Case "General Casualty"
ActiveDocument.AttachedTemplate _
.AutoTextEntries("atLogoGC").Insert _
Where:=.Bookmarks("bmLogo").Range, _
RichText:=True
Case "North Pointe"
ActiveDocument.AttachedTemplate _
.AutoTextEntries("atLogoNP").Insert _
Where:=.Bookmarks("bmLogo").Range, _
RichText:=True
Case "Unigard"
ActiveDocument.AttachedTemplate _
.AutoTextEntries("atLogoUIC").Insert _
Where:=.Bookmarks("bmLogo").Range, _
RichText:=True
Case Else
MsgBox "Something is not right here."
End Select
 
T

Tony Jollans

Personally I would remove the AutoText Insert outside the condition:

Dim ATName As String
ATName = ""

Select Case ListBox1.Value
Case "QBE (all pillars)": ATName = "atLogoQBE"
Case "Agri": ATName = "atLogoAgri"
Case "Farmers Union": ATName = "atLogoFUI"
Case "General Casualty": ATName = "atLogoGC"
Case "North Pointe": ATName = "atLogoNP"
Case "Unigard": ATName = "atLogoUIC"
End Select

If ATName <> "" Then
With ActiveDocument
.AttachedTemplate.AutoTextEntries(ATName).Insert _
Where:=.Bookmarks("bmLogo").Range, _
RichText:=True
End With
End If
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top