2016-09-29 191 views
0

我的情況:我有一個Excel工作簿應該創建一個帶有xslm作爲文件擴展名的新工作簿,編輯單張工作表的名稱並在其中放入一些數據。保存並關閉後VBA宏丟失xslm

之後,ComboBox被創建並且ComboBox被填充數據並且在新的工作簿中創建了一個具有ComboBox1_Change Sub的宏。

當所有這些都完成後,新的工作簿被保存並關閉。

現在的問題是,如果我重新打開工作簿,ComboBox是空的,宏不再在工作簿中。 經過一番研究,我發現這個問題只有人們將表格保存爲xlsx而不是xslm,但我確信這不是我的代碼。

也許我忽略了一些東西,但我甚至在關閉命令中做了一個斷點來檢查是否所有的東西都起作用了,並且它在那之前做了它應該做的事情。所以問題必須在關閉和保存之內。 我甚至試圖保存並單獨關閉它或設置新的工作簿並保存。 也許有人在這裏可以幫助我。

編輯:我使用Excel 2007

在此先感謝的代碼如下:

小組來創建新的工作簿:

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填充組合框:

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 
+0

您使用的是什麼版本的Excel? – Clusks

+1

手動創建包含代碼和控件的模板工作簿會不會更容易,然後在您的代碼中使用它?如果用戶將Excel選項設置爲使用單張工作表創建新工作簿,則代碼也會失敗 - 不會有工作表重命名「假期」和「人員列表」。 –

+0

我添加了我正在使用的Excel版本(2007)。 @ DarrenBartrup-Cook是的,這可能會更容易,但我這樣做,因爲如果主工作簿中有變化,模板將不得不改變,並且用戶按下按鈕以更快更容易地創建新版本,而不是每次手動創建新版本 – Bumpf

回答

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,它可能不會幫助你,但我會試一試。

+0

感謝您的回答,但它不適合我。主要的鬥爭點仍然在保存和關閉我猜,因爲如果我不關閉它的代碼是在工作表中。 即使我在檢查代碼並重新打開後手動保存它,代碼也不見了。 – Bumpf