Macro to Create New Workbook and Copy Data at Each Change of X
Closed
greatdanejes
Posts
1
Registration date
Friday August 25, 2017
Status
Member
Last seen
August 25, 2017
-
Aug 25, 2017 at 03:08 PM
vcoolio Posts 1411 Registration date Thursday July 24, 2014 Status Moderator Last seen September 6, 2024 - Aug 26, 2017 at 06:26 AM
vcoolio Posts 1411 Registration date Thursday July 24, 2014 Status Moderator Last seen September 6, 2024 - Aug 26, 2017 at 06:26 AM
Related:
- Vba create new workbook with name
- Create snapchat account with email - Guide
- Vba case like - Guide
- Create skype account with gmail - Guide
- Vba create folder if not exist ✓ - Excel Forum
- How to create @ in laptop - Guide
1 response
vcoolio
Posts
1411
Registration date
Thursday July 24, 2014
Status
Moderator
Last seen
September 6, 2024
262
Aug 26, 2017 at 06:26 AM
Aug 26, 2017 at 06:26 AM
Hello GreatDaneJess,
Try the following VBA code:-
The code should create new work books for each patient named in Column D and transfer the relevant rows of data to sheet1 of each work book. Each new work book is named after each patient, surname first. Each new work book is saved to the same folder which holds the originating work book (line 10 in the code above).
Test the code in a copy of your work book first.
I hope that this helps.
Cheerio,
vcoolio.
Try the following VBA code:-
Sub GreatDaneJes()
'Create new work books, transfer data.
Dim dic As Object, rng As Range, ws As Worksheet, mypath As String, lr As Long
Set dic = CreateObject("Scripting.Dictionary")
Set ws = Sheet1
mypath = ThisWorkbook.Path & "\"
lr = ws.Range("A" & Rows.Count).End(xlUp).Row
Application.ScreenUpdating = False
With ws
For nrow = lr To 2 Step -1 '----> Next row
If (Not dic.exists(.Cells(nrow, "D").Value)) Then
dic.Add .Cells(nrow, "D").Value, .Cells(nrow, "D").Value
Set rng = .Range("A1:I" & .Cells(Rows.Count, 1).End(xlUp).Row) '----> Change "I" to whatever is your last column.
rng.AutoFilter 4, Range("D" & nrow).Value
rng.Copy
Workbooks.Add
ActiveSheet.Paste
ActiveSheet.Columns.AutoFit
ActiveWorkbook.SaveAs Filename:=mypath & .Range("D" & nrow).Value & ".xlsx"
ActiveWorkbook.Close
End If
Next
.AutoFilterMode = False
End With
MsgBox "Done!", vbExclamation
Application.CutCopyMode = False
Application.ScreenUpdating = True
End Sub
The code should create new work books for each patient named in Column D and transfer the relevant rows of data to sheet1 of each work book. Each new work book is named after each patient, surname first. Each new work book is saved to the same folder which holds the originating work book (line 10 in the code above).
Test the code in a copy of your work book first.
I hope that this helps.
Cheerio,
vcoolio.