保存并关闭后,VBA宏丢失为xslm

时间:2016-09-29 09:16:40

标签: excel vba excel-vba macros

我的情况:我有一个Excel工作簿应该用xslm作为文件扩展名创建一个新的工作簿,编辑单个工作表的名称并在其中放入一些数据。

之后创建一个ComboBox并且ComboBox充满了数据,并且在新的Workbook中创建了一个具有ComboBox1_Change Sub的宏。

完成所有这些后,新工作簿将被保存并关闭。

现在问题是,如果我重新打开工作簿,ComboBox为空,并且工作簿中的宏不再存在。 经过一些研究后,我发现这个问题与人们将工作表保存为xlsx而不是xslm有关,但我确保我的代码不是这种情况。

也许我忽视了一些事情,但我甚至在关闭命令上做了一个断点来检查一切是否有效,并且在那之前它应该做什么。所以问题必须在关闭和保存之内。 我甚至尝试保存并单独关闭它或将新工作簿设置为活动并保存。 也许这里有人可以帮助我。

编辑:我正在使用Excel 2007

先谢谢,代码如下:

创建新工作簿的Sub:

Sub createTemplate()
Dim currentWB As Workbook
Set currentWB = ThisWorkbook
Dim template As Workbook
Dim curResSht As Worksheet
Dim temUtiSht As Worksheet
Dim curHolSht As Worksheet
Dim temHolSht As Worksheet
Workbooks.Add
Set curResSht = currentWB.Worksheets("Resource Plan")
Set curHolSht = currentWB.Worksheets("Holidays")
Application.DisplayAlerts = False
ActiveWorkbook.SaveAs Filename:=currentWB.Path & "\Template.xlsm", FileFormat:=52
Set template = ActiveWorkbook
template.Sheets(3).name = "Holidays"
template.Sheets(2).name = "Person List"
template.Sheets(2).Visible = False
template.Sheets(1).name = "Utilisation"
Set temUtiSht = template.Worksheets("Utilisation")
Set temHolSht = template.Worksheets("Holidays")
For i = 1 To 4
    curResSht.Rows(i).EntireRow.Copy
    temUtiSht.Rows(i).EntireRow.PasteSpecial
Next i

For i = 1 To 3
    curHolSht.Rows(i).EntireRow.Copy
    temHolSht.Rows(i).EntireRow.PasteSpecial
Next i

Application.DisplayAlerts = True

Dim curDatSht As Worksheet
Dim temLisSht As Worksheet
Set curDatSht = currentWB.Worksheets("Data")
Set temLisSht = template.Worksheets("Person List")
i = 5
Dim j As Integer
j = 1
While Not IsEmpty(curResSht.Cells(i, 1))
    If curResSht.Cells(i, 1).Interior.Color = curDatSht.Cells(2, 1).Interior.Color Then GoTo skipLine
    If curResSht.Cells(i, 3).Value = "Total" Then GoTo skipLine
    For k = 1 To 7
        curResSht.Cells(i, k).Copy
        temLisSht.Cells(j, k).PasteSpecial
    Next k
    j = j + 1
skipLine:
    i = i + 1
Wend

Set temCom = temUtiSht.OLEObjects.Add(ClassType:="Forms.ComboBox.1", Link:=False, DisplayAsIcon:=False, Left:=49, Top:=30, Width:=200, Height:=25)
template.Save
template.Close
Workbooks.Open (currentWB.Path & "\Template.xlsm")
Call fillEmpInLis
Call createCodeInTemplate
Application.Workbooks("Template.xlsm").Close savechanges:=True
End Sub

宏创建子:

Sub createCodeInTemplate()
Dim tmWB As Workbook
Dim tmVP As VBProject
Dim tmVC As VBComponent
Dim tmCM As CodeModule

Set tmWB = Application.Workbooks("Template.xlsm")
Set tmVP = tmWB.VBProject
Set tmVC = tmVP.VBComponents("Sheet1")
Set tmCM = tmVC.CodeModule

With tmCM
    .InsertLines 1, "Private Sub ComboBox1_Change()"
    .InsertLines 2, vbTab & "Dim sht As Worksheet"
    .InsertLines 3, vbTab & "Dim sht2 As Worksheet"
    .InsertLines 4, vbTab & "Dim sht3 As Worksheet"
    .InsertLines 5, vbTab & "Set sht = ThisWorkbook.Worksheets(""Utilisation"")"
    .InsertLines 6, vbTab & "Set sht2 = ThisWorkbook.Worksheets(""Person List"")"
    .InsertLines 7, vbTab & "Set sht3 = ThisWorkbook.Worksheets(""Holidays"")"
    .InsertLines 8, vbTab & "Dim k As Integer"
    .InsertLines 9, vbTab & "k = 4"
    .InsertLines 10, vbTab & "Dim i As Integer"
    .InsertLines 11, vbTab & "i = 5"
    .InsertLines 12, vbTab & "Dim j As Integer"
    .InsertLines 13, vbTab & "j = 1"
    .InsertLines 14, vbTab & "While Not IsEmpty(sht.Cells(i, 1))"
    .InsertLines 15, vbTab & vbTab & "i = i + 1"
    .InsertLines 16, vbTab & "Wend"
    .InsertLines 17, vbTab & "While Not IsEmpty(sht3.Cells(k, 1))"
    .InsertLines 18, vbTab & vbTab & "k = k + 1"
    .InsertLines 19, vbTab & "Wend"
    .InsertLines 20, vbTab & "While Not IsEmpty(sht2.Cells(j, 1))"
    .InsertLines 21, vbTab & vbTab & "If sht2.Cells(j, 1).Value = ComboBox1.Value Then"
    .InsertLines 22, vbTab & vbTab & vbTab & "sht2.Rows(j).EntireRow.Copy"
    .InsertLines 23, vbTab & vbTab & vbTab & "sht.Rows(i).EntireRow.PasteSpecial"
    .InsertLines 24, vbTab & vbTab & vbTab & "sht2.Cells(i, 1).Copy"
    .InsertLines 25, vbTab & vbTab & vbTab & "sht3.Cells(k, 2).PasteSpecial"
    .InsertLines 26, vbTab & vbTab & vbTab & "sht2.Cells(i, 2).Copy"
    .InsertLines 27, vbTab & vbTab & vbTab & "sht3.Cells(k, 1).PasteSpecial"
    .InsertLines 28, vbTab & vbTab & vbTab & "k = k + 1"
    .InsertLines 29, vbTab & vbTab & vbTab & "i = i + 1"
    .InsertLines 30, vbTab & vbTab & "End If"
    .InsertLines 31, vbTab & vbTab & "j = j + 1"
    .InsertLines 32, vbTab & "Wend"
    .InsertLines 33, "End Sub"
End With
End Sub

Sub填充ComboBox:

Sub fillEmpInLis()
Dim wb As Workbook
Dim utiSht As Worksheet
Dim perSht As Worksheet
Set wb = Application.Workbooks("Template.xlsm")
Set utiSht = wb.Worksheets("Utilisation")
Set perSht = wb.Worksheets("Person List")
Set box = wb.Sheets("Utilisation").ComboBox1
box.Clear
Dim i As Integer
i = 2

box.AddItem perSht.Cells(1, 1).Value
While Not IsEmpty(perSht.Cells(i, 1))
    Dim resultIndex As Boolean
    resultIndex = False
    For j = 0 To box.ListCount - 1
        If box.List(j) = perSht.Cells(i, 1).Value Then
            resultIndex = True
        End If
    Next j
    If resultIndex = False Then
        For j = 0 To box.ListCount - 1
            If perSht.Cells(i, 1) < box.List(j) And j = 0 Then
                box.AddItem perSht.Cells(i, 1), j
                GoTo skip
            ElseIf perSht.Cells(i, 1) > box.List(j) And j = box.ListCount - 1 Then
                box.AddItem perSht.Cells(i, 1)
                GoTo skip
            ElseIf perSht.Cells(i, 1) > box.List(j) And perSht.Cells(i, 1) < box.List(j + 1) Then
                box.AddItem perSht.Cells(i, 1), j
                GoTo skip
            End If
        Next j
skip:
    End If
    i = i + 1
Wend
End Sub

1 个答案:

答案 0 :(得分:0)

我希望这会有所帮助:

我的猜测是保存时模板中没有正确创建宏代码。所以我只是将你的代码复制到一个空的工作簿中,并在创建宏代码的Sub中进行了这些更改:

Sub createCodeInTemplate()
  Dim tmWB As Workbook

    Set tmWB = Application.Workbooks("Template.xlsm")

    With tmWB.VBProject.VBComponents(tmWB.Sheets("Utilisation").CodeName).CodeModule

然后我测试了完整的代码,并且创建的文件(Template.xlsm)包含宏代码。

由于我使用的是Excel 2013,它可能对您没有帮助,但我会尝试一下。