Application.ScreenUpdating = False
Application.DisplayAlerts = False
'**引用打开窗口
Dim fd As FileDialog
Dim vrtSelectedItem As Variant
Set fd = Application.FileDialog(msoFileDialogOpen)
fd.InitialFileName = Sheets("设置").Range("CU7").Value & "\库存核对" '默认打开的文件夹
With fd
.AllowMultiSelect = True '可选多个文件
If .Show = -1 Then
For Each vrtSelectedItem In .SelectedItems
FJ = Split(vrtSelectedItem, "\")
ThisWorkbook.Sheets("设置").Range(CR).Value = FJ(3) '记录文件名
ThisWorkbook.Sheets("设置").Range("AG1").Value = FJ(3) '记录文件名
fd.Execute '执行打开
Me.CommandButton62.Enabled = True
Exit For
Next
End If
End With
Set fd = Nothing
Environ("Computername")
Selection.Copy
Range("E5").Select
ActiveSheet.PasteSpecial Format:=3, Link:=1, DisplayAsIcon:=False, _
IconFileName:=False
ActiveWindow.FreezePanes = False
ActiveWindow.FreezePanes = True
With ActiveSheet.PageSetup
.LeftFooter = "编制: 审核:" '页脚LEFT
.PrintTitleRows = "$1:$3" '要打印的默认页头
.PrintArea = "$A$1:$E$12" '打印区域
End With
.PrintOut Copies:=2 '打印(2份)
****设置批注
Range("F8").AddComment'添加批注
Range("F8").Comment.Visible = False'隐藏框
Range("F8").Comment.Text Text:="黄传兵:" & Chr(10) & "SS"
Dim I As Integer
Dim MC, MC_CR As String
L3 = ThisWorkbook.Sheets("设置").Range("N2").Value
For I = 4 To L3 + 3
MC_CR = "N" & I
MC = ThisWorkbook.Sheets("设置").Range(MC_CR).Value
If UCase(MC) = UCase(WJ) Then
OPEN_JL = "Y"
Exit For
End If
Next I
End Function
'打开需引用的文件
Public Sub OPEN_WJ(LJ, WJ As String)On Error GoTo X:
Dim M4, Y3 As String
Dim LJWJ As String
LJWJ = LJ & WJ
If OPEN_YN(WJ) <> "Y" Then '如果未被其它引用并打开
Workbooks.Open Filename:=LJWJ
L3 = ThisWorkbook.Sheets("设置").Range("N2").Value
M3_CR = "N" & L3 + 4
M4_CR = "O" & L3 + 4
ThisWorkbook.Sheets("设置").Range(M3_CR).Value = WJ
ThisWorkbook.Sheets("设置").Range(M4_CR).Value = 1
Windows(WJ).Visible = False
Else '如果已被其它引用并打开
If OPEN_JL(WJ) = "" Then
L3 = ThisWorkbook.Sheets("设置").Range("N2").Value
M3_CR = "N" & L3 + 4
M4_CR = "O" & L3 + 4
ThisWorkbook.Sheets("设置").Range(M3_CR).Value = WJ
ThisWorkbook.Sheets("设置").Range(M4_CR).Value = 2
End If
End If
Exit Sub
X:
MsgBox """ & WJ & ""未打开,请检查路径。"
'检测文件是否已经打开
Public Function OPEN_YN(WJ As String) Dim X As Workbook
For Each X In Application.Workbooks
If UCase(CStr(X.Name)) = UCase(WJ) Then
OPEN_YN = "Y"
Exit For
End If
Next
End Function
'关闭引用文件
Public Sub CLOSE_YY() On Error Resume Next
Dim I, L As Integer
Dim MC, MC_CR, ZT, ZT_CR As String
L = ThisWorkbook.Sheets("设置").Range("N2").Value
For I = L + 3 To 4 Step -1
MC_CR = "O" & I
ZT_CR = "P" & I
MC = ThisWorkbook.Sheets("设置").Range(MC_CR).Value
ZT = ThisWorkbook.Sheets("设置").Range(ZT_CR).Value
If MC <> "" Then
If Workbooks(MC).Saved = False Then Workbooks(MC).Save
If ZT = 1 Then Workbooks(MC).Close '如果是本文件引用并打开的则关闭
ThisWorkbook.Sheets("设置").Range(MC_CR).Value = ""
ThisWorkbook.Sheets("设置").Range(ZT_CR).Value = ""
End If
Next I
End Sub
***设置控件变量
Dim LB As MSForms.Label
Set LB = SYS.Controls("LB" & I + 1)
, ReadOnly:=True
, SaveChanges:=False
TextBox1.MaxLength = 5 '最大允许输入的字符长度5
TextBox1.AutoTab = True '当达到最大允许输入的字符长度是,自动跳格
Right(ActiveWorkbook.Name, Len(ActiveWorkbook.Name) _
- InStr(ActiveWorkbook.Name, ".") + 1)
Z = Me.TextBox37.Value
LS = InStr(1, Z, "(")
RS = InStr(1, Z, ")")
Replace(Z, Mid(Z, LS + 1, RS - LS - 1), Sheets("设置").Range("J1").Value)
***单元格背景、前景设置
.Cells(R + 1, C).Interior.Color = 255'背景红
.Cells(R + 1, C).Font.ThemeColor = xlThemeColorDark1 '前景白
.Cells(R + 1, C).Font.ColorIndex = xlAutomatic'前景黑(默认)
***当前单元格的行、列号
Selection.Row
Selection.Column
***当关闭文件时自动备份----------------------------------
Dim NEW_NAME As String
NEW_NAME = Year(Date) & Month(Date)
NEW_NAME = "\\Ck2\公司平台 (e)\仓库备份勿删\月度进销存" & NEW_NAME & ".xlsm"
Me.SaveAs Filename:=NEW_NAME, FileFormat:=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False
'U_NAME是修改人的名字
WITH RANGE(CR)
If .Comment Is Nothing Then
.AddComment
.Comment.Visible = False
.Comment.Text Text:=U_NAME & ":" & Chr(10) & "原" & Z & "," & Date & GG
Else
.Comment.Text Text:=.Comment.Text & Chr(10) & U_NAME & ":" & Chr(10) & "原" & Z & "," & Date & GG
End If
END WITH
Public Function HOW_CS(STR1 As String, STR2 As String) '得到 STR2 在 STR1 中出现的次数
Dim I As Integer
Dim B As String
'黄传兵定稿的2008-12-17
B = STR1
If InStr(B, STR2) = 0 Then
I = 0
Else
For I = 1 To 50
B = Replace(B, Left(B, InStr(B, STR2)), "", 1, 1)
If Len(B) = 0 Or InStr(B, STR2) = 0 Then
Exit For
End If
Next I
End If
HOW_CS = I
End Function
用API切换打印机
Application.Dialogs(xlDialogPrinterSetup).Show
Application.ActivePrinter'当前打印机
'隐藏列
Columns(I + J).EntireColumn.Hidden = True '隐藏列
'隐藏行
Rows(I).EntireRow.Hidden =True
'隐藏表
Sheets("表1").Visible = False
'为Image控件添加图片
Me.Image1.Picture = LoadPicture("E:\跟踪卡管理系统\跟踪卡日志\CT1.jpg")
Sub OUT_JPG() '将图表另存为JPG
Dim shap As Shape
Dim i As Integer
With ThisWorkbook.Sheets("1")
For i = 1 To .Shapes.Count
Set shap = .Shapes(i)
shap.Copy
With .ChartObjects.Add(0, 0, shap.Width, shap.Height).Chart
.Paste
.Export "d:\" & i & ".jpg"
.Parent.Delete
End With
Next i
End With
End Sub
'动态添加控件
Set Mycmd = Controls.Add("MsForms.CommandButton.1") ', CommandButton2,Visible)
Mycmd.Left = 18
Mycmd.Top = 150
Mycmd.Width = 175
Mycmd.Height = 20
Mycmd.Caption = "非常有趣。" & Mycmd.Name
'数字转换为中文大写(A1单元格)公式
=IF(A1<0,"(金额为负无效)",IF((A1-INT(A1))=0,"(人民币)"&TEXT(A1,"[DBNUM2]")&"元整",IF(INT(A1*10)-A1*10=0,"(人民币)"&TEXT(INT(A1),"[DBNUM2]")&"元"&TEXT((INT(A1*10)-INT(A1)*10),"[DBNUM2]")&"角整",TEXT(INT(A1),"[DBNUM2]")&"元"&IF(INT(A1*10)-INT(A1)*10=0,"零",TEXT(INT(A1*10)-INT(A1)*10,"[DBNUM2]")&"角")&TEXT(RIGHT(A1,1),"[DBNUM2]")&"分")))
Sheets("Sheet1").Copy Before:=/After:=Sheets(2)
Sheets("Sheet1 (4)").Name = "1"
Public Sub QHHZ(TXT As MSForms.TextBox, GJZ, DTHZ As String)
'将指定文本框中指定的文字块(可多选,用“,”分隔)替换为特定的文字(文本框名,要替换的字,被替换的字)
Dim I As Integer
Dim Y As String
Dim FJ() As String
With TXT
If .Value <> "" Then
FJ = Split(DTHZ, ",")
Y = ""
For I = 0 To 3
If InStr(1, .Value, FJ(I)) <> 0 Then '如果找到FJ(I)最先出现的位置
Y = "Y"
Exit For
End If
Next I
If Y = "Y" Then
.Value = Replace(.Value, FJ(I), GJZ)
Else
.Value = .Value & GJZ
End If
End If
.SetFocus
End With
End Sub
.Underline = xlUnderlineStyleSingle
End With
Private Sub TextZ_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
'如果离开TextZ,按"回车"则转移焦点到TextX
If KeyCode = 13 Then
With Me.TextX
.SetFocus
If .Value <> "" Then
.SelStart = 0
.SelLength = Len(.Value)
End If
End With
End If
End Sub
'将列表框中的数据分别显示到文本框中
With Me
If .ListXYZ.ListIndex <> -1 Then
.LabelId = .ListXYZ.Column(0, .ListXYZ.ListIndex)
.TextX = .ListXYZ.Column(1, .ListXYZ.ListIndex)
.TextY = .ListXYZ.Column(2, .ListXYZ.ListIndex)
.TextZ = .ListXYZ.Column(3, .ListXYZ.ListIndex)
End If
End With
End Sub
'获得某列最后一个有数据的行/列号
MsgBox ThisWorkbook.Sheets("A7").Range("zz2").End(xlUp).Column
退出当前excel进程
|
xy As String, Optional cf = False) As String
If Me.Saved = False And Not Me.ReadOnly Then
Me.Save
Else
Me.Saved = True
End If
End Sub
Selection.NumberFormatLocal = "@"
ActiveSheet.ShowAllData
'“关闭”文件前自动判断是否为“只读方式”打开,若是则不提示保存,否则自动保存并关闭,适用于文件BeforeClose事件中
If .ReadOnly = True Then
.Saved = True
Else
If .Saved = False Then
.Save
.Close
End If
End If
End With
解决VBA运行因公式造成缓慢的问题
'计算程序运行时间(转换为秒)
Me.Label6.Caption = "用时:" & Round((time2 - time1) * 24 * 3600, 1) & " 秒" '显示用时
Cells.Interior.Color = Sheets("设置").Range("G1").Interior.Color'背景色
Cells.Font.Color = Sheets("设置").Range("G1").Font.Color'前景色
End If
Application.ScreenUpdating = False
Application.DisplayStatusBar = False
Application.Calculation = xlCalculationManual
Application.EnableEvents = False
ActiveSheet.DisplayPageBreaks = False '注:这是工作表级的设置
Application.ScreenUpdating = True 'screenUpdateState
Application.DisplayStatusBar = True 'statusBarState
Application.Calculation = xlAutomatic 'calcState
Application.EnableEvents = True 'eventsState
ActiveSheet.DisplayPageBreaks = True 'displayPageBreaksState '注:这是工作表级的设置
With ActiveSheet.PageSetup
.LeftMargin = Application.InchesToPoints(0.36)
.RightMargin = Application.InchesToPoints()
.TopMargin = Application.InchesToPoints()
.BottomMargin = Application.InchesToPoints()
End With
Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Type POINTAPI
X As Long
Y As Long
End Type
Public Function getmouse_x_y() As POINTAPI
GetCursorPos getmouse_x_y
End Function
sub test()
'call getmouse_x_y '调用“获取鼠标坐标值过程”(假定你们给的过程/程序,名叫getmouse_x_y)
if getmouse_x_y.x>100 and getmouse_x_y.y>100 then …… '根据返回当前鼠标的坐标值执行某过程/程序
……
end sub
Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
'为获取鼠标位置,声明POINTAPI数据结构
Type POINTAPI
X As Long
Y As Long
End Type
'获取鼠标位置
GetCursorPos get_point
'MsgBox get_point.X & "," & get_point.Y
End Function
'获取数组元素数