1
votes

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

1
Re: Edit 3 -- A potential fix is to create a new validation for the ranges AFTER I saved my sheet into a new workbook. (see my edit, after, not before) So, assign the ranges to the DestWB object. However, your current code is looking only at ThisWorkbook.Sheets(2) but it looks like you're copying each visible sheet to a new workbook (DestWB). Validation in DestWB can't refer back to another workbook (ThisWorkbook.Sheets(2)), it needs to refer to a sheet within DestWB.David Zemens
Good point David. Each sheet in the workbook is being created from the original sheet (Sheet 5) which filters for a row and creates 95 new sheets, one for each filter I have in a column. I was hoping that since the sheets were just being copied, that the validation would go with them (which seems true, although now I need to go and double check it). Perhaps I need to keep the validation somewhere else on the sheet and see if that will allow things to work. The issue is that the filter jumbles everything up, so I'm currently recopying the filtered cells after sheet creation. Thanks!uttuck
That gives me an idea... I will write some suggestions as an answer :)David Zemens
I found an interesting ideaDavid Zemens
eh, my idea busted. cheers, though.David Zemens

1 Answers

0
votes

Answered! My issue was that when I copied to a new workbook, the range that my cells were using for validation didn't come with it (cells were copied to new sheets but the named ranges for validation stayed on the old sheets, new sheets were saved to a new workbook, validation stayed on the old workbook). To fix it, after the new sheet was created I renamed the ranges for validation on the new sheet. Here is the code:

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

Thanks to David for pointing out my problem.