我的公司使用excel来生产出我们所有商店的网络配置。除了在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
非常感谢任何帮助。谢谢。
答案 0 :(得分:1)
升级时出错让我觉得你的代码可能正在使用早期绑定(尽管我看到的唯一引用是常量,如msoOLEControlObject
和xlByColumns
,我不知道为什么这些会导致复制失败)。您可能需要检查VBA中的引用,或者查看转换为后期绑定。不幸的是,这些内容有点复杂。
另一种可能性是新版VBA中的复制已经改变(仍然是2007年我自己)。当光标在单词Copy上时,您应该能够按F1,以了解它应该如何工作。
无论哪种方式,你可能都在学习一点编程! ;)