我現在想知道是否可以刪除一行vba而不會彈出錯誤1004。我不希望用戶必須進入vba項目對象模型的設置和信任訪問。這可能嗎? 當前的代碼(如果不相信自己選擇的錯誤發生): ActiveWorkbook.Application.VBE.ActiveVBProject.VBComponents("Sheet1").CodeModule.DeleteLines 170, 1
用vba刪除一行excel vba
回答
我不希望用戶必須進入設置和信任對VBA項目對象模型。這可能嗎?
不,這是一項安全功能。使用VBA以編程方式切換此安全設置是不可能的。
每Microsoft(強調):
此設置是爲開發人員和用於故意鎖定 或允許來自任何 自動化客戶端VBA對象模型編程訪問。換句話說,它爲 代碼編寫了一個安全選項,用於自動執行Office程序,而 以編程方式處理Microsoft Visual Basic的 應用程序(VBA)環境和對象模型。這是每個用戶 和每個應用程序設置,默認情況下拒絕訪問。這種安全選項使未經授權的程序難以構建可能損害最終用戶系統的「自我複製」代碼。 對於任何 自動化客戶端,要能夠以編程方式訪問VBA對象模型 ,運行代碼的用戶必須明確授予 訪問權限。要打開訪問權限,請選中複選框。
因此,沒有用戶不得不信任訪問權限的情況下刪除一行宏是沒有辦法的? – Noisewater
如果沒有明確允許它作爲應用程序級設置,就無法以編程方式訪問VBA項目對象模型。 –
你可以嘗試像下面的代碼,看看它是否適合您的需要:
Option Explicit
Sub CheckTrustAccess_toVBAProjModule()
' display Windows Version installed on this PC
' Win7. (=6.1, 64bit)
' Win8 (=6.2, 64bit)
' Win8.1 (=6.3*)
' Win10 (=10.0*)
'
' MsgBox "Windows Version is: " & getVersion
Dim TrustAccess_VBAProjModule_Path As String
Dim TrustAccess_VBAProjModule As Integer
' ----- first check if "Trust Access to the VBA Project module is on ------
TrustAccess_VBAProjModule_Path = "HKEY_CURRENT_USER\Software\Microsoft\Office\" & Application.Version & "\Excel\Security\AccessVBOM"
' check if Trust Access to the VBA Projct Module in the Registry is 1 or 0
TrustAccess_VBAProjModule = CInt(RegKeyRead(TrustAccess_VBAProjModule_Path))
If TrustAccess_VBAProjModule = 1 Then
' run your code here ...
Else ' need to auto-click the "v" by modifying the registry settings
' loop until Trust Access to the VBA Projct Module in the Registry is 1
' it might take time to modify, so use this instead of timers
While TrustAccess_VBAProjModule = 0
Call RegKeySave(TrustAccess_VBAProjModule_Path, "1")
TrustAccess_VBAProjModule = CInt(RegKeyRead(TrustAccess_VBAProjModule_Path))
Wend
MsgBox "Initiated VBA settings, please run the Export again", vbInformation
End If
End Sub
'===========================================================
Function RegKeyRead(i_RegKey As String) As String
' reads the value for the registry key i_RegKey
' if the key cannot be found, the return value is ""
' Link : http://vba-corner.livejournal.com/3054.html
Dim myWS As Object
On Error Resume Next
'access Windows scripting
Set myWS = CreateObject("WScript.Shell")
'read key from registry
RegKeyRead = myWS.RegRead(i_RegKey)
End Function
'===========================================================
Sub RegKeySave(i_RegKey As String, i_Value As String, Optional i_Type As String = "REG_DWORD")
' sets the registry key i_RegKey to the value i_Value with type i_Type
' if i_Type is omitted, the value will be saved as string
' if i_RegKey wasn't found, a new registry key will be created
' Link : http://vba-corner.livejournal.com/3054.html
Dim myWS As Object
'access Windows scripting
Set myWS = CreateObject("WScript.Shell")
'write registry key
myWS.RegWrite i_RegKey, i_Value, i_Type
End Sub
@Noisewater你看過我的代碼嗎?你有沒有試過看它是否適合你的需求? –
我剛測試過它,它確實有效。用戶點擊加載項兩次沒有什麼大不了的。謝謝! – Noisewater
只是一個問題。雖然它確實檢查,如果它不被信任,檢查框。它僅在用戶打開信任中心/宏安全性並選擇確定時纔有效。這是朝正確方向邁出的一步,但用戶仍然需要給予正式的確定。 – Noisewater
- 1. VBA Excel刪除重複行
- 2. Excel vba Underscore。刪除_
- 3. 使用VBA刪除空行 - MS Excel
- 4. 使用VBA在Excel中刪除行
- 5. 刪除重複項excel vba
- 6. 刪除向量中的行VBA Excel
- 7. VBA Excel - 以特定間隔刪除行
- 8. 使用VBA刪除重複 - Excel 2003
- 9. 使用Excel VBA刪除圖片
- 10. 自動刪除行,並用Excel中的較低行替換Vba
- 11. VBA宏快速刪除行
- 12. 刪除當前行VBA
- 13. VBA-Excel循環刪除表格的最後一行
- 14. Excel VBA-刪除字符串和單元格內的下一行
- 15. Excel VBA刪除一行中的三重複制循環
- 16. Excel VBA - 循環未正確刪除表中的最後一行
- 17. Excel VBA - 如果一個範圍的總和爲0 - 刪除行
- 18. Excel VBA排除列
- 19. For Excel for Excel VBA只刪除每第二行
- 20. 基於Excel中的字典值在Excel中刪除行VBA
- 21. Excel的VBA比較,條件,刪除
- 22. Excel VBA過濾器和刪除數據
- 23. Excel VBA-刪除部分字符串
- 24. Excel VBA詞典刪除項目
- 25. 優化/精簡Excel VBA刪除空列
- 26. Excel VBA從公式中刪除等號
- 27. Excel VBA union刪除列中的數據
- 28. 刪除整個排在Excel VBA
- 29. 如何刪除範圍Excel VBA的列
- 30. 刪除空白(空)列Excel VBA
爲了不「選擇」是,有一個代碼,但不那麼短... –
看到我的答案和下面的代碼,我用它在我使用的一些自動工具 –