VBA 从另一个工作表调用标签

问题描述

只是我需要知道是否可以调用位于同一工作簿上另一张工作表中的特定标签

我知道我可以很容易地在同一张纸上调用标签,但是另一张纸上的标签呢??

或者有其他类似的方法可以做到这一点

例如: 通常,当我们运行一个代码时,我们可以通过调用它的子程序名称调用一个模块代码,例如,Label 是一种跳过行并从特定行开始的方法

Dim AnswerP As String
    AnswerP = Application.InputBox("Do You Have a file to be uploaded! Please type yes ",Default:="yes",Title:="Checking File Existance!")
    'If Answer is Yes Upload file and Goto Another File (Mobile label)
    If AnswerP = "yes" Then    
    Z_Import_Package_2        'Calling another module code by its name
    Dim UploadedP As String
    UploadedP = "Selected"
    GoTo Mobile  'Label
    ElseIf AnswerP = "no" Then     'Checking Next Mobile File
    UploadedP = "Skipped"

Dim LastRowCP As Long
LastRowCP = Ssheet4.Cells(Rows.Count,"C").End(xlUp).Offset(1).Row
Ssheet4.Range("C3:C" & LastRowCP).copy Destination:=DestShtBPS.Cells(LR_DestShtB,"C")

MobileF:
    Dim AnswerM As String
    AnswerM = Application.InputBox("Do You Have another File to be uploaded! Please type yes ",Title:="Checking File Existance!")
    If AnswerM = "yes" Then    
    Z_Import_Mobile_2                     'Calling another module code by its name

我可以直接从另一张纸上调用 MobileF Label 吗 因为调用子程序标签会从它的开始调用完整的代码,这并不可怕

解决方法

Goto 对于流量控制来说并不是一个很好的方法:我们有很多更好的选择。关于 Goto 在错误处理之外的唯一需要是打破多个嵌套循环。建议您重构代码以使用常规流程控制(if/then),并将您需要从多个位置调用的任何代码分解为独立方法(Sub 或 Function)

If MsgBox("Do You Have a file to be uploaded?",_
          vbQuestion + vbYesNo,"Checking File") = vbYes Then
    Z_Import_Package_2        'Calling another module code by its name
    Dim UploadedP As String
    UploadedP = "Selected"
    MobileF  'call MobileF
Else
    UploadedP = "Skipped"
    Dim LastRowCP As Long
    LastRowCP = Ssheet4.Cells(Rows.Count,"C").End(xlUp).Offset(1).Row
    Ssheet4.Range("C3:C" & LastRowCP).Copy Destination:=DestShtBPS.Cells(LR_DestShtB,"C")
    '...
    '...

将 MobileF 移至独立的 Sub:

Sub MobileF()
    If MsgBox("Do You Have another File to be uploaded?",_
              vbQuestion + vbYesNo,_
              "Checking File Existance!") = vbYes Then
        Z_Import_Mobile_2
    End If
End Sub

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...