我的代码打开了几个.XLS文件,并使用Active Workbook生成SumIF
函数。
我的代码存在问题,我在打印屏幕上对'Pmax BM
,'Available Declaration
和'Generation Schedule
(列C
D
E
发表评论
我认为这是我的问题TW.Range(Cells(i, 1), Cells(i + 23, 1)=
,因为我使用SumIf的结果设置了整个范围,这就是为什么我在该范围内得到相同的结果。
Dim data As Date
Dim rng As Range
Dim TW As Worksheet: Set TW = ThisWorkbook.Worksheets("Centralizator Lunar")
i = 2
j = 20
StrFile = ThisWorkbook.Path
StrFile = Dir(StrFile & "\*INT2*")
Do While Len(StrFile) > 0
TW_P = ThisWorkbook.Path
myfile = TW_P & "\" & StrFile
Set AWb = Workbooks.Open(myfile)
data = Left(ActiveWorkbook.Name, 10)
nr_AWb = ActiveWorkbook.Worksheets("Sheet1").UsedRange.Rows.Count
'Data
TW.Range(Cells(i, 1), Cells(i + 23, 1)) = data
'Pmax BM
TW.Range(Cells(i, 3), Cells(i + 23, 3)) = WorksheetFunction.SumIf(AWb.Worksheets("Sheet1").Range("D20:D" & nr_AWb), _
AWb.Worksheets("Sheet1").Cells(j, 4), _
AWb.Worksheets("Sheet1").Range("N20:N" & nr_AWb))
'Available Declaration
TW.Range(Cells(i, 4), Cells(i + 23, 4)) = WorksheetFunction.SumIf(AWb.Worksheets("Sheet1").Range("D20:D" & nr_AWb), _
AWb.Worksheets("Sheet1").Cells(j, 4), _
AWb.Worksheets("Sheet1").Range("I20:I" & nr_AWb))
'Generation Schedule
TW.Range(Cells(i, 5), Cells(i + 23, 5)) = WorksheetFunction.SumIf(AWb.Worksheets("Sheet1").Range("D20:D" & nr_AWb), _
AWb.Worksheets("Sheet1").Cells(j, 4), _
AWb.Worksheets("Sheet1").Range("H20:H" & nr_AWb))
i = i + 24
j = j + 1
StrFile = Dir
ActiveWindow.Close SaveChanges:=False
Loop
答案 0 :(得分:1)
我不明白您的要求,但对您的代码进行了一些更改,可能会解决您的问题(添加对范围的工作表引用):
Dim data As Date
Dim rng As Range
Dim TW As Worksheet: Set TW = ThisWorkbook.Worksheets("Centralizator Lunar")
i = 2
j = 20
StrFile = ThisWorkbook.Path
StrFile = Dir(StrFile & "\*INT2*")
Do While Len(StrFile) > 0
TW_P = ThisWorkbook.Path
myfile = TW_P & "\" & StrFile
Set AWb = Workbooks.Open(myfile)
data = Left(ActiveWorkbook.Name, 10)
nr_AWb = ActiveWorkbook.Worksheets("Sheet1").UsedRange.Rows.Count
'Data
TW.Range(TW.Cells(i, 1), TW.Cells(i + 23, 1)) = data
'Pmax BM
TW.Range(TW.Cells(i, 3), TW.Cells(i + 23, 3)) = WorksheetFunction.SumIf(AWb.Worksheets("Sheet1").Range("D20:D" & nr_AWb), _
AWb.Worksheets("Sheet1").Cells(j, 4), _
AWb.Worksheets("Sheet1").Range("N20:N" & nr_AWb))
'Available Declaration
TW.Range(TW.Cells(i, 4), TW.Cells(i + 23, 4)) = WorksheetFunction.SumIf(AWb.Worksheets("Sheet1").Range("D20:D" & nr_AWb), _
AWb.Worksheets("Sheet1").Cells(j, 4), _
AWb.Worksheets("Sheet1").Range("I20:I" & nr_AWb))
'Generation Schedule
TW.Range(TW.Cells(i, 5), TW.Cells(i + 23, 5)) = WorksheetFunction.SumIf(AWb.Worksheets("Sheet1").Range("D20:D" & nr_AWb), _
AWb.Worksheets("Sheet1").Cells(j, 4), _
AWb.Worksheets("Sheet1").Range("H20:H" & nr_AWb))
i = i + 24
j = j + 1
StrFile = Dir
ActiveWindow.Close SaveChanges:=False
Loop