Answered! Thanks to David for pointing out my issue. I left the question if it helps someone else. See answer below.
I have a VBA script that saves every sheet to a new workbook, however when I open the new workbook the validation from the previous sheet doesn't work. I also discovered that when both old and new workbooks are open, the validations work on both sheets. When I close the old workbook, the validation on the new workbook doesn't work.
I have read that if the version is older than 2013 then the validation must be on the same page or in a named range. I've tried both of those, but it didn't change anything.
How can I get the validation to work on the new workbook?
Edit: the reason the validation works when the old book is open is because the validation connects the named range to the named range in the old book. If I change the cells on the new book, the validation doesn't change. Therefore (see edit 2)
Edit 2: if that is the case, how can I rename a range in the new sheet and validate a cell for that range? For example, I need some VBA to designate a name for range AF2:AF8 and then validate another range O2:O25000 so that it can only use the first range as possibilities.
Edit 3 A potential fix is to create a new validation for the ranges before I saved my sheet into a new workbook. I have tried to do that with the code below, but it still isn't working:
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
Here is the original code:
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
DestWB
object. However, your current code is looking only atThisWorkbook.Sheets(2)
but it looks like you're copying each visible sheet to a new workbook (DestWB
). Validation inDestWB
can't refer back to another workbook (ThisWorkbook.Sheets(2)
), it needs to refer to a sheet withinDestWB
. – David Zemens