列出所有工作薄的 VBA 由 Mr Colo寫的 VBA 需要在VBA內(nèi)選取 Microfost Visual Basic Applications Extensbility 請?jiān)?Tools - 宏 - 安全性 - 選取 信任存取 Visual Basic 項(xiàng)目 ‘ Module
‘ List All VBA module Dim x As Long Dim aList() Sub GetVbProj() Dim oVBC As VBIDE.VBComponent Dim Wb As Workbook x = 2 For Each Wb In Workbooks For Each oVBC In Workbooks(Wb.Name).VBProject.VBComponents If Workbooks(Wb.Name).VBProject.Protection = vbext_pp_none Then Call GetCodeRoutines(Wb.Name, oVBC.Name) End If Next Next With Sheets.Add .[A1].Resize(, 3).Value = Array("Workbook", "Module", "Procedure") .[A2].Resize(UBound(aList, 2), UBound(aList, 1)).Value = _ Application.Transpose(aList) .Columns("A:C").Columns.AutoFit End With End Sub Private Sub GetCodeRoutines(wbk As String, VBComp As String) Dim VBCodeMod As CodeModule Dim StartLine As Long On Error Resume Next Set VBCodeMod = Workbooks(wbk).VBProject.VBComponents(VBComp).CodeModule With VBCodeMod StartLine = .CountOfDeclarationLines + 1 Do Until StartLine >= .CountOfLines ReDim Preserve aList(1 To 3, 1 To x - 1) aList(1, x - 1) = wbk aList(2, x - 1) = VBComp aList(3, x - 1) = .ProcOfLine(StartLine, vbext_pk_Proc) x = x + 1 StartLine = StartLine + .ProcCountLines(.ProcOfLine(StartLine, _ vbext_pk_Proc), vbext_pk_Proc) If Err Then Exit Sub Loop End With Set VBCodeMod = Nothing End Sub 不可以選擇或編輯單元格 Private Sub Worksheet_SelectionChange(ByVal Target As Range) ‘Stop select firing a second time
Application.EnableEvents = False If KeepOut.Rows.Count = 65536 And KeepOut.Columns.Count = 256 Then ‘Entire sheet is the KeepOut range. Eek! ‘Bounce user to a dummy sheet On Error Resume Next Set ws = ThisWorkbook.Sheets("KickMeTo") On Error GoTo 0 If ws Is Nothing Then Set ws = ThisWorkbook.Sheets.Add ws.Name = "KickMeTo" End If MsgBox "Houston we have a problem" & vbNewLine & _ "You cannot select any cell in " & vbNewLine & "‘" & KeepOut.Parent.Name & "‘" & vbNewLine & _ "So you have been directed to a different sheet" ws.Activate ElseIf KeepOut.Rows.Count = 65536 Then ‘If all rows are contained in the "KeepOut" range then: ‘Now we need to find a cell that is in a column to the right or left of this range If KeepOut.Cells(1).Column > 1 Then ‘If there is a valid column to the left of the range then select the cell in this column Cells(KeepOut.Cells(1).Row, KeepOut.Cells(1).Column - 1).Select Else ‘Else select the cell in first column to the right of the range Cells(KeepOut.Cells(1).Row, KeepOut.Cells(1).Column + 1).Select End If MsgBox "You cannot select " & KeepOut.Address(False, False) & vbNewLine & _ "You have been directed to the first free column in the protected range", vbCritical ElseIf KeepOut.Rows.Count + KeepOut.Cells(1).Row - 1 = 65536 Then ‘Select first cell in Column A before "KeepOut" Range Cells(KeepOut.Cells(1).Row - 1, 1).Select MsgBox "You cannot select " & KeepOut.Address(False, False) & vbNewLine & _ "You have been directed to the first free cell in Column A above the protected range", vbCritical Else ‘Select first cell in Column A beyond "KeepOut" Range MsgBox "You cannot select " & KeepOut.Address(False, False) & vbNewLine & _ "You have been directed to the first free cell in Column A below the protected range", vbCritical Cells(KeepOut.Rows.Count + KeepOut.Cells(1).Row, 1).Select End If Application.EnableEvents = True End Sub MicroSoft 沒有文件顯示 編碼 的大小限制 以下編碼檢示 Module 的大小 Sub get_Mod_Size() ‘ ************************************************************************************** ‘ Set these to run ‘ ***** No action needed after this point ***** ‘ Export the component (module, form, etc) - this is only temporary ‘ Get the size of the file created ‘ Return the file size ‘ Delete the exported file End Sub
測試 WorkSheet 是否存在
Sub IsSheetExist()
Dim wSheet As Worksheet On Error Resume Next Set wSheet = Sheets("Sheet6") If wSheet Is Nothing Then MsgBox "Worksheet does not exist" Set wSheet = Nothing On Error GoTo 0 Else MsgBox "Sheet does exist" Set wSheet = Nothing On Error GoTo 0 End If End Sub
讓工作表始終置頂
----------------- Module Private Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal x As Long, y, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long Public Sub MakeNormal(hwnd As Long) Sub test()
Call MakeTopMost(Application.hwnd) Call MakeNormal(Application.hwnd) End Sub 有效性下拉框的高度 顯示更多更直觀
Option Explicit Dim oDpd As Object Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range) If Not prvTarget Is Nothing Then On Error Resume Next If Target.Count > 1 Then Set vld = Target.Validation Set prvTarget = Target lDpdLine = Range(Mid(sFml1, 2)).Rows.Count With Target
Set oDpd = ActiveSheet.DropDowns.Add( _ .Left - dFixedPos, _ .Top - dFixedPos, _ .Width + dFixWidth + dFixedPos * 2, _ .Height + dFixedPos * 2) End With With oDpd .ListFillRange = sFml1 .DropDownLines = lDpdLine .Display3DShading = True End With Terminate: End Sub
請問如何不改變activecell之下將某一儲存格顯示於左上角?
1. ActiveWindow.SmallScroll Up:=65536 ActiveWindow.SmallScroll ToLeft:=256 用上面的方法先回到 A1 再用下面的方法到定點(diǎn) ActiveWindow.SmallScroll Down:=儲存格列號 - 1 ActiveWindow.SmallScroll ToRight:=儲存格欄號 - 1 2. ActiveCell.Select ActiveWindow.ScrollRow = ActiveCell.Row ActiveWindow.ScrollColumn = ActiveCell.Column 3. Application.Goto ActiveCell, True
Save Sheet as WorkBook
Sub SaveShtsAsBook()
Dim Sheet As Worksheet, SheetName$, MyFilePath$, N& MyFilePath$ = ActiveWorkbook.Path & "\" & _ Left(ThisWorkbook.Name, Len(ThisWorkbook.Name) - 4) With Application .ScreenUpdating = False .DisplayAlerts = False ‘ End With On Error Resume Next ‘<< a folder exists MkDir MyFilePath ‘<< create a folder For N = 1 To Sheets.Count Sheets(N).Activate SheetName = ActiveSheet.Name Cells.Copy Workbooks.Add (xlWBATWorksheet) With ActiveWorkbook With .ActiveSheet .Paste .Name = SheetName [A1].Select End With ‘save book in this folder .SaveAs Filename:=MyFilePath _ & "\" & SheetName & ".xls" .Close SaveChanges:=True End With .CutCopyMode = False Next End With Sheet1.Activate End Sub +++++++++++++++++++++++++++++++++++++++++++++++++++++
Sub BreakExternalLinks() Dim WS As Worksheet For Each WS In ActiveWorkbook.Worksheets On Error GoTo 0
If Not Rng1 Is Nothing Then For Each Cell In Rng1 If Left(Cell.Formula, 2) = "=‘" Then Cell.Value = Cell.Value End If Next End If Set Rng1 = Nothing End With Next End Sub
使用期限設(shè)定
‘ chijanzen
(原始) 2003/10/1 ‘ 今天介紹如何讓Excel檔案有使用期限,範(fàn)例中使用Windows Script"在註冊表上的讀.寫.刪除的用法 ‘ 本範(fàn)例使用期限設(shè)定 0 天,所以檔案只能開啟一次就自動銷毀 ‘ Script 能使用的根鍵值有五個(gè)根鍵名稱 HKEY_CURRENT_USER ‘縮寫 HKCU HKEY_LOCAL_MACHINE ‘縮寫 HKLM HKEY_CLASSES_ROOT ‘縮寫 HKCR HKEY_USERS ‘縮寫 HKEY_USERS HKEY_CURRENT_CONFIG ‘縮寫 HKEY_CURRENT_CONFIG Sub CheckFileDate() Dim Counter As Long, LastOpen As String, Msg As String If RegRead = "" Then Term = 0 ‘範(fàn)例用 0 天 TermDate = DateSerial(Year(Now), Month(Now), Day(Now)) + Term MsgBox "本檔案只能使用到" & TermDate & "日" & Chr(13) & "超過期限將自動銷毀" RegWrite (Term) Else If CDate(RegRead) <= Now Then RegDelete KillMe End If End If End Sub Sub KillMe() Application.DisplayAlerts = False ActiveWorkbook.ChangeFileAccess xlReadOnly Kill ActiveWorkbook.FullName ThisWorkbook.Close False End Sub Sub RegWrite(Term) ‘RegWrite:建立新鍵,、將另一個(gè)值名稱加入現(xiàn)有鍵 (並將值指派給它),或變更現(xiàn)有值名稱的值,。 Dim WshShell, bKey fname = ThisWorkbook.Name TermDate = DateSerial(Year(Now), Month(Now), Day(Now)) + Term Regkey = "HKCU\chijanzen\Budget\Date\" & fname Set WshShell = CreateObject("WScript.Shell") WshShell.RegWrite Regkey, TermDate, "REG_SZ" End Sub Function RegRead() ‘RegRead: 從註冊傳回鍵的值或值名稱 On Error Resume Next Dim WshShell, bKey fname = ThisWorkbook.Name Regkey = "HKCU\chijanzen\Budget\Date\" & fname Set WshShell = CreateObject("WScript.Shell") RegRead = WshShell.RegRead(Regkey) End Function Sub RegDelete() ‘RegDelete :從註冊刪除某鍵或它的一個(gè)值(請小心使用) Dim WshShell, bKey Regkey = "HKCU\chijanzen\Budget\Date\" Set WshShell = CreateObject("WScript.Shell") WshShell.RegDelete Regkey ‘刪除檔名 End Sub
防止 Excel 關(guān)閉
原碼出自 Tek-Tips Forum ‘ Module Option Explicit ‘Set Types Public Type LUID_AND_ATTRIBUTES Public Type TOKEN_PRIVILEGES ‘ Declare API functions. ‘ Set Set ShutDown Privilege Constants Public Sub SetShutDownPrivilege() Phndl = GetCurrentProcess() End Sub
Option Explicit Private Sub Workbook_BeforeClose(Cancel As Boolean) ‘ Define message. End Sub Private Sub Workbook_Open()
On Error Resume Next ‘Activate the 1st worksheet using the workbooks worksheet index Worksheets(1).Activate ‘Or If you want to use the actual worksheet name ‘Worksheets("Sheet1").Activate End Sub 指定電腦上運(yùn)行 ‘用 F8 逐句執(zhí)行籃色編碼,取值后更改紅色部份 ‘ ThisWorkBook Private Declare Function w32_GetComputerName Lib "kernel32" _ Private Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" _ Private Sub Workbook_Open() Function GetComputerName() Function UserName() As String
Dim Buffer As String * 100 Dim BuffLen As Long BuffLen = 100 GetUserName Buffer, BuffLen UserName = Left(Buffer, BuffLen - 1) End Function 可以監(jiān)控刪除行及列嗎
‘ Module Option Explicit
‘// Worksheet RowColumn Deleted Event ‘// This is NOT a real event but just hack the command button. ‘// You can know when the rows or the columns was deleted by user‘s opelation. Sub EventHack() ‘ 執(zhí)行監(jiān)控程序 AssignMacro "JudgeRng" End Sub Sub EventReset() ‘ 取消監(jiān)控程序 AssignMacro "" End Sub Private Sub AssignMacro(ByVal strProc As String) Dim lngId As Long Dim CtrlCbc As CommandBarControl Dim CtrlCbcRet As CommandBarControls Dim arrIdNum As Variant ‘// 293=Delete menu of the right click on row ‘// 294=Delete menu of the right click on column ‘// 293=Delete menu of the Edit of main menu arrIdNum = Array(293, 294, 478) For lngId = LBound(arrIdNum) To UBound(arrIdNum) Set CtrlCbcRet = CommandBars.FindControls(ID:=arrIdNum(lngId)) For Each CtrlCbc In CtrlCbcRet CtrlCbc.OnAction = strProc Next Set CtrlCbcRet = Nothing Next End Sub Private Sub JudgeRng() If Not TypeOf Selection Is Range Then Exit Sub With Selection If .Address = .EntireRow.Address Then Call DelExecute("Row:" & .Row, xlUp) ElseIf .Address = .EntireColumn.Address Then Call DelExecute("Column:" & .Column, xlToLeft) Else Application.Dialogs(xlDialogEditDelete).Show End If End With End Sub Private Sub DelExecute(ByVal str, ByVal lngDerec As Long) MsgBox "deleted:" & str Selection.Delete lngDerec End Sub
測試 WorkBook 是否已開啟
Sub IsWorkBookOpen() Dim wBook As Workbook On Error Resume Next Set wBook = Workbooks("Book180.xls") If wBook Is Nothing Then MsgBox "Workbook is not open" Set wBook = Nothing On Error GoTo 0 Else MsgBox "Yes it is open" Set wBook = Nothing On Error GoTo 0 End If End Sub
|
|