|
楼主 |
发表于 2010-8-16 16:46
|
显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
问题35:如何在不同的工作表之间进行复制?
问题:请问如何用函数将表格1自动复制至表格2对应的页?例如:我想将表格1对应的1、2、3、4复制至表格2对应1、2、3、4时,它会按要求自动复制,同时,当我想将表格1对应1、2复制至表格2对应1、2,表格1其余3、4不想同时复制,怎样可以做到呢?
解答:(陈希章)
至少有两个同时打开的工作簿,受保护的工作表不能被复制(自动被隐藏掉了)。
如图,在某个工作表中制作如下窗体:
相应代码如下:
Private Sub cb1_Change()
Dim ws As Worksheet
If cb1.ListIndex <> -1 Then
Lst1.Clear
For Each ws In Workbooks(cb1.Value).Worksheets
If ws.ProtectContents = False Then Lst1.AddItem ws.Name
Next
Else
Lst1.Clear
End If
End Sub
Private Sub cb2_Change()
Dim ws As Worksheet
If cb2.ListIndex <> -1 Then
lst2.Clear
For Each ws In Workbooks(cb2.Value).Worksheets
If ws.ProtectContents = False Then _
lst2.AddItem ws.Name
Next
Else
lst2.Clear
End If
End Sub
Private Sub cmdadd_Click()
Dim n As Integer
IfLst1.ListIndex <> -1 And lst2.ListIndex <> -1Then
If cb1.Value <> cb2.Value Then
lst3.AddItem cb1.Value
n = lst3.ListCount - 1
lst3.List(n, 1) = Lst1.Value
lst3.List(n, 2) = "=>"
lst3.List(n, 3) = cb2.Value
lst3.List(n, 4) = lst2.Value
Else
MsgBox "必须选择两个不同的工作簿", vbExclamation, "错误"
End If
Else
MsgBox "必须先选择两个工作表", vbExclamation, "错误"
End If
End Sub
Private Sub cmddelete_Click()
Dim n As Integer
n = lst3.ListIndex
If n <> -1 Then
lst3.RemoveItem n
Else
MsgBox"请先选择一个要删除的条件", vbExclamation, "错误"
End If
End Sub
Private Sub cmdgo_Click()
Dim n AsInteger, m As Integer
Dim sws AsWorksheet, dws As Worksheet
n =lst3.ListCount
If n > 0Then
For m = 0 To n - 1
Set sws = Workbooks(lst3.List(m, 0)).Worksheets(lst3.List(m,1))
Set dws = Workbooks(lst3.List(m, 3)).Worksheets(lst3.List(m,4))
sws.Cells.Copy dws.Cells
Next
MsgBox "复制完毕", vbInformation, "报告"
Else
MsgBox "没有需要执行的任务", vbExclamation, "错误"
End If
End Sub
Private Sub CommandButton2_Click()
UnloadMe
End Sub
Private Sub UserForm_Initialize()
Dim wb As Workbook
Dim n As Integer
n = Application.Workbooks.Count
If n = 1 Then
cb1.Enabled= False
cb2.Enabled= False
Lst1.Enabled= False
lst2.Enabled= False
cmdadd.Enabled = False
cmddelete.Enabled = False
cmdgo.Enabled = False
MsgBox"当前只有一个工作簿", vbExclamation, "错误"
ExitSub
Else
For Each wbIn Application.Workbooks
cb1.AddItem wb.Name
Next
cb1.Value =ThisWorkbook.Name
cb2.List =cb1.List
End If
End Sub
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = == = = =
问题36:如何在打开工作簿时自动运行宏?
解答:通过Workbook_Open事件,有两种基本的方法可以运行/执行宏代码,见下面的示例:
Private Sub Workbook_Open()
MsgBox"您好!", vbInformation, "fanjy.blog.excelhome.net"
End Sub
也可以通过下面的方式:
Private Sub Workbook_Open()
Run"MyMacro"
End Sub
Private Sub MyMacro()
MsgBox "您好", vbInformation,"fanjy.blog.excelhome.net"
End Sub
但是MyMacro宏程序必须处于任何其它独立的模块(即“插入>>模块”),不能与Work_Open事件在同一模块中。如果想在同一Private模块中,则不能使用Run语句,只使用程序名,如:
Private Sub Workbook_Open()
MyMacro
End Sub
Private Sub MyMacro()
MsgBox "您好", vbInformation,"fanjy.blog.excelhome.net"
End Sub
- - - - - - - - - - - - - - - - -- - - - - - - - - - - - - - - - - - - - - - -
问题37:如何在指定的时间或指定的间隔运行宏?
解答:有时想在预定的时间运行宏或在指定的间隔运行宏,则可以使用Application对象的OnTime方法来实现。例如,如果想在每天下午15:00运行某个宏,可以将代码放在Work_Open事件中:
Private Sub Workbook_Open()
Application.OnTime TimeValue("15:00:00"),"MyMacro"
End Sub
“MyMacro”是想运行的宏名,放置在单独的模块中,也有OnTime方法:
Sub MyMacro()
Application.OnTime TimeValue("15:00:00"),"MyMacro"
'<在这里放置代码>
End Sub
此时,将在每天下午15点运行MyMacro过程。
如果想在打开工作簿后,每隔15分钟运行MyMacro宏,则可以在ThisWorkbook模块中输入下面的代码:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Application.OnTime dTime, "MyMacro", ,False
End Sub
Private Sub Workbook_Open()
Application.OnTime Now + TimeValue("00:15:00"),"MyMacro"
End Sub
然后,在独立的模块中,输入下面的代码:
Public dTime As Date
Sub MyMacro()
dTime = Now + TimeValue("00:15:00")
Application.OnTime dTime, "MyMacro"
'<在这里放置代码>
End Sub
- - - - - - - - - - - - - - - - -- - - - - - - - - - - - - - - - - - - - - - - |
|