Common features of the workbook workbook:
1. Create a new workbook
- Dim WB as Workbook
- Application.sheetsinnewworkbook = 1 ' Sets the number of worksheets in the initial workbook
- Set WB = Application.WorkBooks.Add
- Wb. Worksheets (1). Name = "Table 1" to set the name of the first sheet
- Application.sheetsinnewworkbook = 3
2. Open an Excel file with the Excel dialog box
- Dim fileInfo as String
- FileInfo = Application.getopenfilename ("Excel Workbook (*.xlsx), *.xlsx")
- MsgBox FileInfo
If the user selects an Excel file, FileInfo is the full path to the file. If not selected, the FileInfo is False
3. Backup (Save As) Excel file
- Dim WB as Workbook
- Set WB = application.workbooks (1) ' original file name is Vba.xlsx
- Wb. Activate
- Wb. SaveCopyAs Wb.path & "\" & "Bak_" & wb.name ' backup file named Bak_vba.xlsx
4. Save the Excel file with the Excel dialog box
- dim fileinfo as string
- fileinfo = application.getsaveasfilename ( "Excelfile", "excel Workbook (*.xlsm), *.xlsm")
- if not fileinfo = "False" then
- activeworkbook.saveas filename:=fileinfo
- End < span class= "keyword" >if
- MsgBox
- else
5. Set the size of the workbook (not the size of the Excel window) although using the Application Property ActiveWindow, the property corresponds to a workbook window
- Dim win as Window
- Dim winstate as Long
- Dim winwidth as Double
- Dim winheight as Double
- Set win = Application.ActiveWindow
- Win. Activate
- With win
- . WindowState = xlminimized ' minimized workbook, but Excel program window is not minimized
- End with
- With win
- Winstate =. WindowState
- Winwidth =. Width
- Winheight =. Height
- End with
- Debug.Print "Winstate:" & Winstate
- Debug.Print "Winwidth:" & Winwidth
- Debug.Print "Winheight:" & Winheight
6. Freeze Panes. Same as above still using Application.ActiveWindow to manipulate workbooks
- dim win as window
- set win = application.activewindow
- with win
- . Split = true
- &NBSP;&NBSP;&NBSP;&NBSP;. Splitcolumn = 1
- . splitrow = 3
- &NBSP;&NBSP;&NBSP;&NBSP;. Freezepanes = true If you do not set FreezePanes, a thick split line will appear on the worksheet
- end with
7. Modify Workbook file name
- Dim fileName as String
- filename = "C:\book.xlsx" file does not exist will error
- Name fileName as "C:\book1.xlsx" Workbook should be turned off, and the path should be consistent
- Debug.Print "Renamed ok!"
Workbook Main Events:
Workbook events are easier to write than previous application events. We directly use the ThisWorkbook object's event (also a Workbook object)
For example, double-click ThisWorkbook, select the event you want to implement directly above the code area on the right, and then write code in the auto-generated event function
1.BeforeClose Events
- Private Sub workbook_beforeclose (Cancel as Boolean)
- MsgBox "Can't shut it down."
- Cancel = True
- End Sub
This event is triggered before the workbook is closed
2.BeforeSave Events
- Private Sub workbook_beforesave (ByVal SaveAsUi As Boolean, Cancel as boolean)
- If not saveasui Then
- MsgBox "can't be saved."
- Cancel = True
- End If
- End Sub
triggered when the Save button or Ctrl+s is clicked
3.WindowActive Events
- Private Sub workbook_windowactivate (ByVal Wn as Window)
- MsgBox "Activating Workbook"
- Wn.width = 100
- End Sub
VBA Learning Notes Workbook