|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
我在vb6.0里面的vba代码如下:
Option Explicit
Sub hbgzb(EApp As Excel.Application, r As Long, c As Integer, i As Long, _
str As String, str1 As String, str2 As String, filename, erow, arr, fn) '把各个工作簿中保存的信息汇总到同文件夹中另一个工作薄的同一张工作表里
Dim excelApp As New Excel.Application
'创建工作簿对象
Dim excelWorkBook As New Excel.Workbook
'创建工作表对象
Dim Wb As Excel.Workbook, sht As Excel.Worksheet
Set Wb = EApp.ThisWorkbook
Set sht = Wb.Worksheet
Application.DisplayAlerts = False
Application.ScreenUpdating = False
On Error Resume Next
str = InputBox(prompt:="Please enter a sheet name, or click cancel", Title:="Operating tips information") '输入表名
str1 = InputBox(prompt:="Please enter the number of columns:", Title:="Operating tips information") '输入列数
str2 = InputBox(prompt:="Please enter the starting position:", Title:="Operating tips information") '输入起始位置
i = 0
r = 1 '1是表头的行数
c = str1 - 2 '8是表头的列数
'Range(Cells(r + 1, "A"), Cells(65536, c)).ClearContents '清除汇总表中原表数据
Cells.ClearContents
'Dim filename As String, sht As Worksheet, erow As _
Long, fn As String, arr As Variant
filename = Dir(ThisWorkbook.Path & "\*.xls")
Do While filename <> ""
If filename <> ThisWorkbook.Name Then '判断文件是否是本工作簿
erow = Range("A1").CurrentRegion.Rows.Count + 1 '取得汇总表中第一条空行行号
fn = ThisWorkbook.Path & "\" & filename
Set Wb = GetObject(fn) '将fn代表的工作簿对象赋给变量
Set sht = Wb.Worksheets(str) '汇总的是第一张工作表
'将数据表中的记录保存在arr数组里
i = i + 1
arr = sht.Range(sht.Cells(str2, "A"), sht.Cells(65536, "B").End(xlUp).Offset(0, c))
'将数组arr中的数据写入工作表
Cells(erow, "A").Resize(UBound(arr, 1), UBound(arr, 2)) = arr
Wb.Close False
End If
filename = Dir '用Dir函数取得其他文件名,并赋给变量
Loop
MsgBox " Merge Completed !" & Chr(13) & "The merged a total of " & i & " tables", Title:="Operating tips information"
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
生成dll文件后调用代码:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Shell "Regsvr32 /u /s " & VBA.Chr(34) & ThisWorkbook.Path & "\hbgzb.dll" & VBA.Chr(34), vbHide
End Sub
Private Sub Workbook_Open()
Shell "Regsvr32 /s " & VBA.Chr(34) & ThisWorkbook.Path & "\hbgzb.dll" & VBA.Chr(34), vbHide
End Sub
模块:
Sub test1()
Dim sa As New hbb
sa.hbgzb
Set sa = Nothing
End Sub
为什么引用不了,我已经在项目-引用里添加 Microsoft Excel
|
|