问题:在使用宏复制工作表时,我遇到数据验证无法复制到复制的工作表的问题。有没有办法使用我当前的代码?
是的,我也知道有一个类似的问题(这里:Data validation lost when I copy a worksheet to another workbook)但问题并不完全相同,目前还没有答案。任何有助于将这些数据验证与数据一起复制的帮助将非常受欢迎,并且可以节省数小时的不必要的重复性工作。
编辑:此代码位于我的工作簿的“ThisWorkbook”部分。
我的代码如下:
Dim wb As Workbook
Dim wsTemp As Worksheet
Dim sName As String
Dim bValidName As Boolean
Dim i As Long
bValidName = False
Do While bValidName = False
sName = InputBox("Please name this new worksheet:", "New Sheet Name", Sh.Name)
If Len(sName) > 0 Then
For i = 1 To 7
sName = Replace(sName, Mid(":\/?*[]", i, 1), " ")
Next i
sName = Trim(Left(WorksheetFunction.Trim(sName), 31))
If Not Evaluate("ISREF('" & sName & "'!A1)") Then bValidName = True
End If
Loop
With Application
.ScreenUpdating = False
.DisplayAlerts = False
.EnableEvents = False
End With
Set wb = ThisWorkbook
Set wsTemp = wb.Sheets("TEMPLATE")
wsTemp.Visible = xlSheetVisible
wsTemp.Copy After:=wb.Sheets(wb.Sheets.Count)
ActiveSheet.Name = sName
Sh.Delete
wsTemp.Visible = xlSheetHidden 'Or xlSheetVeryHidden
With Application
.ScreenUpdating = True
.DisplayAlerts = True
.EnableEvents = True
End With
' Call Sort_Active_book
' Call Rebuild_TOC
答案 0 :(得分:1)
您应该能够复制工作表并保留DV。这个例子:
Sub Macro2()
Sheets("Sheet1").Select
Range("D1").Select
ActiveCell.FormulaR1C1 = "alpha"
Range("D2").Select
ActiveCell.FormulaR1C1 = "beta"
Range("D3").Select
ActiveCell.FormulaR1C1 = "gamma"
Range("B1").Select
With Selection.Validation
.Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
xlBetween, Formula1:="=$D$1:$D$3"
.IgnoreBlank = True
.InCellDropdown = True
.InputTitle = ""
.ErrorTitle = ""
.InputMessage = ""
.ErrorMessage = ""
.ShowInput = True
.ShowError = True
End With
Sheets("Sheet1").Select
Sheets("Sheet1").Copy After:=Sheets(3)
End Sub
这是在Win 7 / Excel 2007系统上的新空白工作簿上运行的录制代码。
你能复制我的结果吗?
如果我的代码适用于您的系统,请首先尝试使用刻录机手动模拟您的VBA代码。然后记录您录制的代码并对其进行修改以包含不可录制的部分(如InputBox
语句)。