2
我的公司使用excel生成網絡配置,這些配置會發送到我們所有的商店。配置與在Excel中輸入到變量表中的一些變量的例外幾乎相同。然後用戶單擊一個命令按鈕,然後將這些變量放入配置中,並在工作簿中創建一個新工作表。多年來這一直爲我們工作。但是,我們最近升級到Office 2013,現在我們的配置模板都不起作用。代碼沒有太多,所以這可能相對容易,但我不是程序員。這裏是代碼:Excel宏升級到Office 2013後無法正常工作
Public Sub ReplaceValues(OriginalSheetName As String, VariableSheetName As String, NewSheetName As String)
Dim iVariableRowCounter As String
Dim sSearchValue As String
Dim sReplacementValue As String
Dim iControlCounter As Integer
ThisWorkbook.Sheets(OriginalSheetName).Copy after:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)
ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count).Name = NewSheetName
For iControlCounter = ThisWorkbook.Sheets(NewSheetName).Shapes.Count To 1 Step -1
If ThisWorkbook.Sheets(NewSheetName).Shapes(iControlCounter).Type = msoOLEControlObject Then
ThisWorkbook.Sheets(NewSheetName).Shapes(iControlCounter).Delete
End If
Next
iVariableRowCounter = 2
While ThisWorkbook.Sheets(VariableSheetName).Cells(iVariableRowCounter, 1).Value <> ""
sSearchValue = ThisWorkbook.Sheets(VariableSheetName).Cells(iVariableRowCounter, 1).Value
sReplacementValue = ThisWorkbook.Sheets(VariableSheetName).Cells(iVariableRowCounter, 2).Value
ThisWorkbook.Sheets(NewSheetName).UsedRange.Replace what:=sSearchValue, replacement:=sReplacementValue, searchorder:=xlByColumns, MatchCase:=False, lookat:=xlPart
iVariableRowCounter = iVariableRowCounter + 1
Wend
End Sub
Public Function GenerateNewWorksheetName(OriginalSheetName As String) As String
Dim sNewSheetName As String
Dim iIncrement As Integer
Dim iSheetCounter As Integer
Dim bGoodName As Boolean
Dim bSheetFound As Boolean
iIncrement = 1
bGoodName = False
While Not bGoodName
sNewSheetName = OriginalSheetName & " - " & iIncrement
bSheetFound = False
For iSheetCounter = 1 To ThisWorkbook.Sheets.Count
If ThisWorkbook.Sheets(iSheetCounter).Name = sNewSheetName Then
bSheetFound = True
Exit For
End If
Next
If Not bSheetFound Then
bGoodName = True
End If
iIncrement = iIncrement + 1
Wend
GenerateNewWorksheetName = sNewSheetName
End Function
任何幫助,非常感謝。謝謝。
什麼不行?錯誤的值或者你是否得到錯誤?還有別的嗎? – user2140261 2013-05-01 19:08:26
對不起,這可能有幫助。當我單擊命令按鈕輸入變量時,出現運行時錯誤「1004」: Worksheet類的複製方法失敗。我得到一個結束按鈕和一個調試按鈕。當我單擊調試按鈕時,它突出顯示此行: ThisWorkbook.Sheets(OriginalSheetName).Copy after:= ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count) – user2340287 2013-05-01 19:16:05
調用此代碼的代碼行是什麼?點擊調試後,如果將鼠標懸停在'OriginalSheetName'上,小彈出窗口會說什麼?這是運行代碼的3工作簿中的工作表之一的名稱? – user2140261 2013-05-01 21:01:02