Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

Transfer to master file

0

Hi all i was wondering if anyone can help i have a master workbook that everymorning gets opened and renamed to the relavent day IE, 05 but they than have to transfer the last line of date from the previous day to the newely named workbook and start again this is done every day. 

i have attached a little bit of the workbook as a sample with more detailed infomration of my objective. any help in this would be much appreciated.  

Answer
Discuss

Discussion

Hi Saxon

Firstly you really shouldn't put details of your question in the file (that makes it harder for contributors/ other user to know what you want and is against the Forum rules).

Secondly is it permissible to add a column before Time From/ Time To columns (E and F) in which the date could be added (previous day's or current)?

Thirdly, what about naming the file by (machines and) date e.g "A02A03A04_2023_January_04.xlsm" say? (Easy enough to do in VBA)
John_Ru (rep: 6152) Jan 5, '23 at 5:52 pm
Hello Saxon,
There are several ways to do what you are looking to achieve but will need some clarification on a few things.
In the extra details (in the file) you say each machine has 13 lines but in your sample file each machine has 14 lines. You say the lines to transfer are highlighted in orange but in the file I downloaded nothing is highlighted. Using your sample file as reference, the data to be entered in the first row of the new report would be: A02 - Row5 copied to Row2, A03 - Row19 copied to Row17, and A04 - Row35 copied to Row32; correct? Is there a column which will always have an entry (ex: "E", "G") ?
WillieD24 (rep: 557) Jan 5, '23 at 10:32 pm
John_Ru
notes taken onboard. to add another column is not a problem for the date.
as for naming the file by the machine numbers would be hard this is just a little sample i have 42 machines listed so the sample sheet was to see if it’s possible.

WillieD24

apologies you are correct its 14 lines as for highlighting the things I need to transfer over I highlighted the header column so the data I need to take from the last entry of each machine would be column B,C,D,G,H, & I  

As an example for A02 I would need to send to the master workbook Lines B2 C2 D2 G5 and depending on what’s the last entry in H & I column and all returned to the relevant column/line 2

And if we take A04 I would need to take the last entries B35, C32 , D35, G35 and depending on what’s the last entry in H & I column and all returned to the relevant column/line for that machine 32   I hope this makes sense
Saxon (rep: 4) Jan 6, '23 at 3:07 am
@Willie- I've run out of time today- could you reply to Saxon please?
John_Ru (rep: 6152) Jan 6, '23 at 10:45 am
@John - This one has me stumpped (so far). I have not been able to find code to find the last cell with data in a specific range - which is what I think is needed. EX: for A03 need to find last row with data in "B2:B15", then "C2:C15", and "D2:D15", etc; and then the same for A03, A04, and so on. Everything I've found so far returns 15 - the last row in the range. Can't find code to return 2 - the last row in the range with data. If there is such code, then a Do Loop could be created. I bow to your superior knowledge and skills.
Cheers   :-)
WillieD24 (rep: 557) Jan 6, '23 at 10:56 pm
@Willie I don't know of a last used cell per range but I used an xlSpecialCell just to size a backwards loop. I then looked for the change from blank to data and wrote to an array (/overwrote blank ones) to create a "lattened" data row showing the last entry per column per machine, Not sure I have "superior knowledge and skills" as you suggest (thanks) but I did spent quite a bit of time creating this answer!
John_Ru (rep: 6152) Jan 7, '23 at 11:15 am
Add to Discussion

Answers

0
Selected Answer

Saxon

In the attached file, I've added a macro to "summarise" each block of machine data provided:

  1. The first block starts after row 2
  2. There's a gap between blocks (e.g. your rows 16 and 32)
  3. The last record per machine has a Time in column E at least (note that I formatted columns E and F as Time and corrected values- this is needed later, when the record date is added on summarsing).

The new sheet "Copied sheet" is just for test purposes (copying data back to sheet 1) and should be deleted once you've tested.

There's a green button at the top of Sheet1 (near the top of columns J:L) called "Create new summary file" which is assigned to the sheet macro below - press that and you may be asked to save the file (if there have been changes) and will be asked to provide two dates. The code below is commented so you can (hopefully) follow the logic:

Public Sub CollectLastActivity()

Dim n As Long, LstArray As Variant, fName As String
Dim NwDate As Date, RecDate As Date, r As VbMsgBoxResult

' disable save alerts
Application.DisplayAlerts = False
'check this file has been saved
If ThisWorkbook.Saved = False Then
    r = MsgBox("You must save this file before summarising, okay?", vbYesNo, "File changed since last save...")
    If r = vbNo Then Exit Sub
    ThisWorkbook.Save
End If

' see how save date compares to today
NwDate = InputBox("What date is the NEW summary file needed for? (E.g. tomorrow- please correct)" & vbCr & "(Today is " & Date & ")", "Creating new file...", Date)
If IsDate(NwDate) = False Then Exit Sub
' see when records were from (and check they're not after new date?!
RecDate = InputBox("What date are these records from? (Please correct)", "Creating new file", Date - 1)
If IsDate(RecDate) = False Or RecDate > NwDate Then Exit Sub

Fnd = 0
' find last row per section (based on Start time)
' loop backwards from end of used range
For n = Range("A1").SpecialCells(xlCellTypeLastCell).Row To 2 Step -1
    ' compare the array values, if it exists
    If Not IsEmpty(LstArray) Then
                'compare array values
                For m = 1 To UBound(LstArray, 2)
                ' if a cell is empty, overwrite it
                    If Trim(LstArray(1, m)) = "" Then LstArray(1, m) = Cells(n, m + 1).Value
                Next m
    End If
    ' if no array, check if end of data block
    If Cells(n, 5) <> "" And Fnd = 0 Then
            'collect last line of data block
            LstArray = Range(Cells(n, 2), Cells(n, 9)).Value
            Fnd = n
            Else
                If n = 2 Or (Cells(n, 5) = "" And Fnd > 0) Then
                ' adjust so code below works
                If n = 2 Then n = 1
                ' gap in data so clear data from collected range
                Range(Cells(n + 1, 2), Cells(Fnd, 9)).ClearContents
                ' add user provided record date to last Start and Stop fields
                LstArray(1, 4) = RecDate & " " & CDate(LstArray(1, 4))
                LstArray(1, 5) = RecDate & " " & CDate(LstArray(1, 5))
                ' copy array to row below
                Range(Cells(n + 1, 2), Cells(n + 1, 9)).Value = LstArray
                ' empty the array
                ReDim LstArrray(UBound(LstArray, 1), UBound(LstArray, 2))
                ' reset the found variable
                Fnd = 0
                End If
        End If
Next n
' adjust columns
Columns("A:I").AutoFit

'Define new file name with path
fName = ThisWorkbook.Path & "\" & Format(NwDate, "yyyy_mmm_dd") & ".xlsm"
' save file with new date from user, using 52 (for PC) for macro enabled workboo
ThisWorkbook.SaveAs fName, 52

Application.DisplayAlerts = True
' tell user what happened
MsgBox "Saved! This new file is at " & fName

End Sub

It will "flatten" the data per machine (it should cope with more that 3 machines per file) and save the summary in a new named file (e.g. 2023_Jan_08.xlsm) in the same folder as where the original file is stored.That file can be opened on that date and populated with events. It will have the same button/ code and can be used to create the next day's file etc..

Note that I didn't add a new date column (as I'd suggested) but made the summary Start and Stop entries include the date of the record e.g. 01/06/2023  10:00

I notice that your conditional formnatting rules probably need some rationlisation but I don't have time to do that!

Hope this works well for you.

Discuss

Discussion

I Can not thank you enough for this it will save a lot of time on transfering data from one sheet to another.

yes the conditional formatting needs a littel work i can sort that the work you have done for me here is such a big help. 

once again thanks John_Ru
Saxon (rep: 4) Jan 9, '23 at 6:34 am
Glad that worked. Thanks for selecting my Answer, Saxon
John_Ru (rep: 6152) Jan 9, '23 at 8:43 am
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login