BOB
BOB

Reputation: 700

VBA Loop WorksheetFunction.SumIf on Active Workbook

My code opens several .XLS files and makes an SumIF function with Active Workbook. My code have an issue where I comment 'Pmax BM, 'Available Declaration and 'Generation Schedule (Column C D E from print screen)

I think here is my problem TW.Range(Cells(i, 1), Cells(i + 23, 1)= because I set an entire range with the results of SumIf, and that's why i have the same results in that range.`

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

Upvotes: 1

Views: 363

Answers (1)

SJR
SJR

Reputation: 23081

I don't understand what you are asking, but have made some changes to your code which may address your problem (adding sheet references to ranges):

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

Upvotes: 1

Related Questions