Raaj Chauhan
New Member
- Joined
- Aug 1, 2015
- Messages
- 9
I am very new to VBA programming and trying to write a VBA code in excel This code will filter my file by criteria as per column “V”. Finally, I found the code on google and its working fine. Exactly what I was looking for. But I need small changes. In columns “V” I need only selected criteria “LFU” and “MIS”
As per below codes it copying the entire columns within given range from “A to V”. I want only selected columns like “B, C, I, J, K, L, M, U, and V.
I think I'm making this harder then it is, but I'm just not comprehending what needs to be done. Suggestions?
As per below codes it copying the entire columns within given range from “A to V”. I want only selected columns like “B, C, I, J, K, L, M, U, and V.
I think I'm making this harder then it is, but I'm just not comprehending what needs to be done. Suggestions?
Code:
Sub NewWBS()
Dim wbNew As Workbook
Dim wsData As Worksheet
Dim wsCrit As Worksheet
Dim wsNew As Worksheet
Dim rngCrit As Range
Dim LastRow As Long
Set wsData = Worksheets("Data") ' name of worksheet with the data
Set wsCrit = Worksheets.Add
LastRow = wsData.Range("A" & Rows.Count).End(xlUp).Row
' column V has the criteria
wsData.Range("V1:V" & LastRow).AdvancedFilter Action:=xlFilterCopy, CopyToRange:=wsCrit.Range("A1"), Unique:=True
Set rngCrit = wsCrit.Range("A2")
While rngCrit.Value <> ""
Set wsNew = Worksheets.Add
' change v to reflect columns to copy (wants to cutomize the columns)
wsData.Range("A1:V" & LastRow).AdvancedFilter Action:=xlFilterCopy, CriteriaRange:=rngCrit.Offset(-1).Resize(2), CopyToRange:=wsNew.Range("A1"), Unique:=True
wsNew.Name = rngCrit
wsNew.Copy
Set wbNew = ActiveWorkbook
' saves new workbook in path of existing workbook
wbNew.SaveAs ThisWorkbook.Path & "\" & rngCrit
wbNew.Close SaveChanges:=True
Application.DisplayAlerts = False
wsNew.Delete
rngCrit.EntireRow.Delete
Set rngCrit = wsCrit.Range("A2")
Wend
wsCrit.Delete
Application.DisplayAlerts = True
End Sub