VBA code allowing user to enter/type cell number/range in masterfile from multiple different project files

patrickpperron

New Member
Joined
Aug 8, 2014
Messages
4
Hello,

To start off, I am brand new to excel VBA, and I'm one of those who is learning from the Dummies handbook.

OVERVIEW:
My boss wants me to write a VBA code that essentially takes cells/ranges of cells' values from multiple project files and inputs them in a masterfile summary table. The project "sub"-files are located in a projects folder, while the masterfile is outside the folder. The project files are labeled as: Prio1.xlsx, Prio2.xlsx.....

This exemplary summary table should make things clearer:

[TABLE="width: 500"]
<tbody>[TR]
[TD]Project[/TD]
[TD]Distance[/TD]
[TD]Diameter[/TD]
[TD]Thickness[/TD]
[/TR]
[TR]
[TD]Prio001.xlsx[/TD]
[TD]700[/TD]
[TD]50[/TD]
[TD]12[/TD]
[/TR]
[TR]
[TD]Prio002.xlsx[/TD]
[TD]800[/TD]
[TD]60[/TD]
[TD]51[/TD]
[/TR]
[TR]
[TD]Prio003.xlsx[/TD]
[TD]600[/TD]
[TD]45[/TD]
[TD]1000[/TD]
[/TR]
</tbody>[/TABLE]


I have done this successfully, but he wants to take things further and be able to pick which cell Prio* file he wants by typing the cell number (Ex: "A2") of the subfile into a table next to the masterfile summary table. He wants to be able to modify the cell number/label under "Cells of interest" when he wants, so that the values from the Prio* files that appear in the mastefile get adjusted to the appropriate project subfile cell when applying the macro. This adjacent table would look like this:

[TABLE="width: 500"]
<tbody>[TR]
[TD]Column[/TD]
[TD]Cells of interest[/TD]
[/TR]
[TR]
[TD]Distance[/TD]
[TD]$A$2[/TD]
[/TR]
[TR]
[TD]Diameter[/TD]
[TD]$B$2[/TD]
[/TR]
[TR]
[TD]Thickness[/TD]
[TD]$D$2[/TD]
[/TR]
</tbody>[/TABLE]

Again, so far, the summary table has been created and automated, using the code below. But I want to know how to get the second table to work (on the same mastefile worksheet) using a vba code. What should I add/modify/replace for my current code?

CURRENT CODE:

Option Explicit
Sub Automatic_Properties()
Dim MyPath As String, FilesInPath As String
Dim MyFiles() 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

Range("A2:A700000").Clear
Range("B2:B700000").Clear
Range("C2:C700000").Clear
Range("D2:D700000").Clear

'Fill in the path\folder where the files are
MyPath = "D:\patrick\projects"


'Add a slash at the end if the user forget it
If Right(MyPath, 1) <> "\" Then
MyPath = MyPath & "\"
End If


'If there are no Excel files in the folder exit the sub
FilesInPath = Dir(MyPath & "Prio*.xl*")
If FilesInPath = "" Then
MsgBox "No files found"
Exit Sub
End If


'Fill the array(myFiles)with the list of Excel files in the folder
Fnum = 0
Do While FilesInPath <> ""
Fnum = Fnum + 1
ReDim Preserve MyFiles(1 To Fnum)
MyFiles(Fnum) = FilesInPath
FilesInPath = Dir()
Loop


'Change ScreenUpdating, Calculation and EnableEvents
With Application
CalcMode = .Calculation
.Calculation = xlCalculationManual
.ScreenUpdating = False
.EnableEvents = False
End With


'Master workbook
Set BaseWks = Workbooks("master.xlsm").Worksheets(1)
rnum = 2


'Loop through all files in the array(myFiles)
If Fnum > 0 Then
For Fnum = LBound(MyFiles) To UBound(MyFiles)
Set mybook = Nothing
On Error Resume Next
Set mybook = Workbooks.Open(MyPath & MyFiles(Fnum))
On Error GoTo 0


If Not mybook Is Nothing Then


On Error Resume Next






With mybook.Worksheets(1)
Set sourceRange = .Range("A2:C2")
End With


If Err.Number > 0 Then
Err.Clear
Set sourceRange = Nothing
Else
'if SourceRange use all columns then skip this file
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 "Sorry there are not enough rows in the sheet"
BaseWks.Columns.AutoFit
mybook.Close savechanges:=False
GoTo ExitTheSub
Else


'Copy the file name in column A
With sourceRange
BaseWks.Cells(rnum, "A"). _
Resize(.Rows.Count).Value = MyFiles(Fnum)
End With


'Set the destrange
Set destrange = BaseWks.Range("B" & rnum)


'we copy the values from the sourceRange to the destrange
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:
'Restore ScreenUpdating, Calculation and EnableEvents
With Application
.ScreenUpdating = True
.EnableEvents = True
.Calculation = CalcMode
End With
End Sub


Thank you!

Patrick Pomerleau-Perron
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.

Forum statistics

Threads
1,223,231
Messages
6,170,885
Members
452,364
Latest member
springate

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