2013-05-01 142 views
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 

任何幫助,非常感謝。謝謝。

+3

什麼不行?錯誤的值或者你是否得到錯誤?還有別的嗎? – user2140261 2013-05-01 19:08:26

+0

對不起,這可能有幫助。當我單擊命令按鈕輸入變量時,出現運行時錯誤「1004」: Worksheet類的複製方法失敗。我得到一個結束按鈕和一個調試按鈕。當我單擊調試按鈕時,它突出顯示此行: ThisWorkbook.Sheets(OriginalSheetName).Copy after:= ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count) – user2340287 2013-05-01 19:16:05

+2

調用此代碼的代碼行是什麼?點擊調試後,如果將鼠標懸停在'OriginalSheetName'上,小彈出窗口會說什麼?這是運行代碼的3工作簿中的工作表之一的名稱? – user2140261 2013-05-01 21:01:02

回答

1

升級時出現錯誤會讓我認爲你的代碼可能使用了早期綁定(雖然我看到的唯一引用是常量,如msoOLEControlObjectxlByColumns,我不知道爲什麼這些會導致Copy失敗)。您可能需要檢查VBA中的引用,或查看轉換爲後期綁定。不幸的是,這裏有些複雜。

另一種可能性是Copy在VBA的新版本中發生了變化(仍然在2007年我自己)。您應該能夠在光標位於單詞Copy時按下F1以查明它應該如何工作。

無論哪種方式,你可能學習一點編程! ;)

相關問題