Macro to Create New Workbook and Copy Data at Each Change of X
Closed
greatdanejes
Posts
1
Registration date
Friday 25 August 2017
Status
Member
Last seen
25 August 2017
-
25 Aug 2017 à 15:08
vcoolio Posts 1411 Registration date Thursday 24 July 2014 Status Contributor Last seen 6 September 2024 - 26 Aug 2017 à 06:26
vcoolio Posts 1411 Registration date Thursday 24 July 2014 Status Contributor Last seen 6 September 2024 - 26 Aug 2017 à 06:26
Related:
- Vba create new workbook with name
- Create new sheet vba - Guide
- Vba find - Guide
- Create skype account with gmail - Guide
- Vba case like - Guide
- Create snapchat account with email - Guide
1 response
vcoolio
Posts
1411
Registration date
Thursday 24 July 2014
Status
Contributor
Last seen
6 September 2024
262
26 Aug 2017 à 06:26
26 Aug 2017 à 06:26
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.