【问题标题】:Macro for excel VBA will not run second sub in same moduleexcel VBA的宏不会在同一模块中运行第二个子
【发布时间】:2018-06-03 03:46:35
【问题描述】:

以下是我目前用于某些发票标签的宏。工作簿中的第一个工作表设置为使用超链接的工作表名称进行索引。此模块设置为由 crtl+shift+n 激活。第二个子在我尝试的前几次工作中工作,但现在它没有将数据从填充的前一行复制到填充的第一个空行。似乎它在第一个子之后停止了。有什么想法吗?

Option Explicit

Sub NewRequistionRecord()
'
' NewRequistionRecord Macro
' Used for creaing a new requistion record to be auto updated in master 
Requisitions worksheet
'
' Keyboard Shortcut: Ctrl+Shift+N
'

Dim wb As Workbook: Set wb = ThisWorkbook
Dim ws As Worksheet: Set ws = wb.sheets("BlankReq")
Dim wbBefore As Workbook: Set wbBefore = Application.Workbooks("OPP Stores 
Orders Macro.xlsm")
Dim wsBefore As Worksheet: Set wsBefore = wbBefore.sheets("BlankReq")
Dim answer
Dim NewName As String
Application.ScreenUpdating = False

    ws.Select 'Goes to template worksheet


    ws.Copy Before:=wsBefore ' Forces a copy to be made always before the template so it is always at the end    
sheets("BlankReq (2)").Select
sheets("BlankReq (2)").Name = "Enter Req Number" ' changes the name to indicate a requisition number needs to be entered
Range("A1").Select ' hyperlink goes back  Master Req index page
Selection.Hyperlinks(1).Follow NewWindow:=False, AddHistory:=True

Application.ScreenUpdating = True

sheets("Enter Req Number").Select
answer = MsgBox("Please rename the sheet with the Requisition number", vbOK)

    If answer = vbOK Then

Another:
    NewName = InputBox("Requisition number - ?")
    ActiveSheet.Name = NewName

    Range("D2").Select
    NewName = InputBox("Requisition Description- NO SPACES!! USE UNDERSCORE ?")
    Range("D2").Value = NewName
    wsBefore.Select

    Range("H2").Select
    NewName = InputBox("Please enter Requested By ")
    Range("H2").Value = NewName
    wsBefore.Select
    answer = MsgBox("Please select your data to copy and paste into this sheet. Line Cost must be selected seperately from the items.", vbOKOnly)

    End If

End Sub


Sub Filldown()
Dim strFormulas(1 To 6) As Variant
Dim wb As Workbook: Set wb = ThisWorkbook
Dim ws As Worksheet: Set ws = wb.ActiveSheet
Dim wbBefore As Workbook: Set wbBefore = Application.Workbooks("OPP StoresOrders Macro.xlsm")
Dim wsBefore As Worksheet: Set wsBefore = wbBefore.sheets("BlankReq")
Dim LRow As Long
Dim xRow As Variant
Application.ScreenUpdating = False

        With ThisWorkbook.sheets("Requisitions")
        Range("A1").Select
        Columns("B:K").Select
        Selection.SpecialCells(xlCellTypeBlanks).Select
        Selection.FormulaR1C1 = "=R[-1]C"
        End With




Application.ScreenUpdating = True

End Sub 

【问题讨论】:

    标签: excel indexing hyperlink autofill vba


    【解决方案1】:

    快捷方式可能设置为 Sub,而不是 Module。因此,第二个 sub 默认情况下可能没有运行。

    要先运行第一个子程序,然后运行第二个子程序,请尝试以下操作:

    Sub TestMe()
        NewRequistionRecord
        FillDown
    End Sub
    

    【讨论】:

      【解决方案2】:

      您根本没有调用第二个宏。所以它会运行代码,并且在任何时候都没有得到消息来运行第二个宏。您需要以某种方式告诉您的程序运行第二个宏 - 我喜欢 call Macroname

      【讨论】:

      • 感谢您的建议。在这方面我还是很陌生。
      • 嘿,我们都从某个地方开始!把模块想象成电子表格——你有它们,它们存储代码。模块中的每个 sub 都是一个单独的宏。您可以为每个单独的宏创建快捷方式/按钮。如果你想一次使用多个宏,你需要一个“包装”宏来保存它们——比如 Sub Wrapper() Call Macro1 Call Macro2 End sub
      • 只是提到Call 语句已被弃用,不应再使用。相反,只需写Macroname(无需致电)。
      • 当你有多输入功能时仍然需要调用。我也是它的粉丝,因为它明确显示并提醒“嘿,这是在抓取另一个宏,记得吗?”现在您提到不应该使用它 - 有没有理由避免使用它?
      猜你喜欢
      • 1970-01-01
      • 2022-11-26
      • 2017-07-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-27
      • 1970-01-01
      • 2020-05-09
      相关资源
      最近更新 更多