如果那么有例外

时间:2013-08-02 15:46:27

标签: excel exception

除非Station2中没有数据,否则此宏效果很好。宏分为三个部分:Station1,Station2和Station3。我希望它运行如下,但如果Station2中没有任何内容,则跳到最后。然后我可以为Station3的部分重复这一行。

Sub PTPMLRs()
Dim MyHost As Object
Dim HE As Object
Set HE = CreateObject("HostExplorer")
Set MyHost = HE.CurrentHost
Dim RC As Integer

Dim Lsheet As Worksheet
Set Lsheet = ThisWorkbook.Worksheets("PTPMLRs")


Dim Station1 As String
Dim Station1Loc As Range

Dim Station2 As String
Dim Station2Loc As Range

Dim Station3 As String
Dim Station3Loc As Range

Dim DestinationE As String
Dim DestinationELoc As Range

Dim DestinationN As String
Dim DestinationNLoc As Range

Dim DestinationA As String
Dim DestinationALoc As Range

Dim Vehicle As String
Dim VehicleLoc As Range

Dim AllVeh As String
Dim AllVehLoc As Range

Dim BeginDate As String
Dim BeginDateLoc As Range

Dim EndDate As String
Dim EndDateLoc As Range

Dim BeginTime As String
Dim BeginTimeLoc As Range

Dim EndTime As String
Dim EndTimeLoc As Range

Dim Inv As String
Dim InvLoc As Range

Dim Mlr As String
Dim MlrLoc As Range

Set Station1Loc = Lsheet.Range("I6")
Set Station2Loc = Lsheet.Range("M6")
Set Station3Loc = Lsheet.Range("Q6")

Set DestinationELoc = Lsheet.Range("I8")
Set DestinationNLoc = Lsheet.Range("M8")
Set DestinationALoc = Lsheet.Range("Q8")

Set VehicleLoc = Lsheet.Range("E6")
Set AllVehLoc = Lsheet.Range("E8")
Set BeginDateLoc = Lsheet.Range("E10")
Set EndDateLoc = Lsheet.Range("E12")
Set BeginTimeLoc = Lsheet.Range("E14")
Set EndTimeLoc = Lsheet.Range("E16")
Set InvLoc = Lsheet.Range("E18")
Set MlrLoc = Lsheet.Range("E20")

     Station1 = Station1Loc.Value
     Station2 = Station2Loc.Value
     Station3 = Station3Loc.Value

     DestinationE = DestinationELoc.Value
     DestinationN = DestinationNLoc.Value
     DestinationA = DestinationALoc.Value




     RC = MyHost.Runcmd("pF2")
     RC = MyHost.keys(Station1)
     RC = MyHost.Runcmd("pF3")
     RC = MyHost.Runcmd("TAB")
     RC = MyHost.Runcmd("TAB")
     RC = MyHost.Runcmd("TAB")



     Do Until DestinationELoc.Value = ""

     Vehicle = VehicleLoc.Value
     AllVeh = AllVehLoc.Value
     BeginDate = BeginDateLoc.Value
     EndDate = EndDateLoc.Value
     BeginTime = BeginTimeLoc.Value
     EndTime = EndTimeLoc.Value
     Inv = InvLoc.Value
     Mlr = MlrLoc.Value




     RC = MyHost.Runcmd("pF2")
     RC = MyHost.keys(DestinationE)
     RC = MyHost.Runcmd("pF3")
     RC = MyHost.Runcmd("PAGE-DOWN")
     RC = MyHost.Runcmd("INSERT-HERE")
     RC = MyHost.Runcmd("TAB")
     RC = MyHost.Runcmd("TAB")
     RC = MyHost.keys(Vehicle)
     RC = MyHost.Runcmd("TAB")
     RC = MyHost.keys(BeginDate)
     RC = MyHost.Runcmd("TAB")
     RC = MyHost.keys(EndDate)
     RC = MyHost.Runcmd("PAGE-DOWN")
     RC = MyHost.keys(BeginTime)
     RC = MyHost.Runcmd("TAB")
     RC = MyHost.keys(EndTime)
     RC = MyHost.Runcmd("TAB")
     RC = MyHost.keys(Inv)
     RC = MyHost.Runcmd("TAB")
     RC = MyHost.keys(Mlr)
     RC = MyHost.Runcmd("pF4")
     RC = MyHost.keys(AllVeh)
     RC = MyHost.Runcmd("ENTER")
     RC = MyHost.Runcmd("ENTER")
     RC = MyHost.Runcmd("PAGE-UP")
     RC = MyHost.Runcmd("PAGE-UP")



        Set DestinationELoc = DestinationELoc.Offset(2, 0)
        DestinationE = DestinationELoc.Value



Loop

     RC = MyHost.Runcmd("PAGE-UP")
     RC = MyHost.Runcmd("pF2")
     RC = MyHost.keys(Station2)
     RC = MyHost.Runcmd("pF3")
     RC = MyHost.Runcmd("TAB")
     RC = MyHost.Runcmd("TAB")
     RC = MyHost.Runcmd("TAB")

     Do Until DestinationNLoc.Value = ""

     Vehicle = VehicleLoc.Value
     AllVeh = AllVehLoc.Value
     BeginDate = BeginDateLoc.Value
     EndDate = EndDateLoc.Value
     BeginTime = BeginTimeLoc.Value
     EndTime = EndTimeLoc.Value
     Inv = InvLoc.Value
     Mlr = MlrLoc.Value




     RC = MyHost.Runcmd("pF2")
     RC = MyHost.keys(DestinationN)
     RC = MyHost.Runcmd("pF3")
     RC = MyHost.Runcmd("PAGE-DOWN")
     RC = MyHost.Runcmd("INSERT-HERE")
     RC = MyHost.Runcmd("TAB")
     RC = MyHost.Runcmd("TAB")
     RC = MyHost.keys(Vehicle)
     RC = MyHost.Runcmd("TAB")
     RC = MyHost.keys(BeginDate)
     RC = MyHost.Runcmd("TAB")
     RC = MyHost.keys(EndDate)
     RC = MyHost.Runcmd("PAGE-DOWN")
     RC = MyHost.keys(BeginTime)
     RC = MyHost.Runcmd("TAB")
     RC = MyHost.keys(EndTime)
     RC = MyHost.Runcmd("TAB")
     RC = MyHost.keys(Inv)
     RC = MyHost.Runcmd("TAB")
     RC = MyHost.keys(Mlr)
     RC = MyHost.Runcmd("pF4")
     RC = MyHost.keys(AllVeh)
     RC = MyHost.Runcmd("ENTER")
     RC = MyHost.Runcmd("ENTER")
     RC = MyHost.Runcmd("PAGE-UP")
     RC = MyHost.Runcmd("PAGE-UP")



        Set DestinationNLoc = DestinationNLoc.Offset(2, 0)
        DestinationN = DestinationNLoc.Value

Loop

     RC = MyHost.Runcmd("PAGE-UP")
     RC = MyHost.Runcmd("pF2")
     RC = MyHost.keys(Station3)
     RC = MyHost.Runcmd("pF3")
     RC = MyHost.Runcmd("TAB")
     RC = MyHost.Runcmd("TAB")
     RC = MyHost.Runcmd("TAB")

     Do Until DestinationALoc.Value = ""

     Vehicle = VehicleLoc.Value
     AllVeh = AllVehLoc.Value
     BeginDate = BeginDateLoc.Value
     EndDate = EndDateLoc.Value
     BeginTime = BeginTimeLoc.Value
     EndTime = EndTimeLoc.Value
     Inv = InvLoc.Value
     Mlr = MlrLoc.Value




     RC = MyHost.Runcmd("pF2")
     RC = MyHost.keys(DestinationA)
     RC = MyHost.Runcmd("pF3")
     RC = MyHost.Runcmd("PAGE-DOWN")
     RC = MyHost.Runcmd("INSERT-HERE")
     RC = MyHost.Runcmd("TAB")
     RC = MyHost.Runcmd("TAB")
     RC = MyHost.keys(Vehicle)
     RC = MyHost.Runcmd("TAB")
     RC = MyHost.keys(BeginDate)
     RC = MyHost.Runcmd("TAB")
     RC = MyHost.keys(EndDate)
     RC = MyHost.Runcmd("PAGE-DOWN")
     RC = MyHost.keys(BeginTime)
     RC = MyHost.Runcmd("TAB")
     RC = MyHost.keys(EndTime)
     RC = MyHost.Runcmd("TAB")
     RC = MyHost.keys(Inv)
     RC = MyHost.Runcmd("TAB")
     RC = MyHost.keys(Mlr)
     RC = MyHost.Runcmd("pF4")
     RC = MyHost.keys(AllVeh)
     RC = MyHost.Runcmd("ENTER")
     RC = MyHost.Runcmd("ENTER")
     RC = MyHost.Runcmd("PAGE-UP")
     RC = MyHost.Runcmd("PAGE-UP")



        Set DestinationALoc = DestinationALoc.Offset(2, 0)
        DestinationA = DestinationALoc.Value

Loop

     RC = MyHost.Runcmd("PAGE-UP")
     RC = MyHost.Runcmd("pF2")

End Sub

1 个答案:

答案 0 :(得分:0)

您应该能够将if语句与goto语句结合使用。