Capture data from close workbook

motabrasil

New Member
Joined
May 11, 2010
Messages
28
Dear Sirs,

I need to create a code to capture information from a DATABASE.file (closed Workbook) and Sum or Count, based on a several criterias, in the Dashboard.file (Active Workbook). See described below the initial Code.
How to count the numbers of non blanks rows from the DATABASE.file
column "C" for instance (see in the code highlighted in RED)?
When I run this code, it's capturing the last row from the active workbook.


Sub CaptureInfo()<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:eek:ffice:eek:ffice" /><o:p></o:p>
Dim Results(0 To 1) As Long<o:p></o:p>
Dim LastRow As Long<o:p></o:p>
Dim I As Integer<o:p></o:p>
Dim FilePath$<o:p></o:p>
<o:p></o:p>
<o:p></o:p>
Const Filename$ = "DataBase.xls"<o:p></o:p>
Const SheetName$ = "Sheet1"<o:p></o:p>
FilePath = ActiveWorkbook.Path & "\"<o:p></o:p>
<o:p></o:p>
DoEvents<o:p></o:p>
Application.ScreenUpdating = False<o:p></o:p>
If Dir(FilePath & Filename) = Empty Then<o:p></o:p>
MsgBox "The file " & Filename & " was not found", , "File Doesn't Exist"<o:p></o:p>
Exit Sub<o:p></o:p>
End If<o:p></o:p>
<o:p></o:p>
Application.Calculation = xlManual<o:p></o:p>
<o:p></o:p>
LastRow = 1<o:p></o:p>
<o:p></o:p>
Do While Cells(LastRow, "C") <> ""<o:p></o:p>
LastRow = LastRow + 1<o:p></o:p>
Loop<o:p></o:p>
<o:p></o:p>
LastRow = LastRow - 1<o:p></o:p>
<o:p></o:p>
For I = 1 To LastRow<o:p></o:p>
If Cells(I, "F").Value < 0 Then<o:p></o:p>
If UCase(Cells(I, "AP").Value = "Vendor1") Then<o:p></o:p>
Cells(I, "F").Value = Cells(I, "F").Value<o:p></o:p>
Results(0) = Results(0) + Cells(I, "F").Value<o:p></o:p>
End If<o:p></o:p>
End If<o:p></o:p>
If Cells(I, "F").Value < 0 Then<o:p></o:p>
If UCase(Cells(I, "AP").Value = "Vendor2") Then<o:p></o:p>
Cells(I, "F").Value = Cells(I, "F").Value<o:p></o:p>
Results(1) = Results(1) + Cells(I, "F").Value<o:p></o:p>
End If<o:p></o:p>
End If<o:p></o:p>
<o:p></o:p>
Next I<o:p></o:p>
<o:p></o:p>
Sheets("Dashboard").Select<o:p></o:p>
<o:p></o:p>
Range("E6") = Results(0)<o:p></o:p>
Range("E7") = Results(1)<o:p></o:p>
<o:p></o:p>
<o:p></o:p>
Application.Calculation = xlAutomatic<o:p></o:p>
Application.ScreenUpdating = True<o:p></o:p>
<o:p></o:p>
MsgBox "Done!!!"<o:p></o:p>
<o:p></o:p>
End Sub



Thanks in advance
Mota
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
Any help???
I just want to know where I'm doing wrong when I try to find out the last row from the close workbook. See code described.

Thanks
 
Upvote 0
Why not just open the closed workbook, get your data and then close it?

There are ways to get data from closed workbooks but they would generally involve reference that workbook somehow.

In the code you've posted I don't really see any reference to it apart from when you check for it's existence.

Am I missing something?:)
 
Upvote 0
Hi Norie

Actually, I believe to save time when I keep the workbook closed.
First, I check if the file exist or not and then I need to find out the last row from this (Closed) workbook. However the code is set-up to count the active (open workbook) instead of count from the closed one.
My question is: how to fix that?

That's the part I check the file:

If Dir(FilePath & Filename) = Empty Then<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:eek:ffice:eek:ffice" /><o:p></o:p>
MsgBox "The file " & Filename & " was not found", , "File Doesn't Exist"<o:p></o:p>

Exit Sub<o:p></o:p>
End If

Thank you

<o:p></o:p>
 
Upvote 0
This works but it opens the DataBase file.

Code:
Sub CaptureInfo()

    Dim wbData As Workbook
    Dim wsDash As Worksheet
    Dim sFile$
    
    Set wsDash = ActiveWorkbook.Sheets("Dashboard")
    sFile = ActiveWorkbook.Path & "\" & "DataBase.xls"
    
    If Dir(sFile) = Empty Then
        MsgBox "File: " & sFile & vbCr & "...was not found", , "File Doesn't Exist"
        Exit Sub
    Else
        Application.ScreenUpdating = False
        Set wbData = Application.Workbooks.Open(sFile)
    End If
        
    With wbData.Sheets("Sheet1")

        'LastRow = .Cells(Rows.Count, "C").End(xlUp).Row    'Not needed
        wsDash.Range("E6") = Application.SumIf(.Columns("AP"), "Vendor1", .Columns("F"))
        wsDash.Range("E7") = Application.SumIf(.Columns("AP"), "Vendor2", .Columns("F"))
        
    End With
    
    wbData.Close SaveChanges:=False
    'wsDash.Select
    Application.ScreenUpdating = True
    
    MsgBox "Done!!!"
   
End Sub
 
Upvote 0
What makes you think it will save time not opening the workbook?
 
Upvote 0
Hi Alpha,

Your suggestion seems to be good. My doubts is concerning the SUMIF condition. If you see in my previous code we have two conditions to sum. First, the column "AP" must match with the vendor name and the second one is the column "F" must be less than 0. How to fix that?

Thank you
 
Upvote 0
Hey Norie

On my mind, working with closed workbook would be the same that working in background.

Thank you!
 
Upvote 0
What version of Excel do you have?


If you have Excel 2007, use this...
Code:
wsDash.Range("E6") = Application.SumIfs(.Columns("F"), .Columns("F"), "<0", .Columns("AP"), "Vendor1")
wsDash.Range("E7") = Application.SumIfs(.Columns("F"), .Columns("F"), "<0", .Columns("AP"), "Vendor2")


If you have Excel 2003, use this...
Code:
        strF = "[Database.xls]Sheet1!$F$1:$F$" & .Cells(Rows.Count, "C").End(xlUp).Row
        strAP = Replace(strF, "$F$1:$F$", "$AP$1:$AP$")
        wsDash.Range("E6") = Evaluate("SUMPRODUCT((" & strF & "<0)*(" & strAP & "= ""Vendor1"") * (" & strF & "))")
        wsDash.Range("E7") = Evaluate("SUMPRODUCT((" & strF & "<0)*(" & strAP & "= ""Vendor2"") * (" & strF & "))")
 
Upvote 0

Forum statistics

Threads
1,215,635
Messages
6,125,945
Members
449,275
Latest member
jacob_mcbride

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top