簡體   English   中英

當vba腳本保存到新工作簿時,Excel驗證中斷

[英]Excel validation broken when vba script saves to new workbook

回答! 感謝大衛指出我的問題。 我離開了這個問題,如果它可以幫助其他人。 見下面的答案。

我有一個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

結束子

回答! 我的問題是,當我復制到一個新的工作簿時,我的單元格用於驗證的范圍沒有隨附(單元格被復制到新的工作表,但驗證的命名范圍保留在舊工作表上,新工作表已保存對於新的工作簿,驗證保留在舊工作簿上)。 為了解決這個問題,在創建新工作表之后,我在新工作表上重命名了驗證范圍。 這是代碼:

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

感謝大衛指出我的問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM