Can i shorten this simple code

clares

Well-known Member
Joined
Mar 14, 2002
Messages
557
Hi All

I am sure that I can, but can this code be shortened?

Code:
Private Sub CommandButton1_Click()
Dim PeterHSG As Integer
Dim PeterHSS As Integer
Dim PeterHHG As Integer
Dim PeterHHS As Integer
Dim BarryHSG As Integer
Dim BarryHSS As Integer
Dim BarryHHG As Integer
Dim BarryHHS As Integer
Dim AlanHSG As Integer
Dim AlanHSS As Integer
Dim AlanHHG As Integer
Dim AlanHHS As Integer

PeterHSG = Range("L18").Value
PeterHSS = Range("L20").Value
PeterHHG = Range("P18").Value
PeterHHS = Range("P20").Value

BarryHSG = Range("L28").Value
BarryHSS = Range("L30").Value
BarryHHG = Range("P28").Value
BarryHHS = Range("P30").Value

AlanHSG = Range("L38").Value
AlanHSS = Range("L40").Value
AlanHHG = Range("P38").Value
AlanHHS = Range("P40").Value

'Peters images
Image1.Visible = False
Image2.Visible = False
Image3.Visible = False
Image4.Visible = False

'Barrys image
Image5.Visible = False
Image6.Visible = False
Image7.Visible = False
Image8.Visible = False

'Alans images
Image9.Visible = False
Image10.Visible = False
Image11.Visible = False
Image12.Visible = False

If (PeterHSG > BarryHSG) And (PeterHSG > AlanHSG) Then
    Image1.Visible = True
ElseIf (BarryHSG > PeterHSG) And (BarryHSG > AlanHSG) Then
    Image5.Visible = True
ElseIf (AlanHSG > PeterHSG) And (AlanHSG > BarryHSG) Then
    Image9.Visible = True
End If

If (PeterHSS > BarryHSS) And (PeterHSS > AlanHSS) Then
    Image2.Visible = True
ElseIf (BarryHSS > PeterHSS) And (BarryHSS > AlanHSS) Then
    Image6.Visible = True
ElseIf (AlanHSS > PeterHSS) And (AlanHSS > BarryHSS) Then
    Image10.Visible = True
End If

If (PeterHHG > BarryHHG) And (PeterHHG > AlanHHG) Then
    Image3.Visible = True
ElseIf (BarryHHG > PeterHHG) And (BarryHHG > AlanHHG) Then
    Image7.Visible = True
ElseIf (AlanHHG > PeterHHG) And (AlanHHG > BarryHHG) Then
    Image11.Visible = True
End If

If (PeterHHS > BarryHHS) And (PeterHHS > AlanHHS) Then
    Image4.Visible = True
ElseIf (BarryHHS > PeterHHS) And (BarryHHS > AlanHHS) Then
    Image8.Visible = True
ElseIf (AlanHHS > PeterHHS) And (AlanHHS > BarryHHS) Then
    Image12.Visible = True
End If

End Sub

Kinest Regards

Peter
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Hi

You can replace this

Code:
'Peters images 
Image1.Visible = False 
Image2.Visible = False 
Image3.Visible = False 
Image4.Visible = False 

'Barrys image 
Image5.Visible = False 
Image6.Visible = False 
Image7.Visible = False 
Image8.Visible = False 

'Alans images 
Image9.Visible = False 
Image10.Visible = False 
Image11.Visible = False 
Image12.Visible = False

With This

Code:
For x = 1 to 12
    UserForm1.Controls("Image" & x).Visible = False
Next x
 
Upvote 0
Hi There

Thanks for that.

Is there a way that I could also reduce the if statements, and perhap dim the variables and set their values to a specific cell reference?

Any help would be very much appreciated.

Kindest Regards

Peter
 
Upvote 0
In a separate part of the worksheet, or in a new sheet build up a table with the three names as the headers and the values HSG, HSS, HHG and HHS values in the rows. Assume the data is A1:D5. In E2, enter the formula:

=MATCH(MAX(B2:D2),B2:D2)

You can then have a simple select case statement for each value in E2 to E5

ps: If a multiple of 3 is added to each of the above results and your image numbers are changed to suit, then even the selece case can be avoided by directly concatanating the Image numbers. (eg. "Image" & [E2] )
 
Upvote 0

Forum statistics

Threads
1,224,582
Messages
6,179,670
Members
452,936
Latest member
anamikabhargaw

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