如何做:打开excel某文件的时候自动运行宏,弹出窗口,可以让人按几个条件选择该文件中某行信息

dayasky 2004-07-23 10:47:21
这个excel中就一个人员表,有几个人员属性的字段,如姓名、岁数、特长等
问题:
1、打开excel的时候自动弹出选择的宏窗口
2、按已经有的字段选择条件,然后将合乎条件的人都取出来
3、可以在合乎条件的人中随机选一个人
呵呵,这个问题好像不复杂,可是我不会,所以对我来说很难

各位大侠,有没有这样的例子?或者指点我一下
在这里回复或社区消息或email:dayasky@163.com都感激不尽
我是一名c#程序员,看得懂vb,如果有例子给我看看应该就能明白了
只是没有做过,现在感觉没有门路

这是表哥叫我帮忙的东西,偶说给做个程序不就好了,可是他不干,说最好用excel的宏实现
唉,难倒我了,大侠们帮个忙吧
第一次来这个块,只能给100分,对不住了


...全文
6589 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
dayasky 2004-07-23
  • 打赏
  • 举报
回复
实在不好意思,因为没有用过
我新建一个excel文件,打开它,选择工具、宏、用vb编辑
然后在打开的vba编辑器中点击ThisWorkBook,在打开空的文件中复制你上面的代码
然后点运行的时候,选了AutoExec,就报无效的过程引用和参数的错误
再再指点一下吗?
Tooler 2004-07-23
  • 打赏
  • 举报
回复
Excel里有四个自动宏,其中的宏名是不能改变的。
//自动宏
Option Explicit
Sub AutoExec()
' Adds an item to the Tools menu.
' This procedure runs automatically when the add-in loads.

Dim offCmdBrPp As CommandBarPopup ' This will be set to
' the Tools menu.
Dim offCmdBtn As CommandBarButton ' This will be set to
' the Project command.

' Create an object variable referring to the Tools menu.
Set offCmdBrPp = _
Application.CommandBars("Menu Bar").Controls("Tools")

' If the Address Book Report command already exists, delete it.
On Error Resume Next
offCmdBrPp.Controls("Address Book Report").Delete

' Add a command in the 4th position on the Tools menu.
Set offCmdBtn = _
offCmdBrPp.Controls.Add(Type:=msoControlButton, Before:=4)

' Set properties on the new command.
With offCmdBtn
.Caption = "Address Book Report"
.OnAction = "ListAddresses"
End With
End Sub


Sub AutoExit()
' Deletes an item on the Tools menu.
' This procedure runs automatically when the add-in unloads.

' If the AddressBook Report command exists, delete it.
On Error Resume Next
Application.CommandBars("Menu Bar").Controls _
("Tools").Controls("Address Book Report").Delete

End Sub


Sub ListAddresses()
' This procedure adds Outlook addresses to a new Word document.
' Called by the OnAction property in the AutoExec procedure.

Dim olList As AddressList ' Outlook address book.
Dim olEntry As AddressEntry ' Outlook address item.
Dim appOutl As Outlook.Application
Dim olNmSpc As Outlook.NameSpace
Dim wrdReportPara As Word.Range
Dim intRepeat As Integer

' Set a reference to an Outlook addresss book.
Set appOutl = New Outlook.Application
Set olNmSpc = appOutl.GetNamespace("MAPI")

' If your system doesn't have a PAB installed,
' the solution will end.
On Error GoTo NoPABAvailable
Set olList = olNmSpc.AddressLists("Personal Address Book")

' Open a new blank document.
Documents.Add

' Add and format text.
' Add 5 paragraphs.
For intRepeat = 0 To 4
ActiveDocument.Paragraphs.Add
Next intRepeat

' Set this object to the 4th paragraph.
Set wrdReportPara = ActiveDocument.Paragraphs(4).Range

' Add and format text.
wrdReportPara.InsertBefore Text:="Address Book"
With wrdReportPara.Font
.Name = "arial"
.Size = 12
.Bold = True
.Italic = True
End With

' Select a range, add a paragraph.
Set wrdReportPara = wrdReportPara.Next
Call AddPara(wrdReportPara)

' Add addresses to the active document.
For Each olEntry In olList.AddressEntries
wrdReportPara.InsertBefore olEntry.Name
Call AddPara(wrdReportPara)
wrdReportPara.InsertBefore olEntry.Address
Call AddPara(wrdReportPara)
Call AddPara(wrdReportPara)
Next

' If your system doesn't have a PAB installed,
' the solution will end.
NoPABAvailable:
MsgBox "Personal Address Book is not available." _
vbTab "Shutting down."
End

' Clean up object variables.
Set olList = Nothing
Set olNmSpc = Nothing
Set appOutl = Nothing
Set wrdReportPara = Nothing

End Sub

dayasky 2004-07-23
  • 打赏
  • 举报
回复
现在已经实现的功能是:
1:打开excel文件的时候自动弹出我的用户窗体
Private Sub Workbook_Open()
UserForm1.Show vbModal
End Sub

2和3还没有门路啊
唉,刚学.net的时候还有自带的详细例子看着做一下,这个vba如何按条件搜索excel啊?
大侠请不吝指教

5,139

社区成员

发帖
与我相关
我的任务
社区描述
其他开发语言 Office开发/ VBA
社区管理员
  • Office开发/ VBA社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧