|
楼主 |
发表于 2012-5-19 21:11
|
显示全部楼层
模块中的代码:- Sub actionModule()
- Dim i, j As Integer
- Dim num As Integer
- Dim extention As String
- Dim Sh As Worksheet, myName$, n%
- Dim myDelimiter As String
- Dim Password As String
- Password = InputBox("要合并的工作表是否有保护?若无请点击取消,若有请输入密码。", "密码保护确认")
- Application.DisplayAlerts = False
- Application.ScreenUpdating = False
- For Each Sh In Worksheets
- If Sh.Name <> ActiveSheet.Name Then '可修改参数
- Sh.Delete
- End If
- Next
- For i = 1 To shsSelect.ListView2.ListItems.Count
- If shsSelect.ListView2.ListItems(i).Checked = True Then
- num = num + 1
- End If
- Next i
- If num = 0 Then
- MsgBox "请注意,您没有选定任何工作表合并!请重新选择需要合并的工作表!"
- Exit Sub
- End If
- n = 1
- myName = Dir(ThisWorkbook.Path & "\*.xls*")
- Range("a2:b65536").ClearContents
- Range("a2:b65536").Hyperlinks.Delete
- PP:
- myDelimiter = InputBox("请输入文件名的分割标识符,默认设置将以被合并Workbook的文件名为Worksheet名称。若输入被合并文件的统一后缀(或文件名中均有的相同文本),如“.20101231”,则被合并Workbook文件名在“.20101231”以前部分将作为合并后的Worksheet名称。", "请输入文件名分隔符", ".xl")
- If myDelimiter = "" Then
- MsgBox "分隔符不能为空!请重新输入分隔符!"
- GoTo PP
- End If
- Do While myName <> ""
- If myName <> ThisWorkbook.Name Then
- If InStr(myName, myDelimiter) = 0 Then
- MsgBox "输入的分隔符有错误,文件名中并不包含此字符串,请重新输入分隔符。"
- GoTo PP
- End If
- Workbooks.Open ThisWorkbook.Path & "" & myName
- If Password <> "" Then
- For j = 1 To ActiveWorkbook.Sheets.Count
- ActiveWorkbook.Sheets(j).Unprotect Password
- Next j
- End If
- Application.StatusBar = "正在处理工作薄" & ActiveWorkbook.Name & ",请稍候……"
- For i = 1 To shsSelect.ListView2.ListItems.Count
- If shsSelect.ListView2.ListItems(i).Checked = True Then
- ActiveWorkbook.Sheets(shsSelect.ListView2.ListItems(i).Text).Activate
- ActiveWorkbook.Sheets(shsSelect.ListView2.ListItems(i).Text).Range("D217:J245").Select
- Selection.Copy
- Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
- :=False, Transpose:=False
- ActiveWindow.View = xlNormalView
- ActiveWindow.Zoom = 85
- ActiveWorkbook.Sheets(shsSelect.ListView2.ListItems(i).Text).Copy After:=ThisWorkbook.Sheets(n)
- n = n + 1
- If num > 1 Then
- extention = "-" & shsSelect.ListView2.ListItems(i).Text
- Else
- extention = ""
- End If
- ThisWorkbook.Sheets(n).Name = VBA.Left(myName, InStr(myName, myDelimiter) - 1) & extention
- ThisWorkbook.Worksheets("首页").Range("a" & n) = n - 1
- ThisWorkbook.Worksheets("首页").Hyperlinks.Add ThisWorkbook.Worksheets("首页").Range("b" & n), Address:="", SubAddress:="'" & ThisWorkbook.Sheets(n).Name & "'!A1", ScreenTip:=ThisWorkbook.Sheets(n).Name, TextToDisplay:=ThisWorkbook.Sheets(n).Name
- ActiveSheet.Hyperlinks.Add ActiveSheet.Range("p1"), Address:="", SubAddress:=Sheets(1).Name & "!A1", ScreenTip:="返回首页", TextToDisplay:="返回"
- Workbooks(myName).Activate
- End If
- Next i
- If Password <> "" Then
- For j = 1 To ActiveWorkbook.Sheets.Count
- ActiveWorkbook.Sheets(j).Protect Password
- Next j
- End If
- Workbooks(myName).Close
- End If
- myName = Dir
- Loop
- Application.StatusBar = False
- ThisWorkbook.Sheets(1).Activate
- Application.ScreenUpdating = True
- Application.DisplayAlerts = True
- Application.Calculation = xlCalculationAutomatic
- End Sub
复制代码 按钮的代码:
- Private Sub CommandButton1_Click()
- Application.Calculation = xlCalculationManual
- If ThisWorkbook.Worksheets.Count > 1 Then
- If MsgBox("重新导入报表将删除原来报表,继续吗? ", 52, "警告") = 7 Then
- Exit Sub
- Else
- shsSelect.Show
- End If
- Else
- shsSelect.Show
- End If
- End Sub
复制代码- Private Sub CommandButton1_Click()
- Dim i, j As Integer
- ListView2.ListItems.Clear
- For i = 1 To ListView1.ListItems.Count
- If ListView1.ListItems(i).Checked = True Then
- j = j + 1
- ListView2.ListItems.Add , , ListView1.ListItems(i).Text
- ListView2.ListItems(j).Checked = True
- End If
- Next i
- If j > 0 Then
- CommandButton5.Enabled = True
- CommandButton6.Enabled = True
- End If
- End Sub
- Private Sub CommandButton2_Click()
- Dim i As Integer
- For i = ListView2.ListItems.Count To 1 Step -1
- If ListView2.ListItems(i).Checked = True Then
- ListView2.ListItems.Remove i
- End If
- Next i
- If ListView2.ListItems.Count = 0 Then
- CommandButton5.Enabled = False
- CommandButton6.Enabled = False
- End If
- End Sub
- Private Sub CommandButton3_Click()
- Dim i As Integer
- For i = 1 To ListView1.ListItems.Count
- ListView1.ListItems(i).Checked = True
- Next i
- End Sub
- Private Sub CommandButton4_Click()
- Dim i As Integer
- For i = 1 To ListView1.ListItems.Count
- ListView1.ListItems(i).Checked = False
- Next i
- End Sub
- Private Sub CommandButton6_Click()
- Dim i As Integer
- For i = 1 To ListView2.ListItems.Count
- ListView2.ListItems(i).Checked = True
- Next i
- End Sub
- Private Sub CommandButton5_Click()
- Dim i As Integer
- For i = 1 To ListView2.ListItems.Count
- ListView2.ListItems(i).Checked = False
- Next i
- End Sub
- Private Sub CommandButton7_Click()
- shsSelect.Hide
- Call actionModule
- End Sub
- Private Sub UserForm_Initialize()
- Dim myFileName As String
- Dim myName As String
- Dim i As Integer
- Dim wbName As String
- Application.DisplayAlerts = False
- Application.ScreenUpdating = False
- 'VBA.ChDir Application.DefaultFilePath '改变打开的默认路径
- 'myName = ThisWorkbook.Path & ""
- 'SendKeys myName & "{TAB}", True
- PP:
- With Application.FileDialog(msoFileDialogFolderPicker)
- .InitialFileName = ThisWorkbook.Path & ""
- End With
- myFileName = Application.GetOpenFilename("Excel工作薄 (*.xls*),*.xls*")
- If myFileName = "False" Then
- MsgBox "没有选择文件!请重新选择一个被合并文件!", vbInformation, "取消"
- GoTo PP
- Else
- If myFileName = myName & ThisWorkbook.Name Then
- MsgBox "您选择的是合并模板,请重新选择一个被合并文件!"
- GoTo PP
- Else
- With ListView2
- .ColumnHeaders.Clear
- .ListItems.Clear
- .ColumnHeaders.Add , , "选定的被合并工作表", ListView2.Width '添加列标
- .Gridlines = True
- End With
- With ListView1
- .ColumnHeaders.Clear
- .ListItems.Clear
- .ColumnHeaders.Add , , "请选择被合并工作表,可以多选", ListView1.Width '添加列标
- .Gridlines = True
- End With
-
-
- Workbooks.Open Filename:=myFileName
- wbName = ActiveWorkbook.Name
- Windows(ActiveWorkbook.Name).Visible = False
-
- For i = 1 To Workbooks(wbName).Worksheets.Count '获得被合并工作薄各工作表名称
- ListView1.ListItems.Add , , Workbooks(wbName).Worksheets(i).Name
- Next i
-
- Workbooks(wbName).Close
- End If
-
- End If
- CommandButton5.Enabled = False
- CommandButton6.Enabled = False
-
- End Sub
复制代码 上面的是useform的代码。这样看着太乱了吧。。。 |
|