VBA manual

References

Determine whether the workbook exists

method one

    Dim wb As Workbook
    For Each wb In Workbooks
        If wb.Name = "1.xlsx" Then
            MsgBox "1文件已经打开"
            Exit Sub
        End If
    Next
    MsgBox "1文件已经没有打开"

Method Two

Set wb = Workbooks(WorkLocation & "\365 report.xlsm")
If Err.Number = 9 Then
Workbooks.OpenText fileName:=WorkLocation & "\365 report.xlsm", DataType:=xlDelimited, Tab:=True
End If

Close without prompt

Application.DisplayAlerts = False
操作完后打开提示
Application.DisplayAlerts = True

Maximize window

Application.WindowState = xlMaximized

Open

Workbooks.Open ("C:"MyFolder"MyBook.xls")
or
Workbooks.OpenText fileName:=WorkLocation & "\365 report.xlsm", DataType:=xlDelimited, Tab:=True

Add new worksheet

Add a new worksheet to the active workbook and set the worksheet's name

Set NewSheet = Worksheets.Add
NewSheet.Name = "current Budget"

keep

ActiveWorkbook.Save
'本示例保存当前活动工作簿的副本。
ActiveWorkbook.SaveCopyAs "C:"TEMP"XXXX.XLS"

activation

Windows(Filen).Activate

Sheets(4).Activate

Close workbook

Close after saving
ThisWorkbook.Saved = True
ThisWorkbook.Close

but does not save changes

 ActiveWorkbook.Close savechanges:=False

Close all workbooks except the one in which this example is running, and save their changes

For Each w In Workbooks
    If w.Name ThisWorkbook.Name Then
        w.Close SaveChanges:=True
    End If
Next w

Square Cell

Worksheets("Sheet1").Range("A1").Value

Sheets

For Each ws In Worksheets
    MsgBox ws.Name
Next ws

Add a new worksheet to the active workbook, and set the name of the worksheet?
Set NewSheet = Worksheets.Add
NewSheet.Name = "current Budget"
Move the new worksheet to the end of the workbook
Private Sub App_WorkbookNewSheet(ByVal Wb As Workbook, ByVal Sh As Object)
Sh.Move After:=Wb.Sheets(Wb.Sheets.Count)
End Sub

scroll mouse

第十行移到窗口的最上面?
Worksheets("Sheet1").Activate
ActiveWindow.ScrollRow = 10

Print

Worksheets("Sheet1").PrintPreview

MsgBox

Dim ask As Integer
ask = MsgBox("Your are working in Production? 
If  you choose no,will handle test process", vbYesNo, "Choose your doing?")
If ask = VBA.vbYes Then
Production = True
Else
Production = False
End If

Syntax:
MsgBox(prompt[, buttons] [, title] [, helpfile, context])
prompt: statement in the dialog box
buttons: select the button form. For example: OK; yes, no; whether to cancel, etc. You can select according to the drop-down list.
title: The title of the pop-up box.

Return value:
For function form, an integer value will be returned according to the button clicked by the user, corresponding to the following:
Constant value description
vbOK 1 Confirm
vbCancel 2 Cancel
vbAbort 3 Terminate
vbRetry 4 Retry vbIgnore
5 Ignore
vbYes 6 Yes
vbNo 7 No

Print file

Workbooks.Open Filename:="E:\VTECHCMS-C.xlsx"
'Application.ActivePrinter = "KD860 on Ne04:"
ActiveWindow.SelectedSheets.PrintOut Copies:=1
    Application.DisplayAlerts = False
    ActiveWorkbook.Close
    Application.DisplayAlerts = True

or

Workbooks.Open fileName:=WorkLocation + "\courier\ZY1-LITEON.xlsx"
'Application.ActivePrinter = "KD860 on Ne04:"
'ActiveWindow.SelectedSheets.PrintOut Copies:=1
'ActiveWindow.Close SaveChanges:=False

wait for some time

Application.Wait (Now + TimeValue("00:00:06"))

If Else

If  Then

ElseIf

End If

Print PDF

#If VBA7 Then
Declare PtrSafe Function apiShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As LongPtr, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As LongPtr) As LongPtr
#Else
Declare Function apiShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As LongPtr, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As LongPtr) As LongPtr
#End If

Public Sub PrintFile(ByVal strPathAndFilename As String)
    Call apiShellExecute(Application.hwnd, "print", strPathAndFilename, vbNullString, vbNullString, 0)
End Sub
 
Sub test()
    PrintFile ("E:\test\mybook.pdf")
End Sub

Get path

Application.Path 
"C:\Program Files\Microsoft Office\Root\Office16"
返回当前工作薄的路径 
ThisWorkbook.Path 
"E:\statement"
返回当前默认文件路径: 
Application.DefaultFilePath 
Application.ActiveWorkbook.Path   只返回路径 
Application.ActiveWorkbook.FullName   返回路径及工作簿文件名 
Application.ActiveWorkbook.Name   返回工作簿文件名 

Copy and create a new workbook

    Sheets("Sheet1").Select
    Sheets("Sheet1").Copy
    ChDir "E:\test"
    ActiveWorkbook.SaveAs Filename:="E:\test\mybook.xlsx", FileFormat:= _
        xlOpenXMLWorkbook, CreateBackup:=False

Debug.Print()

How to Use Excel VBA Debug. Print?
Insert image description here

Set up Macros security

Insert image description here
Or File /Options
Insert image description here
if it is still Block, right-click the file properties
Insert image description here

Fix garbled characters

Tools / Options
Insert image description here
Control Pannel / Region
Insert image description here

Open VBA

Alt +F11

Quickly open VBA

File/Options/Customize Ribbon

Insert image description here

Upgrade and Compatibility

Insert image description here

 #If VBA7 Then
 Declare PtrSafe Function apiShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (
 ByVal hwnd As LongPtr, 
 ByVal lpOperation As String, 
 ByVal lpFile As String, 
 ByVal lpParameters As String, 
 ByVal lpDirectory As String, 
 ByVal nShowCmd As LongPtr) 
 As LongPtr
 #Else
 Declare Function apiShellExecute Lib "shell32.dll" Alias "ShellExecuteA" ( _
    ByVal hwnd As Long, _
    ByVal lpOperation As String, _
    ByVal lpFile As String, _
    ByVal lpParameters As String, _
    ByVal lpDirectory As String, _
    ByVal nShowCmd As Long) _
    As Long
 #End If

Guess you like

Origin blog.csdn.net/MyFreeIT/article/details/132301951