Vba 在合并工作簿上选择范围
我还需要更改要粘贴的合并数据的“目的地”,从单元格第4行开始。我在Microsoft.com中找到的代码(由于下面的答案进行了一些修改)如下Vba 在合并工作簿上选择范围,vba,excel,Vba,Excel,我还需要更改要粘贴的合并数据的“目的地”,从单元格第4行开始。我在Microsoft.com中找到的代码(由于下面的答案进行了一些修改)如下 Sub Button1_Click() Dim MyPath As String, FilesInPath As String Dim MyFiles() As String Dim SourceRcount As Long, FNum As Long Dim mybook As Workbook, BaseWks As W
Sub Button1_Click()
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
' Change this to the path\folder location of your files.
MyPath = "C:\Documents and Settings\laragon2\Desktop\Week's Routers"
' Add a slash at the end of the path if needed.
If Right(MyPath, 1) <> "\" Then
MyPath = MyPath & "\"
End If
' If there are no Excel files in the folder, exit.
FilesInPath = Dir(MyPath & "*.xl*")
If FilesInPath = "" Then
MsgBox "No files found"
Exit Sub
End If
' Fill the myFiles array with the list of Excel files
' in the search folder.
FNum = 0
Do While FilesInPath <> ""
FNum = FNum + 1
ReDim Preserve MyFiles(1 To FNum)
MyFiles(FNum) = FilesInPath
FilesInPath = Dir()
Loop
' Set various application properties.
With Application
CalcMode = .Calculation
.Calculation = xlCalculationManual
.ScreenUpdating = False
.EnableEvents = False
End With
' Add a new workbook with one sheet.
Set BaseWks = ThisWorkbook.Sheets("Routers")
rnum = 1
' Loop through all files in the myFiles array.
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
' Change this range to fit your own needs.
With mybook.Worksheets(1)
Set sourceRange = .Range("A4", .Range("E700").End(xlUp))
End With
If Err.Number > 0 Then
Err.Clear
Set sourceRange = Nothing
Else
' If source range uses 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 "There are not enough rows in the target worksheet."
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 destination range.
Set destrange = BaseWks.Range("b4")
' Copy the values from the source range
' to the destination range.
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 the application properties.
With Application
.ScreenUpdating = True
.EnableEvents = True
.Calculation = CalcMode
End With
End Sub
子按钮1\u单击()
将MyPath设置为字符串,将FileInPath设置为字符串
将MyFiles()设置为字符串
暗源计数等于长,FNum等于长
将mybook设置为工作簿,将BaseWks设置为工作表
变暗源范围作为范围,减小范围作为范围
尺寸与长度相同,计算模式与长度相同
'将此更改为文件的路径\文件夹位置。
MyPath=“C:\Documents and Settings\laragon2\Desktop\Week's Router”
'如果需要,在路径末尾添加斜杠。
如果正确(MyPath,1)“\”则
MyPath=MyPath&“\”
如果结束
'如果文件夹中没有Excel文件,请退出。
FilesInPath=Dir(MyPath&“*.xl*”)
如果FilesInPath=“”,则
MsgBox“未找到任何文件”
出口接头
如果结束
'用Excel文件列表填充myFiles数组
'在搜索文件夹中。
FNum=0
在文件输入路径“”时执行此操作
FNum=FNum+1
ReDim保留我的文件(1到FNum)
MyFiles(FNum)=FilesInPath
FilesInPath=Dir()
环
'设置各种应用程序属性。
应用
CalcMode=.Calculation
.Calculation=xlCalculationManual
.ScreenUpdate=False
.EnableEvents=False
以
'添加带有一张工作表的新工作簿。
Set BaseWks=thishworkbook.Sheets(“路由器”)
rnum=1
'遍历myFiles数组中的所有文件。
如果FNum>0,则
对于FNum=LBound(MyFiles)到UBound(MyFiles)
设置mybook=Nothing
出错时继续下一步
设置mybook=Workbooks.Open(MyPath&MyFiles(FNum))
错误转到0
如果不是的话,我的书什么都不是
出错时继续下一步
'更改此范围以满足您自己的需要。
使用mybook.工作表(1)
设置sourceRange=.Range(“A4”),.Range(“E700”).End(xlUp))
以
如果错误编号>0,则
呃,明白了
设置sourceRange=Nothing
其他的
'如果源范围使用所有列,则
'跳过此文件。
如果sourceRange.Columns.Count>=BaseWks.Columns.Count,则
设置sourceRange=Nothing
如果结束
如果结束
错误转到0
如果不是sourceRange,则为Nothing
SourceRcount=sourceRange.Rows.Count
如果rnum+SourceRcount>=BaseWks.Rows.Count,则
MsgBox“目标工作表中没有足够的行。”
BaseWks.Columns.AutoFit
mybook.Close savechanges:=False
下地狱
其他的
'复制A列中的文件名。
使用sourceRange
基底细胞(rnum,“A”)_
调整大小(.Rows.Count).Value=MyFiles(FNum)
以
'设置目标范围。
设置destrange=BaseWks.Range(“b4”)
'从源范围复制值
'到目标范围。
使用sourceRange
设置减小范围=减小范围_
调整大小(.Rows.Count、.Columns.Count)
以
destrange.Value=sourceRange.Value
rnum=rnum+源计数
如果结束
如果结束
mybook.Close savechanges:=False
如果结束
下一个FNum
BaseWks.Columns.AutoFit
如果结束
退出主题:
'还原应用程序属性。
应用
.ScreenUpdate=True
.EnableEvents=True
.Calculation=CalcMode
以
端接头
在“更改此范围以满足您自己的需要”之后的行中立即设置源范围。看起来如下:
Set sourceRange=.Range(“A1:C1”)
在注释后面的行中设置目的地,该注释指示:“设置目的地范围。
如下所示:
Set destrange=BaseWks.Range(“B”和rnum)
编辑以下是一个示例。创建一个空工作簿。在第1页的A1:A5单元格中输入一些值。政府会这样做:
Sub CopyRangeToRange()
Dim sourceRange As Range
Dim destRange As Range
Set sourceRange = Range("A1:A5")
Set destRange = Sheets(2).Range("A1")
With sourceRange
Set destRange = destRange.Resize( _
.Rows.Count, .Columns.Count)
End With
Sheets(2).Activate
destRange.Activate
destRange.Value = sourceRange.Value
End Sub
这与我上面提出的方法完全相同。如果这是可行的,但正在编写的宏不起作用,则需要调试出错的地方,因为方法是相同的
编辑#2
在你的练习册上试过之后,我想这就是你想要的。我相信我对我的所有更改都发表了评论,您可以在“##
评论中找到。几乎所有应用于With sourcerange
块的更改。我还将rnum
的初始值更改为4,因为这似乎是开始将数据粘贴到路由器工作表中的位置,并修改了循环中每个文件的rnum
增量
Sub Button1_Click()
Dim MyPath As String, FilesInPath As String
Dim MyFiles() As String
Dim SourceRcount As Long, FNum As Long, mybook As Workbook
Dim BaseWks As Worksheet
Dim sourceRange As Range, destrange As Range
Dim rnum As Long, CalcMode As Long
' Change this to the path\folder location of your files.
MyPath = "C:\Documents and Settings\laragon2\Desktop\Week's Routers"
' Add a slash at the end of the path if needed.
If Right(MyPath, 1) <> "\" Then
MyPath = MyPath & "\"
End If
' If there are no Excel files in the folder, exit.
FilesInPath = Dir(MyPath & "*.xl*")
If FilesInPath = "" Then
MsgBox "No files found"
Exit Sub
End If
' Fill the myFiles array with the list of Excel files
' in the search folder.
FNum = 0
Do While FilesInPath <> ""
FNum = FNum + 1
ReDim Preserve MyFiles(1 To FNum)
MyFiles(FNum) = FilesInPath
FilesInPath = Dir()
Loop
' Set various application properties.
With Application
CalcMode = .Calculation
.Calculation = xlCalculationManual
.ScreenUpdating = False
.EnableEvents = False
End With
' Set the destination worksheet:'
Set BaseWks = ThisWorkbook.Sheets("Routers")
'## set rnum to 4 because we begin pasting data in row 4... ##'
rnum = 4
' Loop through all files in the myFiles array.
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
' Change this range to fit your own needs.
With mybook.Worksheets(1)
Set sourceRange = .Range("A4", .Range("E4:E700").End(xlUp)) '## changed dz ##'
End With
If Err.Number > 0 Then
Err.Clear
Set sourceRange = Nothing
Else
' If source range uses 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 "There are not enough rows in the target worksheet."
BaseWks.Columns.AutoFit
mybook.Close savechanges:=False
GoTo ExitTheSub
Else
' Copy the file name in column A.
BaseWks.Activate
With sourceRange
''## changed to make this range the same number of rows as sourceRange ##'
BaseWks.Cells(rnum, 1). _
Resize(.Rows.Count).Value = MyFiles(FNum)
'## moved this code and changed to begin at the last non-blank row in column A, but use column B ##'
'## resize the destrange to the same dimensions as sourcerange ##'
Set destrange = BaseWks.Cells(rnum, 1). _
Resize(.Rows.Count, .Columns.Count).Offset(, 1)
'## Insert the source values in the destination range ##'
destRange.Value = .Value
'## increment rnum to the next appropriate value ##'
rnum = rnum + .Rows.Count
End With
'## Removed as redundant
'With sourceRange
' Set destrange = destrange. _
' Resize(.Rows.Count, .Columns.Count)
'End With
' Copy the values from the source range
' to the destination range.
'## This has been moved to above. ##
' destrange.Value = sourceRange.Value
End If
End If
mybook.Close savechanges:=False
End If
Next FNum
BaseWks.Columns.AutoFit
End If
ExitTheSub:
' Restore the application properties.
With Application
.ScreenUpdating = True
.EnableEvents = True
.Calculation = CalcMode
End With
End Sub
子按钮1\u单击()
将MyPath设置为字符串,将FileInPath设置为字符串
将MyFiles()设置为字符串
Dim Sourcer计数为Long,FNum为Long,mybook为工作簿
将基线设置为工作表
变暗源范围作为范围,减小范围作为范围
尺寸与长度相同,计算模式与长度相同
'将此更改为文件的路径\文件夹位置。
MyPath=“C:\Documents and Settings\laragon2\Desktop\Week's Router”
'如果需要,在路径末尾添加斜杠。
如果正确(MyPath,1)“\”则
MyPath=MyPath&“\”
如果结束
'如果文件夹中没有Excel文件,请退出。
FilesInPath=Dir(MyPath&“*.xl*”)
如果FilesInPath=“”,则
MsgBox“未找到任何文件”