我需要在excel中编写一个名为“ refresh”的简单宏,该宏可以执行以下操作:
我写了一些代码,效果很好。但是,它有两个问题:
因为我是vba的新手,所以我想寻求帮助来解决这些问题
这是我尝试过的代码:
Dim ExcelObj As Object
Dim ExcelBook As Object
Dim ExcelSheet As Object
Cells.ClearContents
Path = ThisWorkbook.Path & "\Sources\"
File = Dir(Path & "*.xls*")
Idx = 1
RowIdx = 3
Set ExcelObj = CreateObject("Excel.Application")
Do Until File = ""
ExcelObj.Workbooks.Open Path & File
Set ExcelBook = ExcelObj.Workbooks(1)
Set ExcelSheet = ExcelBook.Worksheets(1)
If Idx = 1 Then
Offset = 0
Else
Offset = 1
End If
nr = ExcelSheet.UsedRange.Rows.Count
nc = ExcelSheet.UsedRange.Columns.Count
tmp = ExcelSheet.Range(Cells(1 + Offset, 1).Address(), Cells(nr, nc).Address()).Value
Range(Cells(RowIdx, 1).Address(), Cells(RowIdx + nr - 1 - Offset, nc).Address()).Value = tmp
RowIdx = RowIdx + nr - Offset
File = Dir
Idx = Idx + 1
Set ExcelSheet = Nothing
Set ExcelBook = Nothing
Loop
Set ExcelObj = Nothing
如前所述,该代码可以正常运行,至少就我在测试中可以看到的程度而言。但是它非常慢,并且不会释放我已阅读的文件。