我正在尝试将包含多个工作表的多个工作簿中的数据解析为单个摘要工作表或工作簿。到目前为止,我已经能够从指定的单元格中收集数据,但是我想包括一系列单元格(例如,#34; A2:B20")。如何在循环过程中指定它?
Option Explicit
Sub GetMyData()
Dim myDir As String, fn As String, sn As String, sn2 As String, n As Long, NR As Long
'***** Change Folder Path *****
myDir = "C:\attach"
'***** Change Sheetname(s) *****
sn = "Title"
sn2 = "Monday"
fn = Dir(myDir & "\*.xlsx")
Do While fn <> ""
If fn <> ThisWorkbook.Name Then
With ThisWorkbook.Sheets("Sheet10")
NR = .Cells(Rows.count, 1).End(xlUp).Row + 1
'Pick cells from worksheet "Title"
With .Range("A" & NR)
.Formula = "='" & myDir & "\[" & fn & "]" & sn & "'!B4"
.Value = .Value
End With
With .Range("B" & NR)
.Formula = "='" & myDir & "\[" & fn & "]" & sn & "'!B5"
.Value = .Value
End With
With .Range("C" & NR)
.Formula = "='" & myDir & "\[" & fn & "]" & sn & "'!B6"
.Value = .Value
End With
With .Range("D" & NR)
.Formula = "='" & myDir & "\[" & fn & "]" & sn & "'!B7"
.Value = .Value
End With
With .Range("E" & NR)
.Formula = "='" & myDir & "\[" & fn & "]" & sn & "'!A1"
.Value = .Value
End With
With .Range("F" & NR)
.Formula = "='" & myDir & "\[" & fn & "]" & sn & "'!A2"
.Value = .Value
End With
'pick cells from worksheet "Monday"
With .Range("G" & NR)
.Formula = "='" & myDir & "\[" & fn & "]" & sn2 & Range("A1:C57")
End With
End With
End If
fn = Dir
Loop
ThisWorkbook.Sheets("Sheet10").Columns.AutoFit
End Sub
答案 0 :(得分:0)
你可以做任何一个
Col_1 = "A"
Col_2 = "B"
i = 2
j = 20
Range(Col_1 & i,Col_2 & j)
或
Col_1 = "A"
i = 2
j = 20
Range(Col_1 & i).Resize(j-i+1,2)
希望这有帮助
答案 1 :(得分:0)
有两种方法可以做到这一点,假设你想要一个连续的范围:
Range
函数。例如Range("A3:C10")
Range("A3", "C10")