update/adapt code already on forum

rjmdc

Well-known Member
Joined
Apr 29, 2020
Messages
672
Office Version
  1. 365
Platform
  1. Windows
i will start my own thread rather than "hog" another thread
mr danteamor already created this macro to create workbooks
it will not overwrite an existing workbook. it will only create new ones
i need to adapt this code that every time its run, it searches for the workbook, if found it will update, if not found it will create
is that possible?
VBA Code:
Sub SplitIntoBooks()
  Dim wb As Workbook, c As Range, ky As Variant
  Dim lr As Long, lc As Long
  
  Application.ScreenUpdating = False
  Application.DisplayAlerts = False
  
  Sheet1.Range("A1").AutoFilter
  lr = Sheet1.Range("F" & Rows.Count).End(3).Row
  lc = Sheet1.Cells(1, Columns.Count).End(1).Column
  With CreateObject("scripting.dictionary")
    For Each c In Sheet1.Range("F2:F" & lr)
      .Item(c.Value) = Empty
    Next
    For Each ky In .Keys
      Sheet1.Range("A1", Sheet1.Cells(lr, lc)).AutoFilter 6, ky
      Set wb = Workbooks.Add(xlWBATWorksheet)
      Sheet1.AutoFilter.Range.Copy
      Range("A1").PasteSpecial xlPasteAll
      wb.SaveAs ThisWorkbook.Path & "\" & ky
      wb.Close False
    Next
  End With
  
  Sheet1.ShowAllData
  Application.ScreenUpdating = True
End Sub
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
if found it will update
How to update?
Overwrite all data?
Paste the data after the last data?

Try this:
VBA Code:
Sub SplitIntoBooks()
  Dim wb As Workbook, c As Range, ky As Variant
  Dim lr As Long, lc As Long
  Dim sName As String
  
  Application.ScreenUpdating = False
  Application.DisplayAlerts = False
  
  Sheet1.Range("A1").AutoFilter
  lr = Sheet1.Range("F" & Rows.Count).End(3).Row
  lc = Sheet1.Cells(1, Columns.Count).End(1).Column
  With CreateObject("scripting.dictionary")
    For Each c In Sheet1.Range("F2:F" & lr)
      .Item(c.Value) = Empty
    Next
    For Each ky In .Keys
      Sheet1.Range("A1", Sheet1.Cells(lr, lc)).AutoFilter 6, ky
      sName = ThisWorkbook.Path & "\" & ky & ".xlsx"
      If Dir(sName) = "" Then
        Set wb = Workbooks.Add(xlWBATWorksheet)
      Else
        Set wb = Workbooks.Open(sName)
      End If
      Sheet1.AutoFilter.Range.Copy
      Range("A" & Rows.Count).End(3)(2).PasteSpecial xlPasteAll
      wb.SaveAs ThisWorkbook.Path & "\" & ky
      wb.Close False
    Next
  End With
  
  Sheet1.ShowAllData
  Application.ScreenUpdating = True
End Sub
 
Upvote 0
IN REPLY:
How to update?
Overwrite all data? YES
Paste the data after the last data? overwrite
 
Upvote 0
how would i label sheet 1 as "All Payments" instead of workbook name which is the split name?
 
Upvote 0
how can i use your code with this one that i am replacing that i also adapted form the forum
VBA Code:
Sub Splitbook()

Dim xPath As String
xPath = "M:\all\FI Payments\Single Participant Payment Worksheets\split file 3\"
Application.ScreenUpdating = False
Application.DisplayAlerts = False
For Each xWs In ThisWorkbook.Sheets
   Select Case xWs.Name
      Case "All Payments"
      Case Else
    xWs.Copy
         Application.ActiveWorkbook.SaveAs Filename:=xPath & "\" & xWs.Name & ".xlsx"
         Application.ActiveWorkbook.Close False
   End Select
Next
Application.DisplayAlerts = True
Application.ScreenUpdating = True

MsgBox "Complete"
End Sub
 
Upvote 0
this seems to have worked amazingly
I'm glad to help you. Thanks for the feedback.

That last one is a new code, so you should create a new thread, there you explain your requirements.
 
Upvote 0

Forum statistics

Threads
1,214,922
Messages
6,122,281
Members
449,075
Latest member
staticfluids

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top