Macro for not copying Data if no data found

RPM1979

New Member
Joined
Jul 8, 2016
Messages
15
I have an worksheet with data. I want to put autofilter on colum "Type" with Criteria "DD", "Cash". Post autofilter i want to copy the data to new Worksheet and name it "New".

Please help me with a macro to do the above task. Please note where the data is appearing in the sheet post auto filter the same to be copied in new sheet. Incase no data meeting the criteria, then only header row to be copied in new sheet.

Please help urgently with macro

Example data is given below
StateCityLiterateIlliterateBPLMiddleClassRichType
MaharashtraMumbai100080010085050Cash
GujaratAhmedabad200015002501650100Cheque
GoaPanjim10009005094010Cash
PunjabMohali20001400501150800Cheque
DelhiDelhi100098020760220Cash
KarnatakaBangalore20001950401530430Cheque

<colgroup><col><col><col span="3"><col><col span="2"></colgroup><tbody>
</tbody>
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
Hi RPM1979,

The following code should help you and let me know if this is what you want.

Sub AutoFilterCashDD()


Dim arrAll(8, 8)

ArrayItem = 0
For i = 1 To 8
If Cells(i + 1, "H") = "Cash" Or Cells(i + 1, "H") = "DD" Then
arrAll(ArrayItem, 1) = Cells(i + 1, "A")
arrAll(ArrayItem, 2) = Cells(i + 1, "B")
arrAll(ArrayItem, 3) = Cells(i + 1, "C")
arrAll(ArrayItem, 4) = Cells(i + 1, "D")
arrAll(ArrayItem, 5) = Cells(i + 1, "E")
arrAll(ArrayItem, 6) = Cells(i + 1, "F")
arrAll(ArrayItem, 7) = Cells(i + 1, "G")
arrAll(ArrayItem, 8) = Cells(i + 1, "H")
ArrayItem = ArrayItem + 1
End If
Next i


Sheets.Add.Name = "New"

Worksheets("New").Range("A1") = Range("A1")
Worksheets("New").Range("B1") = Range("B1")
Worksheets("New").Range("C1") = Range("C1")
Worksheets("New").Range("D1") = Range("D1")
Worksheets("New").Range("E1") = Range("E1")
Worksheets("New").Range("F1") = Range("F1")
Worksheets("New").Range("G1") = Range("G1")
Worksheets("New").Range("H1") = Range("H1")

For j = 1 To 8
Worksheets("New").Cells(j + 1, "A") = arrAll(j - 1, 1)
Worksheets("New").Cells(j + 1, "B") = arrAll(j - 1, 2)
Worksheets("New").Cells(j + 1, "C") = arrAll(j - 1, 3)
Worksheets("New").Cells(j + 1, "D") = arrAll(j - 1, 4)
Worksheets("New").Cells(j + 1, "E") = arrAll(j - 1, 5)
Worksheets("New").Cells(j + 1, "F") = arrAll(j - 1, 6)
Worksheets("New").Cells(j + 1, "G") = arrAll(j - 1, 7)
Worksheets("New").Cells(j + 1, "H") = arrAll(j - 1, 8)
Next j

End Sub
 
Upvote 0
Hi,

Thanks for assistance. I would like to inform you that my data range is quite large and same is variable i.e. sometimes 2000 rows, some time 200000.

I need a macro which will help me in all scenarios. Please suggest macro for same since i am new to VBA coding and macros.
 
Upvote 0
Hi

I am using below VBA code. The problem is even there is data post filter only header row is copied in new sheet.

Basically i want below 2:

Post filter if there is data, then entire data alongwith Header to be copied in new Sheet.
Post filter if there is no data meeting the criteria, only the Header row to be copied in new sheet.

Dim myRange
ActiveSheet.Range("$A:$T").AutoFilter Field:=20, Criteria1:="<>"
ActiveSheet.Range("$A:$AW").AutoFilter Field:=10, Criteria1:=Array( _
"CHQ RETURN", "INWARD CLEARING ", _
"OUTWARD CLEARING", "CLRG&OTHERS")
Set myRange = Range(Range("J2"), Range("J" & Rows.Count).End(xlUp)).SpecialCells(xlCellTypeVisible)
If myRange.Row = 1 Then
myRange.EntireRow.Copy
Sheets.Add After:=Sheets(Sheets.Count)
Selection.PasteSpecial Paste:=xlPasteColumnWidths, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
ActiveSheet.Paste
Sheets("Sheet7").Select
Sheets("Sheet7").Name = "New Data"

Else
Range("A1").Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.Copy
ActiveWindow.ScrollWorkbookTabs Position:=xlLast
Sheets.Add After:=Sheets(Sheets.Count)
Selection.PasteSpecial Paste:=xlPasteColumnWidths, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
ActiveSheet.Paste
Sheets("Sheet7").Select
Sheets("Sheet7").Name = "New Data"

End If

Please assist in modification of above VBA code to get me my data
 
Upvote 0

Forum statistics

Threads
1,216,113
Messages
6,128,903
Members
449,477
Latest member
panjongshing

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