Hi to all.
I'm facing a strange issue.
I have an excel and i use the code in the bottom to create some other excel files using copy and paste. (The first copy is for the headers and the second is for the data)
The problem is: If i open any of the file i had create excel freeze and i had to make end process to close the excel.
Can anyone help me with this problem??
Thanks and regards.
PS1:If i make copy paste only the headers the new excel's close without problem
PS2:If i make copy and paste special the data the new excel's close without problem
This is the code i use.
I'm facing a strange issue.
I have an excel and i use the code in the bottom to create some other excel files using copy and paste. (The first copy is for the headers and the second is for the data)
The problem is: If i open any of the file i had create excel freeze and i had to make end process to close the excel.
Can anyone help me with this problem??
Thanks and regards.
PS1:If i make copy paste only the headers the new excel's close without problem
PS2:If i make copy and paste special the data the new excel's close without problem
This is the code i use.
Code:
Sub Split()
Dim lRow As Integer
Dim lCol As Integer
Dim First_Range_Line As Integer
Dim Last_Range_Line As Integer
Dim Compare_Data As String
Dim New_Excel_Column_Name1 As String
Dim New_Excel_Column_Name2 As String
Dim New_Excel_Name As String
Dim PathForSave As String
Dim Change_Column As String
Dim Current_Workbook_Name As String
Dim New_Workbook_Name As String
Dim SumiRow As Integer
Current_Workbook_Name = ActiveWorkbook.Name
First_Range_Line = Worksheets("Parm").Cells(2, 2).Value + 1 '4
New_Excel_Column_Name1 = Worksheets("Parm").Cells(5, 2).Value 'A
New_Excel_Column_Name2 = Worksheets("Parm").Cells(5, 3).Value 'B
Change_Column = Worksheets("Parm").Cells(3, 2).Value 'A
New_Workbook_Name = Worksheets("Parm").Cells(4, 2).Value 'Data
Worksheets("DataAll").Activate
PathForSave = Application.ActiveWorkbook.Path & "\"
Compare_Data = ActiveSheet.Cells(First_Range_Line, Change_Column).Value
lRow = ActiveSheet.Range(Change_Column & Rows.Count).End(xlUp).Row 'Last Row
lCol = ActiveSheet.Cells(2, Columns.Count).End(xlToLeft).Column 'Last Column
For i = 5 To lRow + 1
If ActiveSheet.Cells(i, Change_Column).Value <> Compare_Data Then
New_Excel_Name = ActiveSheet.Cells(i - 1, New_Excel_Column_Name1).Value & "-" & ActiveSheet.Cells(i - 1, New_Excel_Column_Name2).Value
Copy_Header
ActiveSheet.Range(ActiveSheet.Cells(1, 1), ActiveSheet.Cells(3, lCol)).Select
Selection.Copy
'Create New Excel with Name
Workbooks.Add
ActiveSheet.Name = New_Workbook_Name
Paste_Header
ActiveSheet.Paste
ActiveWorkbook.SaveAs PathForSave & New_Excel_Name
Workbooks(Current_Workbook_Name).Activate
Worksheets("DataAll").Activate
Last_Range_Line = i - 1
'Copy_Data
ActiveSheet.Range(ActiveSheet.Cells(First_Range_Line, 1), ActiveSheet.Cells(Last_Range_Line, lCol)).Select
Selection.Copy
'Paste_Data
Workbooks(New_Excel_Name & ".xls").Activate '------------------
Worksheets(New_Workbook_Name).Activate
ActiveSheet.Cells(4, 1).Select
ActiveSheet.Paste
'Save & Close New Excel
SumiRow = ActiveSheet.Range(Change_Column & Rows.Count).End(xlUp).Row 'Last Row for Sum
ActiveSheet.Cells(2, 62).Formula = "=SUM(BJ4:BJ" & SumiRow & ")"
Application.DisplayAlerts = False
ActiveWorkbook.Close SaveChanges:=True
Application.DisplayAlerts = True
Compare_Data = ActiveSheet.Cells(i, Change_Column).Value
First_Range_Line = i
End If
Next i
End Sub
Last edited: