Hello MrExcel Team,
I was messing a little bit with many threats here and I cannot found a proper solution. Bellow the code and the explanation:
I open several excel sheets with the very same layout in order to consolidate especific cells. The problem is that the files selected are not accesible and no info is capture to consolidate. The quote for the consolidation is created but not in the proper way. I have no idea how to solve it. Any hint would be very welcome. Thanks!!!
Private Declare Function SetCurrentDirectoryA Lib "kernel32" (ByVal lpPathName As String) As Long
Sub ChDirNet(szPath As String)
SetCurrentDirectoryA szPath
End Sub
Sub ConsolidateRegions()
Dim MyPath As String
Dim Fnum As Long
Dim rnum As Long, CalcMode As Long
Dim SaveDriveDir As String
Dim sFile As Variant
Dim i As Long
Dim SheetArg() As String
Dim sPath1 As String
Dim sPath As String
Dim SelectedFiles() As Variant
Dim FileName As String
Dim FileName2 As String
Dim NFile As Long
Dim LengthPath As Integer
Dim LengthName As Integer
Dim Rest As Integer
'With Application
' CalcMode = .Calculation
' .Calculation = xlCalculationManual
' .ScreenUpdating = False
' .EnableEvents = False
'End With
SaveDriveDir = CurDir
ChDirNet "C:\Users\XXX\Documents\Task\"
'Select files
SelectedFiles = Application.GetOpenFilename(<wbr>filefilter:="Excel Files (*.xl*), *.xl*", MultiSelect:=True)
i = 0
sPath = "C:\Users\XXX\Documents\Task\"
'Create dynamic vector
ReDim SheetArg(1 To 1)
‘ReDim SheetArg2(1 To 1)
'Populate the vector the the taylor made strings by filtering the file name each time
For NFile = LBound(SelectedFiles) To UBound(SelectedFiles)
i = i + 1
FileName = SelectedFiles(NFile)
LengthPath = Len(sPath)
LengthName = Len(FileName)
Rest = LengthName - LengthPath
FileName = Right(FileName, Rest - 8)
ReDim Preserve SheetArg(1 To i)
'ReDim Preserve SheetArg2(1 To i)
'MsgBox sPath, vbInformation
SheetArg(i) = Chr(34) & Chr(39) & "C:\Users\XXX\Documents\Task\" & "[" & FileName & "]Results" & Chr(39) & "!R7C10:R19C11" & Chr(34)
'SheetArg(i) = "'" & sPath & "[" & FileName & "]Results'!R7C10:R19C11"
'SheetArg2(i) = "'" & sPath & "[" & FileName & "]Results'!R7C17:R28C18"
Next NFile
'MsgBox SheetArg(1), vbInformation
'MsgBox SheetArg(2), vbInformation
'Execute the consolidate function. Here the array is not working
Sheets("Divisions").Range("C7"<wbr>).Consolidate Sources:=Array(SheetArg()), Function:=xlSum, TopRow:=False, LeftColumn:=False, CreateLinks:=False
'Sheets("Regions").Range("C7")<wbr>.Consolidate Sources:=Array(SheetArg2), Function:=xlSum, TopRow:=False, LeftColumn:=False, CreateLinks:=False
ChDirNet SaveDriveDir
End Sub
I was messing a little bit with many threats here and I cannot found a proper solution. Bellow the code and the explanation:
I open several excel sheets with the very same layout in order to consolidate especific cells. The problem is that the files selected are not accesible and no info is capture to consolidate. The quote for the consolidation is created but not in the proper way. I have no idea how to solve it. Any hint would be very welcome. Thanks!!!
Private Declare Function SetCurrentDirectoryA Lib "kernel32" (ByVal lpPathName As String) As Long
Sub ChDirNet(szPath As String)
SetCurrentDirectoryA szPath
End Sub
Sub ConsolidateRegions()
Dim MyPath As String
Dim Fnum As Long
Dim rnum As Long, CalcMode As Long
Dim SaveDriveDir As String
Dim sFile As Variant
Dim i As Long
Dim SheetArg() As String
Dim sPath1 As String
Dim sPath As String
Dim SelectedFiles() As Variant
Dim FileName As String
Dim FileName2 As String
Dim NFile As Long
Dim LengthPath As Integer
Dim LengthName As Integer
Dim Rest As Integer
'With Application
' CalcMode = .Calculation
' .Calculation = xlCalculationManual
' .ScreenUpdating = False
' .EnableEvents = False
'End With
SaveDriveDir = CurDir
ChDirNet "C:\Users\XXX\Documents\Task\"
'Select files
SelectedFiles = Application.GetOpenFilename(<wbr>filefilter:="Excel Files (*.xl*), *.xl*", MultiSelect:=True)
i = 0
sPath = "C:\Users\XXX\Documents\Task\"
'Create dynamic vector
ReDim SheetArg(1 To 1)
‘ReDim SheetArg2(1 To 1)
'Populate the vector the the taylor made strings by filtering the file name each time
For NFile = LBound(SelectedFiles) To UBound(SelectedFiles)
i = i + 1
FileName = SelectedFiles(NFile)
LengthPath = Len(sPath)
LengthName = Len(FileName)
Rest = LengthName - LengthPath
FileName = Right(FileName, Rest - 8)
ReDim Preserve SheetArg(1 To i)
'ReDim Preserve SheetArg2(1 To i)
'MsgBox sPath, vbInformation
SheetArg(i) = Chr(34) & Chr(39) & "C:\Users\XXX\Documents\Task\" & "[" & FileName & "]Results" & Chr(39) & "!R7C10:R19C11" & Chr(34)
'SheetArg(i) = "'" & sPath & "[" & FileName & "]Results'!R7C10:R19C11"
'SheetArg2(i) = "'" & sPath & "[" & FileName & "]Results'!R7C17:R28C18"
Next NFile
'MsgBox SheetArg(1), vbInformation
'MsgBox SheetArg(2), vbInformation
'Execute the consolidate function. Here the array is not working
Sheets("Divisions").Range("C7"<wbr>).Consolidate Sources:=Array(SheetArg()), Function:=xlSum, TopRow:=False, LeftColumn:=False, CreateLinks:=False
'Sheets("Regions").Range("C7")<wbr>.Consolidate Sources:=Array(SheetArg2), Function:=xlSum, TopRow:=False, LeftColumn:=False, CreateLinks:=False
ChDirNet SaveDriveDir
End Sub