2

我的公司使用 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

任何帮助是极大的赞赏。谢谢你。

4

1 回答 1

1

升级时出现错误让我认为您的代码可能正在使用早期绑定(尽管我看到的唯一引用是和之类的常量msoOLEControlObjectxlByColumns我不知道为什么这些会导致 Copy 失败)。您可能需要检查 VBA 中的引用,或考虑转换为后期绑定。不幸的是,这些有点复杂。

另一种可能性是新版本的 VBA 中的 Copy 已更改(我自己仍在 2007 年)。您应该能够在光标位于“复制”一词上时按 F1 以了解它应该如何工作。

无论哪种方式,您都可能正在学习一点编程!;)

于 2013-07-30T14:12:29.070 回答