简体   繁体   中英

SUMIF formula doesn't give the correct answer in excel vba

I'm using this SUMIF formula but it gives me a wrong output. Can anybody help me? I have checked a lot of times but unable to find the mistake! Thanks in advance!

Range("B10", Range("B10").Offset(0, -1).End(xlDown).Offset(0, 1)).Formula = _
"=SUMIF('[" & Dest_name & "]Cu Part PVO L'!$M$10:$M$2000,A10," & _
"'[" & Dest_name & "]Cu Part PVO L'!$AD$10:$AD$2000)"
[Output][1]

To clarify my VBA only approach here's a rough out:

Dim i as Long
DIM wb as Workbook
SET wb = Applications.Workbooks.Open(Dest_Name) 'Dest_name would need to be the full file
    'path, i.e. C:\<Folder 1>\<Folder 2>\....\<File Name>.<file extension>
Dim CPPL as Worksheet
Set CPPL = Workbooks(Dest_Name).Worksheets("Cu Part PVO L")
Dim v as String 'change the type based on what's in A10
v = Range("A10").Value
Dim t as long

i = 10
Do While i < 2001
    If CPPL.Range("M" & i).value = v THen
        t = t + CPPL.Range("AD" & i).value
    End if
    i = i + 1
Loop

Range("B10", Range("B10").Offset(0, -1).End(xlDown).Offset(0, 1)).Value = t

As an added bonus, this way, if Dest_Name isn't open the values will still display instead of erroring.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM