Ok, a bit hard to explain this, hope someone understand...
I'm already using a macro to merge data from different Workbooks in a specific folder (same folder as the excel file using the macro is saved). Look further down for the macro I'm currently using.
The data that is imported to the excel file "holding" the macro is gonna be used as a database. I've tried using different general excel formulas (e.g. Vlookup, SUMIF, SUMIFS), which searching the database built up by using the macro. So far, so good..
The problem occure when I run the macro again, with a new set of data. The formulas result doesn't change. I guess they don't act dynamically.
To be a little more specific about what I'm trying to aim (please look at the attachment file):
In Sheet 2 column 'A' there are different project numbers (e.g. 101323). In Column 'C' (Sheet 2) I would get the total sum from Column 'L' in Sheet 1, based on the following criterium: project number, value in column 'A' in Sheet 2 are > 3000 and < 4000. In the attched example the result would have been 533653.
I know that I can use the SUMIFS function to solv this, but it doesn't seem to work when the data is generated by a macro. And as metioned already - a new set of data imported doesn't effect the formulas outcome. E.g. say the new set of data doesn't contain the project number "101323", the SUMIFS formlua won't return 0, but still 533653.
I should also mentioned that the project numers are dynamic, since they are linked to numbers in Sheet 3, column B (but I guess this would not be a problem).
Do I need a macro to solv this or do I just have to modify the macro I'm already using? Or something else... please help!
Here's the macro I'm using:
Private Declare Function SetCurrentDirectoryA Lib _
"kernel32" (ByVal lpPathName As String) As Long
Sub ChDirNet(szPath As String)
SetCurrentDirectoryA szPath
End Sub
Sub Combine_Workbooks_Select_Files()
Dim MyPath As String
Dim SourceRcount As Long, Fnum As Long
Dim mybook As Workbook, BaseWks As Worksheet
Dim sourceRange As Range, destrange As Range
Dim rnum As Long, CalcMode As Long
Dim SaveDriveDir As String
Dim FName As Variant
With Application
CalcMode = .Calculation
.Calculation = xlCalculationManual
.ScreenUpdating = False
.EnableEvents = False
End With
SaveDriveDir = CurDir
ChDirNet ThisWorkbook.Path & Application.PathSeparator
FName = Application.GetOpenFilename(filefilter:="Excel Files (*.xl*), *.xl*", _
MultiSelect:=True)
If IsArray(FName) Then
Set BaseWks = ActiveWorkbook.Worksheets(1)
rnum = 1
For Fnum = LBound(FName) To UBound(FName)
Set mybook = Nothing
On Error Resume Next
Set mybook = Workbooks.Open(FName(Fnum))
On Error GoTo 0
If Not mybook Is Nothing Then
On Error Resume Next
With mybook.Worksheets(12)
Set sourceRange = .Range("B21:M142")
End With
If Err.Number > 0 Then
Err.Clear
Set sourceRange = Nothing
Else
If sourceRange.Columns.count >= BaseWks.Columns.count Then
Set sourceRange = Nothing
End If
End If
On Error GoTo 0
If Not sourceRange Is Nothing Then
SourceRcount = sourceRange.Rows.count
If rnum + SourceRcount >= BaseWks.Rows.count Then
MsgBox "Not enough rows in the sheet. "
BaseWks.Columns.AutoFit
mybook.Close savechanges:=False
GoTo ExitTheSub
Else
Set destrange = BaseWks.Range("A" & rnum)
With sourceRange
Set destrange = destrange. _
Resize(.Rows.count, .Columns.count)
End With
destrange.Value = sourceRange.Value
rnum = rnum + SourceRcount
End If
End If
mybook.Close savechanges:=False
End If
Next Fnum
BaseWks.Columns.AutoFit
End If
ExitTheSub:
With Application
.ScreenUpdating = True
.EnableEvents = True
.Calculation = CalcMode
End With
ChDirNet SaveDriveDir
End Sub
Bookmarks