搜索
查看: 2263|回复: 0

EXCEL VBA常用代码集

[复制链接]

977

主题

1093

帖子

1万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
15934
发表于 2018-12-20 09:04:05 | 显示全部楼层 |阅读模式
1.显示活动工作簿名称
MsgBox "当前活动工作簿是 " & ActiveWorkbook.Name
2.保存活动工作簿
Activeworkbook.Save
3.保存所有打开的工作簿关闭 EXCEL
For Each W in Application.Workbooks
W.Save
Next W
Application.Quit
4.将网格线设置为蓝色
ActiveWindow.GridlineColorIndex = 5
5.将工作表sheet1 隐藏
Sheet1.Visible = xlSheetVeryHidden
6.将工作表Shtte1 显示
Sheet1.Visible = xlSheetVisible
7.单击某单元格,该单元格所在的行以蓝色背景填充 ,字体颜色为白色
Private Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)
  If Target.Row >= 2 Then’第二行以下的区域
     On Error Resume Next
     [ChangColor_With1].FormatConditions.Delete
     Target.EntireRow.Name = "ChangColor_With1"
     With [ChangColor_With1].FormatConditions
      .Delete
      .Add xlExpression, , "TRUE"
      .Item(1).Interior.ColorIndex = 5
      .Item(1).Font.ColorIndex = 2
     End With
  End If
End Sub
8.使窗体在启动的时候自动最大化
Private Sub UserForm_Initialize()
   Application.WindowState = xlMaximized
   With Application
       Me.Top = .Top
       Me.Left = .Left
       Me.Height = .Height
       Me.Width = .Width
   End With
End Sub
9.不保存工作簿退出EXCEL
Application.DisplayAlerts = False
Application.Quit
10.使窗体的关闭按纽不好用
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
If CloseMode = vbformcontrdmenu Then
MsgBox "请用关闭按钮关闭窗口 !!", 64, "提示"
Cancel = True
End If
End Sub
11.使窗体在3 秒后自动关闭
Private Sub UserForm_Activate()
Application.Wait Now + TimeValue("00:00:03")
UserForm1.Hide
End Sub
12.启动窗体的时候自动使Label1显示 Sheet1工作表3 列,8行的内容
Private Sub UserForm_Activate()
Label1.Caption = Sheets("sheet1").Cells(3, 8)
End Sub
13.让按纽CommandButton1 在窗体上以不可用状态显示
CommandButton1.Enabled = False
14.让按纽Commandbutton1 在窗体上以隐藏方式存在
CommandButton10.Visible = False
15.点击Commandbutton1 按纽进入 ”工资” 工作表
Sheets("工资 ").Select
16.在Textbox1 中输入数据 ,窗体可显示出”工资 ”工作表中与输入内容关联的项
Private Sub TextBox1_Change()
 For X = 1 To Application.CountA(Sheets("工资").Range("a:a"))
If Sheets("工资 ").Cells(X, 1) = TextBox1.Text Then ’在工资表第一列查找与Textbox1输入相符的项
    Label2.Caption = Sheets("工资").Cells(X, 2) ’在Label2 中显示Textbox1数据所在的第二列的数据
    Label7.Caption = Sheets("工资").Cells(X, 3) ’在Label2 中显示Textbox1数据所在的第三列的数据 End If
   Next
End Sub
17.使EXCEL 启动的时候自动最小化 /最大化
Private Sub Workbook_Open()
Application.WindowState = xlMinimized’最小化
Application.WindowState = xlMaximized’最大化
End Sub
18.在Label25 以数字的形式显示 TextBox12×Label14 的结果
Label25.Caption = Val(TextBox12.Text) * Val(Label14.Caption)
19.单选按纽名与Sheet6工作表名相同
OptionButton6.Caption = Sheet6.Name
20.”登陆” 窗体的显示 ,隐藏
登陆 .Show’显示
登陆 .Hide’隐藏
21.使窗体的标题栏不显示
(1)插入类模块” CFormChanger” 代码如下 :
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long) As Long
Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Private Declare Function DrawMenuBar Lib "user32" (ByVal hWnd As Long) As Long
Private Const GWL_STYLE As Long = (-16)
Private Const WS_CAPTION As Long = &HC00000
Dim hWndForm As Long
……………………………………………………………………………………………………………..
Public Property Set Form(oForm As Object) '29
    If Val(Application.Version) < 9 Then
        hWndForm = FindWindow("ThunderXFrame", oForm.Caption)
    Else
        hWndForm = FindWindow("ThunderDFrame", oForm.Caption)
    End If
    SetFormStyle
End Property
……………………………………………………………………………………………………………….
Private Sub SetFormStyle()
Dim iStyle As Long, hMenu As Long, hID As Long, iItems As Integer
    iStyle = GetWindowLong(hWndForm, GWL_STYLE)
    iStyle = iStyle And Not WS_CAPTION
    iStyle = iStyle Or WS_THICKFRAME
    SetWindowLong hWndForm, GWL_STYLE, iStyle
    DrawMenuBar hWndForm
End Sub
(2)在所在窗体代码里声明
Dim oFormChanger As New CFormChanger
(3).在窗体的Activate 事件中插入代码
Set oFormChanger.Form = Me
Me.SpecialEffect = fmspecia1EffectRaised
以上三步每一步都不可缺少 ,否则不能完成.
22.单击某单元格,该单元格所在的行与列都以蓝色背景填充
Private Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)
  If Target.Row >= 2 Then ’第二行以下的所有列
     On Error Resume Next
     [ChangColor_With2].FormatConditions.Delete
     [ChangColor_With3].FormatConditions.Delete
     Target.EntireRow.Name = "ChangColor_With2"
     Target.EntireColumn.Name = "ChangColor_With3"
     With [ChangColor_With2].FormatConditions
      .Delete
      .Add xlExpression, , "TRUE"
      .Item(1).Interior.ColorIndex = 5
     End With
     With [ChangColor_With3].FormatConditions
      .Delete
      .Add xlExpression, , "TRUE"
      .Item(1).Interior.ColorIndex = 5
     End With
  End If
End Sub
23.显示动态时间
(1)插入窗体 Userform1及Label1 并在窗体声明中插入
Option Explicit
Public nextRun As Date
(2)在窗体 Activate事件中插入
Showtime
(3)在窗体 QueryClose事件中插入
Application.OnTime nextRun, "showtime", schedule:=False
(4)插入模块 Module1并输入
    Option Explicit
Sub showtime()
UserForm1.Label1 = Now
UserForm1.Repaint
DoEvents
UserForm1.nextRun = Now + 1 / 86400
Application.OnTime UserForm1.nextRun, "showtime"
End Sub
24.加载Combobox1 选项
ComboBox1.AddItem "收入型 "
ComboBox1.Additem “支出型 ”
25.使Textbox1 自动程输入状态显示 (有光标闪动)
TextBox1.SetFocus
26.打开C 盘目录
Shell "explorer.exe  C:\", 1
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

表格智创网

网站简介:表格智创网,是一家以表格设计和技能分享的专业社区,由会计帮帮网投资建设,尽专业,助提高专业技能。

表格智创网欢迎您!

联系我们

  • 工作时间:早上9:00-16:00
  • 客服电话:18668755857
  • 本站网址:www.excelwps.com
  • 淘宝店址:kjbbw.taobao.com

Powered by Discuz! X3.4 © 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表