Forum Discussion
import data from a master file in to other excel files
Hi, i have a master file that i fill in daily with new data that has 5 different business accounts (column k e.g xtr-, bms- etcetera). This file produces a kpi for what's outstanding. New data is added daily
I then have to copy the relevant data (column A, D-N) )from the master file and paste in to the 5 different business accounts (column k) so to 5 different files. The 5 different business accounts (column x) are not allowed to see each others data hence the 5 different files. I have tried using get data from file etc but it copies all the data whereas I need it to only copy the relevant data and also ignore columns that have formulas can anyone please help me?
I have attached the master file and 1 of the 5 other files xtr
2 Replies
- Bernie DeitrickCopper Contributor
You could create 5 power queries within your master workbook, and then use those resulting tables as the source for a power query into the five other workbooks. The files could be set to autorefresh their queries, so the latest data will always be available.
(You could also go directly from your master tables into the five other workbooks, but that would allow a savvy user to edit the query and possible get more information than you want to allow.)
How about this:
Sub ImportFilteredData() Dim masterWB As Workbook Dim sourceWS As Worksheet Dim targetWB As Workbook Dim targetWS As Worksheet Dim lastRow As Long, copyRow As Long Dim i As Long Dim businessCode As String Dim sourcePath As String Dim targetPath As String ' Set paths and business code sourcePath = "C:\Path\To\MasterFile.xlsx" targetPath = "C:\Path\To\xtrFile.xlsx" businessCode = "xtr-" ' Open Master Workbook Set masterWB = Workbooks.Open(sourcePath) Set sourceWS = masterWB.Sheets("Sheet1") lastRow = sourceWS.Cells(sourceWS.Rows.Count, "K").End(xlUp).Row ' Open Target Workbook Set targetWB = Workbooks.Open(targetPath) Set targetWS = targetWB.Sheets("Sheet1") copyRow = targetWS.Cells(targetWS.Rows.Count, "A").End(xlUp).Row + 1 ' Loop through master data For i = 2 To lastRow ' assuming row 1 has headers If sourceWS.Cells(i, "K").Value = businessCode Then Dim col As Long Dim destCol As Long: destCol = 1 ' Copy columns A and D-N, excluding formulas For col = 1 To sourceWS.Columns.Count If col = 1 Or (col >= 4 And col <= 14) Then If Not sourceWS.Cells(i, col).HasFormula Then targetWS.Cells(copyRow, destCol).Value = sourceWS.Cells(i, col).Value End If destCol = destCol + 1 End If Next col copyRow = copyRow + 1 End If Next i ' Save & Close both workbooks targetWB.Save targetWB.Close masterWB.Close False End Sub