[英]Excel VBA 2010: Data validation breaks when workbook saved by VBA, but not when saved manually
上下文和工作簿我有一個用於創建調查表的工作簿。 用戶從多個選項卡的問題列表中進行選擇,然后運行一個宏,將選定的問題整理到新的工作簿中; 用戶會將新的“已發布”工作簿發送給他們的客戶。 也可以選擇帶有問題的回復類型; 例如,“是/否”,“ 1-5分”等。整理問題和選項卡后,響應類型將作為數據驗證添加到新工作簿上。 具有下拉列表的選項卡在新工作簿中存在並且被隱藏。
行為,我正在查看當工作簿在創建后仍處於打開狀態時,一切正常。 但是,當我關閉並重新打開時,出現標准錯誤“找到了無法讀取的內容...您要修復...” excel進行的修復從所有選項卡中刪除了所有數據驗證! 僅當通過VBA創建並保存文件時才會發生這種情況; 手動創建和保存文件我沒有收到此錯誤。 例如,我還嘗試在我自己創建的新工作簿上使用相同的VBA代碼添加數據驗證,並且不會發生此問題。
代碼注釋; 工作流程,以及我嘗試過的工作:
創建和保存新工作簿的代碼
outFileName = Application.GetSaveAsFilename(InitialFileName:=standardName, FileFilter:="Excel Files (*.xlsm), *.xlsm", Title:="Save As")
If outFileName = "FALSE" Then
MsgBox ("Export NOT completed")
GoTo endSafely
Else
outFileName = outFileName
End If
Set outBook = Workbooks.Add
'Activate and save the workbook
outBook.Activate
outBook.SaveAs Filename:=outFileName, FileFormat:=52
應用數據驗證的代碼
Sub addResponseFormatting(targetBook, targetSheet, targetRow, targetColumn, typeResponse)
Set targetBook = Workbooks(targetBook)
Set thisBook = Workbooks(ThisWorkbook.Name)
'---------------------------------------------------------------------------------------------------
' PROCESS
'---------------------------------------------------------------------------------------------------
targetBook.Activate
targetBook.Sheets(targetSheet).Activate
Dim targetCell As Range
With targetBook.Sheets(targetSheet).Cells(targetRow, targetColumn).Validation
Select Case typeResponse
Case "Yes/No"
.Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
xlBetween, Formula1:="=DropDowns!$D$4:$D$5"
.IgnoreBlank = True
.InCellDropdown = True
.InputTitle = ""
.ErrorTitle = ""
.InputMessage = ""
.ErrorMessage = ""
.ShowInput = True
.ShowError = True
Case "1 to 5"
.Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
xlBetween, Formula1:="=DropDowns!$C$4:$C$8"
.IgnoreBlank = True
.InCellDropdown = True
.InputTitle = ""
.ErrorTitle = ""
.InputMessage = ""
.ErrorMessage = ""
.ShowInput = True
.ShowError = True
Case Else
'Do nothing; leave open as free text
'Removes all validation; note this may also remove tooltip messages if we've applied these
.Delete
End Select
End With
End Sub
工作流程
我嘗試過的事情
文件上的其他所有內容均符合預期
其他注意事項
還有其他人解決此問題嗎?
這是我在這里的第一篇文章,所以我希望我做的透徹。 謝謝。
我找到了問題的根源:一些其他數據驗證與我的選項卡一起復制,並且它們的源(列表類型驗證)仍鏈接到原始工作簿-這導致錯誤,並且當Excel嘗試修復文件時,它將從標簽中刪除所有數據驗證(不僅僅是帶有錯誤的數據驗證)。
為了確定哪些單元格獲得和丟失了數據驗證,我使用了以下簡單的代碼來突出顯示帶有驗證的單元格:
子例程 (后面是數據驗證檢查功能)
Sub runascan()
Set targetBook = Workbooks("test25")
targetBook.Activate
For Each sheetsIn In targetBook.Sheets
sheetsIn.Activate
For Each cellin In Range("A1:Z100")
If checkVal(cellin) = 1 Then
cellin.Interior.Color = RGB(0, 255, 0)
Else
End If
Next cellin
Next sheetsIn
End Sub
檢查單元格中的數據驗證功能
Function checkVal(tRange)
Workbooks(ThisWorkbook.Name).Activate
x = 0
On Error Resume Next
x = tRange.SpecialCells(xlCellTypeSameValidation).Count
On Error GoTo 0
If x = 0 Then
checkVal = 0
Else
checkVal = 1
End If
End Function
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.