Struggling with 'If Else' syntax!

  • Thread starter Thread starter Big Chris
  • Start date Start date
B

Big Chris

I'm a real beginner in VBA and I'm trying to write the code which
basically says

If A1 = 1 then run this code

Application.Goto Reference:="dayresultssort"
Selection.Sort Key1:=Range("C9"), Order1:=xlAscending,
Key2:=Range("D9") _
, Order2:=xlDescending, Header:=xlNo, OrderCustom:=1, MatchCase:=False
_
, Orientation:=xlTopToBottom
Range("O9").Select

....which sorts my date within the array 'dayresultssort' by column C.

OR......

If A1=2 then run this code

Application.Goto Reference:="dayresultssort"
Selection.Sort Key1:=Range("G9"), Order1:=xlAscending,
Key2:=Range("D9") _
, Order2:=xlDescending, Header:=xlNo, OrderCustom:=1, MatchCase:=False
_
, Orientation:=xlTopToBottom
Range("O9").Select

......which sorts the same data by column 'G'.

I then want to end by opening my userform 'information_box'.

I've had some great help from here in the past but can't always locate
the helper to offer my appreciation.....so please accept it in
advance.

Regards,
 
If A1 = 1 then run this code

Application.Goto Reference:="dayresultssort"
Selection.Sort Key1:=Range("C9"), Order1:=xlAscending,
Key2:=Range("D9") _
, Order2:=xlDescending, Header:=xlNo, OrderCustom:=1, MatchCase:=False
_
, Orientation:=xlTopToBottom
Range("O9").Select


ElseIf A1=2 then run this code


Application.Goto Reference:="dayresultssort"
Selection.Sort Key1:=Range("G9"), Order1:=xlAscending,
Key2:=Range("D9") _
, Order2:=xlDescending, Header:=xlNo, OrderCustom:=1, MatchCase:=False
_
, Orientation:=xlTopToBottom
Range("O9").Select


End IF


Place code here to open userform 'information_box



Another option is to use Select Case


Select Case A1
Case 1

Application.Goto Reference:="dayresultssort"
Selection.Sort Key1:=Range("C9"), Order1:=xlAscending,
Key2:=Range("D9") _
, Order2:=xlDescending, Header:=xlNo, OrderCustom:=1, MatchCase:=False
_
, Orientation:=xlTopToBottom
Range("O9").Select

Case 2

Application.Goto Reference:="dayresultssort"
Selection.Sort Key1:=Range("G9"), Order1:=xlAscending,
Key2:=Range("D9") _
, Order2:=xlDescending, Header:=xlNo, OrderCustom:=1, MatchCase:=False
_
, Orientation:=xlTopToBottom
Range("O9").Select

End Select


Place code here to open userform 'information_box
 
Big Chris,

Sub TestMe()
If A1 = 1 Then
Application.Goto Reference:="dayresultssort"
Selection.Sort Key1:=Range("C9"), _
Order1:=xlAscending, _
Key2:=Range("D9"), _
Order2:=xlDescending, Header:=xlNo, _
OrderCustom:=1, MatchCase:=False, _
Orientation:=xlTopToBottom
Range("O9").Select
information_box.show
Else
If A1=2 Then
Application.Goto Reference:="dayresultssort"
Selection.Sort Key1:=Range("G9"), _
Order1:=xlAscending, _
Key2:=Range("D9"), _
Order2:=xlDescending, Header:=xlNo, _
OrderCustom:=1, MatchCase:=False, _
Orientation:=xlTopToBottom
Range("O9").Select
information_box.show
End If
End If
End Sub
I've had some great help from here in the past but can't always locate
the helper to offer my appreciation.....so please accept it in
advance.
Just reply to the same thread with a Thank you if someone has helped you.

John



Big Chris said:
I'm a real beginner in VBA and I'm trying to write the code which
basically says

If A1 = 1 then run this code

Application.Goto Reference:="dayresultssort"
Selection.Sort Key1:=Range("C9"), Order1:=xlAscending,
Key2:=Range("D9") _
, Order2:=xlDescending, Header:=xlNo, OrderCustom:=1, MatchCase:=False
_
, Orientation:=xlTopToBottom
Range("O9").Select

...which sorts my date within the array 'dayresultssort' by column C.

OR......

If A1=2 then run this code

Application.Goto Reference:="dayresultssort"
Selection.Sort Key1:=Range("G9"), Order1:=xlAscending,
Key2:=Range("D9") _
, Order2:=xlDescending, Header:=xlNo, OrderCustom:=1, MatchCase:=False
_
, Orientation:=xlTopToBottom
Range("O9").Select

.....which sorts the same data by column 'G'.

I then want to end by opening my userform 'information_box'.

I've had some great help from here in the past but can't always locate
the helper to offer my appreciation.....so please accept it in
advance.

Regards,


------------------------------------------------



~~Now Available: Financial Statements.xls, a step by step guide to
creating financial statements
 
Here is a sub that I use to sort on the column that is double clicked on row
4

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As
Boolean)
If Target.Row = 4 Then
Range("a5:g" & Range("a65536").End(xlUp).Row) _
..Sort Key1:=Cells(5, ActiveCell.Column), Order1:=xlAscending,
Orientation:=xlTopToBottom
Cells(5, Target.Column).Select
End If
end sub
===
or
[CHECKSSORT].Sort Key1:=Cells(1, ActiveCell.Column), _
Order1:=xlAscending, Orientation:=xlTopToBottom
===

so maybe yours could be

x=Choose([b1], "c9", "g9")
range(dayresultsort).Sort Key1:=Range(x), Order1:=xlAscending,
Key2:=Range("D9") _
, Order2:=xlDescending, Header:=xlNo, OrderCustom:=1, MatchCase:=False _
, Orientation:=xlTopToBottom

--
Don Guillett
SalesAid Software
(e-mail address removed)
Big Chris said:
I'm a real beginner in VBA and I'm trying to write the code which
basically says

If A1 = 1 then run this code

Application.Goto Reference:="dayresultssort"
Selection.Sort Key1:=Range("C9"), Order1:=xlAscending,
Key2:=Range("D9") _
, Order2:=xlDescending, Header:=xlNo, OrderCustom:=1, MatchCase:=False
_
, Orientation:=xlTopToBottom
Range("O9").Select

...which sorts my date within the array 'dayresultssort' by column C.

OR......

If A1=2 then run this code

Application.Goto Reference:="dayresultssort"
Selection.Sort Key1:=Range("G9"), Order1:=xlAscending,
Key2:=Range("D9") _
, Order2:=xlDescending, Header:=xlNo, OrderCustom:=1, MatchCase:=False
_
, Orientation:=xlTopToBottom
Range("O9").Select

.....which sorts the same data by column 'G'.

I then want to end by opening my userform 'information_box'.

I've had some great help from here in the past but can't always locate
the helper to offer my appreciation.....so please accept it in
advance.

Regards,


------------------------------------------------



~~Now Available: Financial Statements.xls, a step by step guide to
creating financial statements
 
Back
Top