using VBA copying data from different files in a folder to master sheet - vba

I have the following code on a master sheet when I press a button it goes through the folder that I want and opens and closes the sheets where it should extract data from.
Here is the code I have for opening and closing the files from the button on the master sheet. I need help to write a code for the space ###CODE GOES HERE in the below code. I have been pulling my hair out.
Public Sub test()
Dim wbk As Workbook
Dim Filename As String
Dim Path As String
Dim Wb1 As Workbook, wb2 As Workbook
Path = "\\ttsnas02\user_mdocs$\tdf8273\Documents\Rob\External supplier timesheet\CSV Supplier Main\Inbox folder\" 'CHANGE PATH
Filename = Dir(Path & "*.xl??")
Do While Len(Filename) > 0 'IF NEXT FILE EXISTS THEN
Set wbk = Workbooks.Open(Path & Filename)
'
' ###CODE GOES HERE
'
wbk.Close True
Filename = Dir
Loop
End Sub
Can you please help me write a code in
This code for '###CODE GOES HERE' needs to get the data from the opening sheets rows by checking if there is a value in a specific column. For example, if there is data in L12.Then in the opening sheet, it will copy J8 J9 and L12 in master sheet a2 c2 and e2.
Then in the opening sheet, it checks L13. IF there is value it copies J8 J9 and L13 to a3 c3 and e3.
Then in the opened sheet checks L14...
Until L20
Then the opened workbook is closed and it opens the next workbook in the folder. Checks the same table:if there is data in L12.Then in the opening sheet, it will copy J8 J9 and L12 in master sheet put in the next free row.
This is is supplier timesheet
supplier timesheet
master sheet

This should do what you want. The code is using for both master and opening the first worksheet with index 1. Change the index if needed:
Public Sub test()
Dim wksMaster As Worksheet
Dim wks As Worksheet
Dim rng As Range
Dim i As Integer
Dim wkb As Workbook
Dim Filename As String
Dim Path As String
Dim Wb1 As Workbook, wb2 As Workbook
Path = "\\ttsnas02\user_mdocs$\tdf8273\Documents\Rob\External supplier timesheet\CSV Supplier Main\Inbox folder\" 'CHANGE PATH
Filename = Dir(Path & "*.xl??")
' bind the master worksheet to access it later on
' change index if needed
Set wksMaster = ActiveWorkbook.Worksheets(1) ' or ThisWorkbook
i = 2
Do While Len(Filename) > 0 'IF NEXT FILE EXISTS THEN
Set wkb = Workbooks.Open(Path & Filename)
' loop through range in worksheet with index 1 (the first)
' change index if needed
With wkb.Worksheets(1)
For Each rng In .Range("L12:L20")
' if there is a value in the cell
If rng <> vbNullString Then
wksMaster.Range("A" & i) = .Range("J8")
wksMaster.Range("C" & i) = .Range("J9")
wksMaster.Range("E" & i) = rng
' increment i
i = i + 1
End If
Next
End With
wkb.Close True
Filename = Dir
Loop
End Sub

Related

Run on the master workbook, check if there are any new or deleted worksheets in the daily input workbook

I am trying to figure out how to do this, any guidance would be great.
Setup: I work between 2 workbooks called Master and Daily Input
The Daily Input file contains 10 worksheets, each worksheet = 1 person's name. + 1 worksheet named "Input Template"
The Master workbook contains a bunch of different worksheets for different calculations. + 9 worksheet with the team member's names.
Assume currently there are 9 people in the team.
When new people join or leave the team, they will open / delete worksheets from the Daily Input workbook.
Therefore I want to:
New Team Member added a new worksheet scenario:
If Daily Input have a worksheet that Master does not (except Input Template), then create a new worksheet in Master with the same name. The new worksheet is copied from Output Template that is already in the Master file.
If Master have a worksheet that Daily Input does not (except a few worksheets for calculation), then just prompt a messagebox.
Currently I have written something that extracts all the sheet names from the Daily Input file, and then put it in the Master File but I am not sure how to make use of that...
Maybe load both sheet name lists into an array and compare?
Sub ObtainNameList()
Application.ScreenUpdating = False
Dim WkBk_Input As Workbook
Dim WkBk_Active As Workbook
Dim GetListFName As String
Dim GetListFPath As String
Dim FName As String
Dim FPath As String
Dim i As Integer
Set WkBk_Active = Application.ActiveWorkbook
FPath = WkBk_Active.Worksheets("Menu").Range("B1")
FName = WkBk_Active.Worksheets("Menu").Range("B2")
Set WkBk_Input = Application.Workbooks.Open(FPath & "\" & FName)
WkBk_Active.Worksheets("NameList").Range("A:A").ClearContents
For i = 1 To WkBk_Input.Sheets.Count
WkBk_Active.Worksheets("NameList").Range("A" & i).Value = WkBk_Input.Sheets(i).Name
Next i
WkBk_Input.Close
Application.ScreenUpdating = True
End Sub
This ought to work, but I'm on my phone so can't actually check it:
Sub CheckandCreate()
Dim Fpath As String
Dim Fname As String
Dim master As Workbook
Set master = ThisWorkbook 'assume running in master
Dim daily As Workbook
'set daily path and name here
Set daily = Workbooks.Open(Fpath & "\" & Fname)
Dim ws As Worksheet
For Each ws In daily.Worksheets
Select Case ws.Name
Case "Input Template" 'add ay other sheet names you want to ignore here
Case Else
If SheetNotExist(ws.Name, master) Then
AddSheet (ws.Name)
End If
End Select
Next ws
daily.Close False 'close daily without saving
End Sub
Function SheetNotExist(sheetname As String, where As Workbook) As Boolean
On Error GoTo nope
Dim ws As Worksheet
Set ws = where.Worksheets(sheetname) 'if sheet exists this will work
SheetNotExist = False
Exit Function
nope:
SheetNotExist = True 'will only get here if sheet doesn't exist
End Function
Sub AddSheet(sheetname As String)
Dim ws As Worksheet
ThisWorkbook.Worksheets("Output Template").Copy _ after:=ThisWorkbook.Worksheets(ThisWorkbook.Sheets.Count) 'copy to end of workbook
Set ws = ThisWorkbook.Worksheets(ThisWorkbook.Sheets.Count) 'new worksheet
ws.Name = sheetname
End Sub

Have macro in separate workbook to locate workbook, copy and paste as values from worksheet into new workbook and save in original workbook's location

As pre-warning, I am new to using VBA.
I have scraped together the following code to do the following:
1. Locate the worksheet "Intrastat"
2. Copy the used range of this worksheet
3. Paste as values into a new workbook
4. Reformat dates in column B
5. Save the workbook with in the original workbook's location.
However this only works when I have the macro saved in the original workbook. What I need is to be able to have the macro saved in a different workbook and on running the macro I need to be able to select the "original" workbook from a file location on my HDD.
Any ideas?
The Current Code:
Sub TB_Intrastat_Data_Cleanse()
Dim wb As Workbook
Set wb = Workbooks.Add
Set TWKB = ThisWorkbook
Set sel = Selection
Dim folderPath As String
folderPath = Application.ThisWorkbook.Path
TWKB.Sheets("Intrastat").UsedRange.Copy
wb.Sheets(1).[a1].PasteSpecial xlPasteValues
Columns("B:B").Select
Selection.NumberFormat = "dd/mm/yyyy;#"
nme = "TB Intrastat Data " & Range("A3") & " MTD"
ActiveWorkbook.SaveAs Filename:=folderPath & nme
End Sub
You can use Application.GetOpenFilename() to prompt the user to browse through the file explorer, and open a file. It will then use that file as TWKB:
Sub TB_Intrastat_Data_Cleanse()
Dim wb As Workbook, TWKB As Workbook
Dim sel As Range
Dim nme As String
Set wb = Workbooks.Add
Set TWKB = Application.GetOpenFilename(Title:="Please choose a file to open", FileFilter:="Excel Files *.xls* (*.xls*),")
Set sel = Selection
Dim folderPath As String
folderPath = Application.ThisWorkbook.Path
TWKB.Sheets("Intrastat").UsedRange.Copy
wb.Sheets(1).[a1].PasteSpecial xlPasteValues
Columns("B:B").NumberFormat = "dd/mm/yyyy;#"
nme = "TB Intrastat Data " & Range("A3") & " MTD"
ActiveWorkbook.SaveAs Filename:=folderPath & nme
End Sub
Note: I would change ActiveWorkbook.SaveAs at the end to a workbook variable (or explicitly name the workbook), since you're using two separate ones you want to make sure it's saving the correct one.

Setting wbkDest.worksheets("xxxxxxx") to Reflect a Range

I have a workbook that generates pages/page names. The worksheet name always reflects a job number. The job number is also always displayed in cell A1.
I want to copy data from another book, and paste it back into this worksheet, but I need to target the page I want to paste the data.
My issue is that I can't say wbkDest.Worksheets("sheet1").
I need to say something like wbk.Dest.Worksheets(Range("a1").value).
So again: Paste in the sheet with the same name as cell A1 in the workbook the macro is triggered from.
sub import()
Dim wbkSrc As Workbook, wbkDest As Workbook
Dim myFile As String
Dim Path As String
Dim emptyRow As Long
Dim wsOrig As Worksheet
Set wsOrig = ThisWorkbook.Worksheets(1)
emptyRow = 1
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Set wbkDest = Workbooks("lathe_project6-1-2017.xlsm")
Path = "G:\FIXTURES\" & Range("A1").Value & "\lists\new folder\"
myFile = Dir(Path & "*.xls??")
Set wbkSrc = Workbooks.Open(Path & myFile)
wbkSrc.Worksheets(1).Range("A1:I100").Copy
wbkDest.Worksheets(wsOrig.Range("A1").Value).Cells(emptyRow, 1).PasteSpecial Paste:=xlPasteValues
wbkSrc.Close
End Sub
This doesn't work:?
Dim wskVariableSheet as worksheet
Set wskVariableSheet = wbkDest.Sheets(Range("a1").Value)
And then just reference to wskVariableSheet.range where you want to paste.

VBA: Copy data from another workbook, based on cell value of current workbook

Currently I have a workbook that loops through workbooks in the same folder as it, end copies data from specific cells back to the master workbook. If I want to change to cells from where the code copies cells, I’ll have to change this value in the code.
However, I have co-workers who needs to use this sheet aswell, to collect data from other workbooks. – So I need to make this use friendly.
What I want to do, is to have the code read a cell value in the masterworkbook, and use this cell value as the cell that it copies from.
Example:
If I type “B3” in the masterworkbook cell A1 and run the macro, the macro will copy data from the originsheet B3, into the first cell of the masterworkbook. Does anyone have any idear how to accomplish this?
Or something like:
.Cells(1).Value = originsheet.Range(Range("CellValue from destinationsheet A1").Value).Value
Here is the code I use:
Sub Consolidate()
Dim wkbkorigin As Workbook
Dim originsheet As Worksheet
Dim destsheet As Worksheet
Dim ResultRow As Long
Dim Fname As String
Dim RngDest As Range
Set destsheet = ThisWorkbook.Worksheets("Sheet1")
Set RngDest = destsheet.Cells(Rows.Count, 1).End(xlUp) _
.Offset(1, 0).EntireRow
Fname = Dir(ThisWorkbook.Path & "/*.xlsx")
'loop through each file in folder (excluding this one)
Do While Fname <> "" And Fname <> ThisWorkbook.Name
If Fname <> ThisWorkbook.Name Then
Set wkbkorigin = Workbooks.Open(ThisWorkbook.Path & "/" & Fname)
Set originsheet = wkbkorigin.Worksheets(1)
With RngDest
.Cells(1).Value = originsheet.Range(Range("A1").Value).Value
.Cells(2).Value = originsheet.Range(Range("A2").Value).Value
.Cells(3).Value = originsheet.Range(Range("A3").Value).Value
.Cells(4).Value = originsheet.Range(Range("A4").Value).Value
.Cells(5).Value = originsheet.Range(Range("A5").Value).Value
End With
wkbkorigin.Close SaveChanges:=False 'close current file
Set RngDest = RngDest.Offset(1, 0)
End If
Fname = Dir() 'get next file
Loop
End Sub
And a picture of what i mean, in case im unclear:
enter image description here
I am sorry, but I believe you haven't made yourself clear. However, you may be trying to get something like this (correct me if I'm wrong):
destinationsheet.Range("A1") = originsheet.Range(destinationsheet.Range("A1"))
This will make the value in your Master Workbook be substituted by the content in cell address of the other worksheet.

Copy a template worksheet multiple times in a new workbook with different worksheet names

Trying to complete a VBA routine for the first time.
The goal is :
Use a vertical range of cell that have different names in each cell to create multiples worksheets in one new workbook.
Here's what i got until now :
Sub AddWorksheet()
Dim plage As Range
Dim i As Integer
Dim titre As String
Dim wb As Workbook
Set plage = Range("E6:E24")
Set wb = Workbooks.Add("New Workbook")
For i = 1 To plage.Height
If plage.Cells(i).Value <> "" Then
titre = plage.Cells(i).Value
ActiveWorkbook.Sheets("FeuilleTemplate").Copy After:=wb.Sheets(wb.Sheets.Count)
wb.Sheets(wb.Sheets.Count).Activate
ActiveSheet.Name = titre
End If
Next i
End Sub
Until now the following line is giving me a hard time :
Set wb = Workbooks.add("New Worbook")
The error message is : Error execution '1004' :
The method 'dd' of the object 'Workbooks' has failed.
I'm having a hard time reading and finding the info too for how the methods and class works
I'm use to java.
Thanx for those who gona take time to help me thru this
I think we cannot add a workbook with a specified name as it is not yet saved. So just add workbook do all the operations and in the end save it with the desired name.
Sub AddWorksheet()
Application.DefaultSaveFormat = xlOpenXMLWorkbook
Dim plage As Range
Dim i As Integer
Dim OldBook As Workbook, NewBook As Workbook 'declare both workbooks
Set OldBook = ActiveWorkbook
spath = ThisWorkbook.Path
Set plage = OldBook.Sheets("Sheet Names").Range("E6:E24") 'Assuming that sheet names are in range E6:E24 in "Sheet Names" sheet in old workbook
Set NewBook = Workbooks.Add 'adding new workbook so as to copy the template sheet but this workbook is not saved yet
For i = 1 To plage.Height
If plage.Cells(i).Value <> "" Then 'for each non blank cell in range
OldBook.Sheets("FeuilleTemplate").Copy After:=NewBook.Sheets(NewBook.Sheets.Count) 'Copy "FeuilleTemplate" sheet in workbook after last sheet
NewBook.Sheets("FeuilleTemplate").Name = plage.Cells(i).Value 'Rename the sheet to the desired names from range E6:E24 in "Sheet Names" sheet in old workbook
End If
Next i
With NewBook
.SaveAs Filename:=spath & "\" & "New Workbook with Templates"
.Close SaveChanges:=True
End With
End Sub