回答!感谢大卫指出我的问题。我离开了这个问题,如果它可以帮助其他人。请参阅下面的答案。
我有一个VBA脚本可以将每个工作表保存到新工作簿,但是当我打开新工作簿时,上一个工作表中的验证不起作用。我还发现,当新旧工作簿都打开时,验证工作在两个工作表上都有效。当我关闭旧工作簿时,新工作簿上的验证不起作用。
我已经读过,如果版本早于2013年,则验证必须位于同一页面或命名范围内。我已经尝试了这两种方法,但它没有改变任何东西。
如何让验证能够在新工作簿上运行?
编辑:旧书打开时验证工作的原因是验证将命名范围连接到旧书中的命名范围。如果我更改新书上的单元格,则验证不会更改。因此(见编辑2)
编辑2 :如果是这种情况,如何重新命名新工作表中的范围并验证该范围的单元格?例如,我需要一些VBA来指定范围AF2:AF8的名称,然后验证另一个范围O2:O25000,以便它只能使用第一个范围作为可能性。
编辑3 可能的解决方法是在将工作表保存到新工作簿之前为范围创建新的验证。我尝试使用下面的代码执行此操作,但它仍然无效:
vRange = ThisWorkbook.Worksheets(2).Range("A1:G200")
'Assigning my ranges
IdCo = ThisWorksheet.Range("AF2:AF8")
MeE = ThisWorksheet.Range("AG2:AG7")
GrReEf = ThisWorksheet.Range("AH2:AH5")
CyRePl = ThisWorksheet.Range("AI2:AI4")
IdCo1 = ThisWorksheet.Range("O2:O25000")
For i = 5 To ThisWorkbook.Worksheets.Count
ThisWorkbook.Worksheets(i).Range("AF1:AL200").Value = vRange
'Actually doing the validations
With IdCo.Validation
.Delete 'delete previous validation
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, _
Formula1:="='" & ws.Name & "'!" & IdCo1.Address
End With
Next i
以下是原始代码:
Dim FileExtStr As String
Dim FileFormatNum As Long
Dim Sourcewb As Workbook
Dim Destwb As Workbook
Dim sh As Worksheet
Dim DateString As String
Dim FolderName As String
With Application
.ScreenUpdating = False
.EnableEvents = False
.Calculation = xlCalculationManual
End With
'Copy every sheet from the workbook with this macro
Set Sourcewb = ThisWorkbook
'Create new folder to save the new files in
DateString = Format(Now, "yyyy-mm-dd hh-mm-ss")
FolderName = Sourcewb.Path & "\" & Sourcewb.Name & " " & DateString
MkDir FolderName
'Copy every visible sheet to a new workbook
For Each sh In Sourcewb.Worksheets
'If the sheet is visible then copy it to a new workbook
If sh.Visible = -1 Then
sh.Copy
'Set Destwb to the new workbook
Set Destwb = ActiveWorkbook
'Determine the Excel version and file extension/format
With Destwb
If Val(Application.Version) < 12 Then
'You use Excel 97-2003
'This is the line I put an m in
FileExtStr = ".xlsm": FileFormatNum = -4143
Else
'You use Excel 2007-2013
If Sourcewb.Name = .Name Then
MsgBox "Your answer is NO in the security dialog"
GoTo GoToNextSheet
Else
Select Case Sourcewb.FileFormat
Case 51: FileExtStr = ".xlsx": FileFormatNum = 51
Case 52:
If .HasVBProject Then
FileExtStr = ".xlsm": FileFormatNum = 52
Else
FileExtStr = ".xlsx": FileFormatNum = 51
End If
Case 56: FileExtStr = ".xls": FileFormatNum = 56
Case Else: FileExtStr = ".xlsb": FileFormatNum = 50
End Select
End If
End If
End With
'Change all cells in the worksheet to values if you want
' If Destwb.Sheets(1).ProtectContents = False Then
' With Destwb.Sheets(1).UsedRange
' .Cells.Copy
' .Cells.PasteSpecial xlPasteValues
' .Cells(1).Select
' End With
' Application.CutCopyMode = False
' End If
'Save the new workbook and close it
With Destwb
.SaveAs FolderName _
& "\" & Destwb.Sheets(1).Name & FileExtStr, _
FileFormat:=FileFormatNum
.Close False
End With
End If
GoToNextSheet:
Next sh
MsgBox "You can find the files in " & FolderName
With Application
.ScreenUpdating = True
.EnableEvents = True
.Calculation = xlCalculationAutomatic
End With
End Sub
答案 0 :(得分:0)
回答!我的问题是,当我复制到一个新工作簿时,我的单元格用于验证的范围并没有随之而来(单元格被复制到新工作表中,但验证的命名范围仍保留在旧工作表上,新工作表保存到新工作簿,验证保留在旧工作簿上)。为了解决这个问题,在创建新工作表之后,我在新工作表上重命名了验证范围。这是代码:
With Destwb
Worksheets(1).Range("AF2:AF8").Name = "IdCo"
Worksheets(1).Range("AG2:AG7").Name = "MeE"
Worksheets(1).Range("AH2:AH5").Name = "GrReEf"
Worksheets(1).Range("AI2:AI4").Name = "CyRePl"
.SaveAs FolderName _
& "\" & Destwb.Sheets(1).Name & FileExtStr, _
FileFormat:=FileFormatNum
End With
感谢大卫指出我的问题。