
Originally Posted by
Norie
The SplitFiles sub is setup to work on the workbook the code is in, ThisWorkbook, not the workbook that's been opened, wbk.
Try passing wbk to SplitFiles as an argument.
That might look something like this, though I can't be sure as you've not posted the header for SplitFiles.
Option Explicit
Sub SplitFiles(wbk1 As Workbook)
'Sub excelsplit()
Dim sht As Worksheet
Dim MyPath As String
Dim l_str As Long, l_end As Long, l_row As Long
'to remove unwanted worksheets on the workbook
Application.DisplayAlerts = False
Do Until wbk1.Sheets.Count = 1
wbk1.Sheets(wbk1.Sheets.Count).Delete
Loop
Application.DisplayAlerts = True
'to read the data from first sheet
l_str = 2
l_row = 2
Do While l_row <= wbk1.Sheets(1).Range("A65536").End(xlUp).Row + 1
If wbk1.Sheets(1).Range("A" & l_row).Value = "" And _
wbk1.Sheets(1).Range("B" & l_row).Value = "" And _
wbk1.Sheets(1).Range("C" & l_row).Value = "" Then
wbk1.Sheets.Add After:=wbk1.Sheets(wbk1.Sheets.Count)
wbk1.Sheets(wbk1.Sheets.Count).Range("A2:Z" & l_row - l_str + 1).Value = wbk1.Sheets(1).Range("A" & l_str & ":Z" & l_row).Value
l_str = l_row + 1
End If
l_row = l_row + 1
Loop
'End Sub
'Sub DeleteNoData
For Each sht In wbk1.Sheets
If Not sht.UsedRange.Find("NO DATA", , , 1) Is Nothing Then
Application.DisplayAlerts = False
sht.Delete
Application.DisplayAlerts = True
End If
Next
'End Sub
'Sub RenameTabs()
Dim l As Long
For l = 1 To wbk1.Sheets.Count
With wbk1.Worksheets(l)
If .Range("B8").Value <> "" And _
.Range("B9").Value <> "" And _
.Range("B10").Value <> "" Then
.Name = "DMO_" & Right(.Range("B10").Value, 5)
End If
End With
Next l
'End Sub
'Sub Splitbook()
'Split separate workbook into separate spreadsheet.
MyPath = wbk1.Path
For Each sht In wbk1.Sheets
sht.Copy
ActiveSheet.Cells.Copy
ActiveSheet.Cells.PasteSpecial Paste:=xlPasteValues
ActiveSheet.Cells.PasteSpecial Paste:=xlPasteFormats
ActiveWorkbook.SaveAs _
Filename:=MyPath & "\" & sht.Name & ".xls"
ActiveWorkbook.Close savechanges:=False
Next sht
'End Sub
End Sub
You would now call SplitFiles from Split like this.
Sorry, I did not totally paste the whole format macro, these doesn't work after I change the sub to wbk1:
Columns("A:A").Select
Selection.TextToColumns Destination:=Range("A1"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
Semicolon:=False, Comma:=True, Space:=False, Other:=False, FieldInfo _
:=Array(Array(1, 1), Array(2, 1)), TrailingMinusNumbers:=True
'Sub DeleteRows()
Set SrchRngFr = ActiveSheet.UsedRange
Do
Set a = SrchRngFr.Find("Type:", LookIn:=xlValues)
If Not a Is Nothing Then a.EntireRow.Delete
Loop While Not a Is Nothing
'End Sub
'Sub DeleteRows()
Set SrchRngEn = ActiveSheet.UsedRange
Do
Set b = SrchRngEn.Find("Plan:", LookIn:=xlValues)
If Not b Is Nothing Then b.EntireRow.Delete
Loop While Not b Is Nothing
'End Sub
Bookmarks