我已经发布了一个类似的问题,但设法弄清楚如何做我需要的-几乎!
我有下面的代码,除了信息被复制到活动工作簿到第21行,它工作得很好。我不知道该怎么改变。如果我更改此部分,它会转到工作表的顶部,但我想将信息从A2行开始,然后是之后的每一行:
可在第21行插入:
'Set the destrange
Set destrange = BaseWks.Range("A2" & rnum)
用于在图纸顶部插入:
'Set the destrange
Set destrange = BaseWks.Range("A" & rnum)
任何帮助都将受到感激。
Sub MergeAllWorkbooks()
Dim MyPath As String, FilesInPath As String
Dim MyFiles() As String
Dim SourceRcount As Long, FNum As Long
Dim mybook As Workbook, BaseWks As Worksheet
Dim sourceRange As Range, destrange As Range
Dim rnum As Long, CalcMode As Long
'Fill in the path\folder where the files are
MyPath = "C:\Users\jlinney\OneDrive - Distell\Desktop\Personal\NOMAD\Global Mobility\Excel From"
'Add a slash at the end if the user forget it
If Right(MyPath, 1) <> "\" Then
MyPath = MyPath & "\"
End If
'If there are no Excel files in the folder exit the sub
FilesInPath = Dir(MyPath & "*.xl*")
If FilesInPath = "" Then
MsgBox "No files found"
Exit Sub
End If
'Fill the array(myFiles)with the list of Excel files in the folder
FNum = 0
Do While FilesInPath <> ""
FNum = FNum + 1
ReDim Preserve MyFiles(1 To FNum)
MyFiles(FNum) = FilesInPath
FilesInPath = Dir()
Loop
'Change ScreenUpdating, Calculation and EnableEvents
With Application
CalcMode = .Calculation
.Calculation = xlCalculationManual
.ScreenUpdating = False
.EnableEvents = False
End With
'Add a new workbook with one sheet
Dim wbk As Workbook
Dim ws As Worksheet
Set wbk = Workbooks("Excel to.xlsm")
Set BaseWks = wbk.Worksheets("Sheet1")
rnum = 1
'Loop through all files in the array(myFiles)
If FNum > 0 Then
For FNum = LBound(MyFiles) To UBound(MyFiles)
Set mybook = Nothing
On Error Resume Next
Set mybook = Workbooks.Open(MyPath & MyFiles(FNum))
On Error GoTo 0
If Not mybook Is Nothing Then
On Error Resume Next
With mybook.Worksheets(1)
Set sourceRange = .Range("A2:H2")
End With
If Err.Number > 0 Then
Err.Clear
Set sourceRange = Nothing
Else
'if SourceRange use all columns then skip this file
If sourceRange.Columns.Count >= BaseWks.Columns.Count Then
Set sourceRange = Nothing
End If
End If
On Error GoTo 0
If Not sourceRange Is Nothing Then
SourceRcount = sourceRange.rows.Count
If rnum + SourceRcount >= BaseWks.rows.Count Then
MsgBox "Sorry there are not enough rows in the sheet"
BaseWks.Columns.AutoFit
mybook.Close savechanges:=False
GoTo ExitTheSub
Else
'Copy the file name in column A
'With sourceRange
'BaseWks.Cells(rnum, "A"). _
'Resize(.rows.Count).Value = MyFiles(FNum)
'End With
'Set the destrange
Set destrange = BaseWks.Range("A2" & rnum)
'we copy the values from the sourceRange to the destrange
With sourceRange
Set destrange = destrange. _
Resize(.rows.Count, .Columns.Count)
End With
destrange.Value = sourceRange.Value
rnum = rnum + SourceRcount
End If
End If
mybook.Close savechanges:=False
End If
Next FNum
BaseWks.Columns.AutoFit
End If
ExitTheSub:
'Restore ScreenUpdating, Calculation and EnableEvents
With Application
.ScreenUpdating = True
.EnableEvents = True
.Calculation = CalcMode
End With
End Sub
1条答案
按热度按时间lh80um4z1#
干脆试试
术语
"A" & rnum
将进行 * 字符串连接 *。当你把rnum设置为1时,结果会是"A1"
。如果将其设置为2,则会给予"A2"
,数据将被复制到第二行。当你写
BaseWks.Range("A2" & rnum)
时,字符串A2
和rnum
的内容将被连接起来,所以在你的例子中,这给出了A21
。