VBA MACRO
判断workbook是否存在
方法一
Dim wb As Workbook
For Each wb In Workbooks
If wb.Name = "1.xlsx" Then
MsgBox "1文件已经打开"
Exit Sub
End If
Next
MsgBox "1文件已经没有打开"
方法二
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
关闭不提示
Application.DisplayAlerts = False
操作完后打开提示
Application.DisplayAlerts = True
窗口最大化
Application.WindowState = xlMaximized
打开
Workbooks.Open ("C:"MyFolder"MyBook.xls")
or
Workbooks.OpenText fileName:=WorkLocation & "\365 report.xlsm", DataType:=xlDelimited, Tab:=True
添加新工作表
向活动工作簿添加新工作表 , 并设置该工作表的名称
Set NewSheet = Worksheets.Add
NewSheet.Name = "current Budget"
保存
ActiveWorkbook.Save
'本示例保存当前活动工作簿的副本。
ActiveWorkbook.SaveCopyAs "C:"TEMP"XXXX.XLS"
激活
Windows(Filen).Activate
Sheets(4).Activate
关闭工作簿
保存后关闭
ThisWorkbook.Saved = True
ThisWorkbook.Close
但不保存更改
ActiveWorkbook.Close savechanges:=False
关闭除正在运行本示例的工作簿以外的其他所有工作簿,并保存其更改内容
For Each w In Workbooks
If w.Name ThisWorkbook.Name Then
w.Close SaveChanges:=True
End If
Next w
方格Cell
Worksheets("Sheet1").Range("A1").Value
Sheets
For Each ws In Worksheets
MsgBox ws.Name
Next ws
向活动工作簿添加新工作表 , 并设置该工作表的名称?
Set NewSheet = Worksheets.Add
NewSheet.Name = “current Budget”
新建工作表移到工作簿的末尾
Private Sub App_WorkbookNewSheet(ByVal Wb As Workbook, ByVal Sh As Object)
Sh.Move After:=Wb.Sheets(Wb.Sheets.Count)
End Sub
滚动鼠标
第十行移到窗口的最上面?
Worksheets("Sheet1").Activate
ActiveWindow.ScrollRow = 10
打印
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
语法:
MsgBox(prompt[, buttons] [, title] [, helpfile, context])
prompt:对话框中的语句
buttons:选择按钮的形式。比如:确定;是、否;是否取消 等等,可以根据下拉表来选。
title:弹出框的标题。
返回值:
对于函数形态的,会根据用户点选的按钮返回一个integer值,对应如下:
常数 值 说明
vbOK 1 确定
vbCancel 2 取消
vbAbort 3 终止
vbRetry 4 重试
vbIgnore 5 忽略
vbYes 6 是
vbNo 7 否
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
或者
Workbooks.Open fileName:=WorkLocation + "\courier\ZY1-LITEON.xlsx"
'Application.ActivePrinter = "KD860 on Ne04:"
'ActiveWindow.SelectedSheets.PrintOut Copies:=1
'ActiveWindow.Close SaveChanges:=False
等待一段时间
Application.Wait (Now + TimeValue("00:00:06"))
If Else
If Then
ElseIf
End If
打印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
获取路径
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?
设置Macros安全
或者 File /Options
如果还是Block,右键文件属性
修复乱码
Tools / Options
Control Pannel / Region
打开VBA
Alt +F11
快速打开VBA
File/Options/Customize Ribbon
升级及兼容
#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