FaizanRoshan88
Member
Range(Worksheets("Breaking_Data").Range("M5:R5"), Worksheets("Breaking_Data").Range("M5:R5").End(xlDown)).Copy
I use this line of code for copy data range, this code work good if data range more then 2 rows, if data range have 1 row then its give error. so i need to re-write this line to copy data range however its contain 1 row or number of row. More if possible make this code short for best result.
I use this line of code for copy data range, this code work good if data range more then 2 rows, if data range have 1 row then its give error. so i need to re-write this line to copy data range however its contain 1 row or number of row. More if possible make this code short for best result.
Code:
Private Sub breakMyList_Click()
' This macro takes values in the range myList
' and breaks it in to multiple lists
' and saves them to separate files.
Dim cell As Range
Dim curPath As String
Dim IstCust As Range
Dim MyL As Range
Dim ws As Worksheet, WB As Workbook
Set WB = ActiveWorkbook
Set ws = WB.Sheets("Invoice")
Worksheets("Breaking_Data").Activate
Set MyL = Worksheets("Breaking_Data").Range("B4", Range("I1048576").End(xlUp))
curPath = ActiveWorkbook.Path & "\"
Application.ScreenUpdating = False
Application.DisplayAlerts = False
For Each cell In Range("K5", Range("K1048576").End(xlUp))
Worksheets("Breaking_Data").Range("B2") = cell.Value
[MyL].AdvancedFilter Action:=xlFilterCopy, _
CriteriaRange:=Worksheets("Breaking_Data").Range("B1:I2"), CopyToRange:=Worksheets("Breaking_Data").Range("M4:R4"), Unique:=False
ws.Copy After:=Sheets(WB.Sheets.Count)
ActiveSheet.Name = Worksheets("Breaking_Data").Range("B2").Value
Range(Worksheets("Breaking_Data").Range("M5:R5"), Worksheets("Breaking_Data").Range("M5:R5").End(xlDown)).Copy
On Error GoTo Err_Execute
ActiveSheet.Range("A13").Rows("1:1").Insert Shift:=xlDown
Range(Worksheets("Breaking_Data").Range("M5:R5"), Worksheets("Breaking_Data").Range("M5:R5").End(xlDown)).ClearContents
Next cell