当我发送给同事时,如何创建可以正常工作的VBA宏?

时间:2018-10-08 13:26:30

标签: excel vba excel-vba

我在工作中为数据库创建了VBA代码,我的同事和我将使用该代码存储有关我们正在处理的案例的数据。但是,当我共享Excel时,宏不会自动运行。 为了使其正常工作,我的同事需要进入“宏”,然后将“宏在:”更改为“(文档名称)”,而不是能够使用默认设置“所有打开的工作簿”。 我有什么办法可以修复原始宏,以便在与同事共享时可以运行该宏而无需为每个输入进行调整? 这个问题在这个论坛上可能有点“基本”,但是对任何帮助都是非常感激的。 谢谢!

p.s。如果您需要更多信息来诊断此问题,请告诉我。

Private Sub CommandButton1_Click()
Range("A1").Value = Range("A1").Value + 1
End Sub
Sub Macro1()
'
' basic variable types: strings, integers & longs
Dim ws As Worksheet
Dim lastRow As Long
Dim financing As String
Dim compName As String
Dim wrkSht As Worksheet
Dim fortnr As String
Dim lr As Long


    Set ws = Sheets("INPUT")

    financing = ws.Range("B2").Value
    compName = ws.Range("B3").Value
    fortnr = compName & "-" & financing

    lastRow = ws.Cells(Rows.Count, "B").End(xlUp).Row + 1
    ws.Cells(lastRow, "B") = financing
    ws.Cells(lastRow, "C") = compName
'
'    ActiveWorkbook.Sheets.Add After:=Worksheets(Worksheets.Count)
'    ActiveWorkbook.Sheets(Worksheets.Count).Name = compName & "-" & financing

    ActiveWorkbook.Worksheets("Template").Copy After:=Worksheets("Template")
    ActiveWorkbook.Sheets("Template").Name = compName & "-" & financing
    ActiveWorkbook.Sheets(compName & "-" & financing).Visible = xlSheetVisible

    ActiveWorkbook.Sheets("Template (2)").Name = "Template"

    ActiveWorkbook.Sheets(fortnr).Select

    ActiveWorkbook.Sheets(fortnr).Range("C4").Value = financing
    ActiveWorkbook.Sheets(fortnr).Range("C5").Value = compName

    ws.Cells(lastRow, "D").Formula = "='" & fortnr & "'!$C$7"
    ws.Cells(lastRow, "E").Formula = "='" & fortnr & "'!$C$15"
    ws.Cells(lastRow, "F").Formula = "='" & fortnr & "'!$C$10"
    ws.Cells(lastRow, "G").Formula = "='" & fortnr & "'!$C$11"
    ws.Cells(lastRow, "H").Formula = "='" & fortnr & "'!$C$12"
    ws.Cells(lastRow, "I").Formula = "='" & fortnr & "'!$C$6"
    ws.Cells(lastRow, "J").Formula = "='" & fortnr & "'!$C$14"
    ws.Cells(lastRow, "L").Formula = "='" & fortnr & "'!$C$19"
    ws.Cells(lastRow, "M").Formula = "='" & fortnr & "'!$C$17"
    ws.Cells(lastRow, "N").Formula = "='" & fortnr & "'!$C$21"
    ws.Cells(lastRow, "O").Formula = "='" & fortnr & "'!$C$22"
    ws.Cells(lastRow, "Q").Formula = "='" & fortnr & "'!$C$25"
    ws.Cells(lastRow, "R").Formula = "='" & fortnr & "'!$C$26"
    ws.Cells(lastRow, "S").Formula = "='" & fortnr & "'!$C$27"
    ws.Cells(lastRow, "T").Formula = "='" & fortnr & "'!$C$28"
    ws.Cells(lastRow, "U").Formula = "='" & fortnr & "'!$C$29"
    ws.Cells(lastRow, "V").Formula = "='" & fortnr & "'!$C$30"
    ws.Cells(lastRow, "W").Formula = "='" & fortnr & "'!$C$31"
    ws.Cells(lastRow, "X").Formula = "='" & fortnr & "'!$C$32"
    ws.Cells(lastRow, "K").Formula = "='" & fortnr & "'!$C$16"
    ws.Cells(lastRow, "P").Formula = "='" & fortnr & "'!$C$20"

    'ws.Cells(lastRow, "D") = Sheets(fortnr).Range("B6").Value
    'ws.Cells(lastRow, "E") = Sheets(fortnr).Range("B7").Value

    'ws.Cells(lastRow, "D") = Sheets(fortnr).Range("B6").Address
    'ws.Cells(lastRow, "E") = Sheets(fortnr).Range("B7").Address

    ActiveSheet.Hyperlinks.Add Anchor:=ws.Cells(lastRow, 1), Address:="", SubAddress:= _
        "'" & fortnr & "'" & "!A1", TextToDisplay:="Check"                                  'Anchor: the place where the link will be

    ActiveSheet.Hyperlinks.Add Sheets(compName & "-" & financing).Range("A1"), "", Sheets("INPUT").Name & "!A1", TextToDisplay:="Back to Input-sheet"

End Sub

0 个答案:

没有答案