Need to convert a excel to CSV format with specific condition.
[TABLE="width: 500"]
<tbody>[TR]
[TD]DEp[/TD]
[TD]YY[/TD]
[TD]DD[/TD]
[TD]YTD[/TD]
[TD]Invoice Number[/TD]
[/TR]
[TR]
[TD]ab[/TD]
[TD]12[/TD]
[TD]1[/TD]
[TD]Y[/TD]
[TD]123456[/TD]
[/TR]
[TR]
[TD]as[/TD]
[TD]11[/TD]
[TD]2[/TD]
[TD]N[/TD]
[TD]123456[/TD]
[/TR]
[TR]
[TD]qw[/TD]
[TD]1[/TD]
[TD]1[/TD]
[TD]Y[/TD]
[TD]466578[/TD]
[/TR]
[TR]
[TD]qq[/TD]
[TD]2[/TD]
[TD]1[/TD]
[TD]Y[/TD]
[TD]123874[/TD]
[/TR]
</tbody>[/TABLE]
1) Need to filter using invoice number using unique number
2) Copy the visible cells expect the last column(invoice #)
3) Save the file with invoice number
4) I have the code to all expect the deleting the last column(invoice#)
My Code :
Sub Button2_Click()
Dim LR As Long
Application.ScreenUpdating = False
Application.DisplayAlerts = False
iCol = 5
strOutputFolder = "D:\temp"
Set ws = ThisWorkbook.ActiveSheet
Set rngLast = Columns(iCol).Find("*", Cells(1, iCol), , , xlByColumns, xlPrevious)
ws.Columns(iCol).AdvancedFilter Action:=xlFilterInPlace, Unique:=True
Set rngUnique = Range(Cells(2, iCol), rngLast).SpecialCells(xlCellTypeVisible)
If Dir(strOutputFolder, vbDirectory) = vbNullString Then MkDir strOutputFolder
For Each strItem In rngUnique
If strItem <> "" Then
ws.UsedRange.AutoFilter Field:=iCol, Criteria1:=strItem.Value
Workbooks.Add
ws.UsedRange.SpecialCells(xlCellTypeVisible).Copy Destination:=[A1]
strFilename = strOutputFolder & "\" & strItem
ActiveWorkbook.SaveAs Filename:=strFilename, FileFormat:=xlCSV
ActiveWorkbook.Close savechanges:=False
End If
Next
ws.ShowAllData
[TABLE="width: 500"]
<tbody>[TR]
[TD]DEp[/TD]
[TD]YY[/TD]
[TD]DD[/TD]
[TD]YTD[/TD]
[TD]Invoice Number[/TD]
[/TR]
[TR]
[TD]ab[/TD]
[TD]12[/TD]
[TD]1[/TD]
[TD]Y[/TD]
[TD]123456[/TD]
[/TR]
[TR]
[TD]as[/TD]
[TD]11[/TD]
[TD]2[/TD]
[TD]N[/TD]
[TD]123456[/TD]
[/TR]
[TR]
[TD]qw[/TD]
[TD]1[/TD]
[TD]1[/TD]
[TD]Y[/TD]
[TD]466578[/TD]
[/TR]
[TR]
[TD]qq[/TD]
[TD]2[/TD]
[TD]1[/TD]
[TD]Y[/TD]
[TD]123874[/TD]
[/TR]
</tbody>[/TABLE]
1) Need to filter using invoice number using unique number
2) Copy the visible cells expect the last column(invoice #)
3) Save the file with invoice number
4) I have the code to all expect the deleting the last column(invoice#)
My Code :
Sub Button2_Click()
Dim LR As Long
Application.ScreenUpdating = False
Application.DisplayAlerts = False
iCol = 5
strOutputFolder = "D:\temp"
Set ws = ThisWorkbook.ActiveSheet
Set rngLast = Columns(iCol).Find("*", Cells(1, iCol), , , xlByColumns, xlPrevious)
ws.Columns(iCol).AdvancedFilter Action:=xlFilterInPlace, Unique:=True
Set rngUnique = Range(Cells(2, iCol), rngLast).SpecialCells(xlCellTypeVisible)
If Dir(strOutputFolder, vbDirectory) = vbNullString Then MkDir strOutputFolder
For Each strItem In rngUnique
If strItem <> "" Then
ws.UsedRange.AutoFilter Field:=iCol, Criteria1:=strItem.Value
Workbooks.Add
ws.UsedRange.SpecialCells(xlCellTypeVisible).Copy Destination:=[A1]
strFilename = strOutputFolder & "\" & strItem
ActiveWorkbook.SaveAs Filename:=strFilename, FileFormat:=xlCSV
ActiveWorkbook.Close savechanges:=False
End If
Next
ws.ShowAllData