Remove columns when generating PDF VBA - excel

I have a excel file with template which look something like this.
I filter down records based on Customer ID column and save them as individual Pdf's.I am using below VBA code to do the job.
Public Sub Create_PDFs()
Dim CustomerIDsDict As Object, CustomerID As Variant
Dim r As Long
Dim currentAutoFilterMode As Boolean
Set CustomerIDsDict = CreateObject("Scripting.Dictionary")
'The code looks at data on the active sheet
With ActiveSheet
'Save current UI autofilter mode
currentAutoFilterMode = .AutoFilterMode
If currentAutoFilterMode Then .AutoFilter.ShowAllData
'Create dictionary containing unique Customer IDs (column B) and associated Country (column B), keyed on Customer ID
For r = 5 To .Cells(.Rows.Count, "B").End(xlUp).Row
CustomerIDsDict(.Cells(r, "B").Value) = .Cells(r, "C").Value
Next
'For each unique Customer ID
For Each CustomerID In CustomerIDsDict.keys
'AutoFilter on column B (Field:=2) with this Customer ID
'.UsedRange.AutoFilter Field:=2, Criteria1:=CustomerID
With .Range("A3")
.AutoFilter Field:=2, Criteria1:=CustomerID
.Rows(2).EntireRow.Hidden = False
End With
'Save filtered data as PDF file "<Customer ID> <Country>.pdf" in same folder as this workbook
.ExportAsFixedFormat Type:=xlTypePDF, Filename:=ThisWorkbook.Path & "\" & CustomerID & " " & CustomerIDsDict(CustomerID) & ".pdf", _
Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
Next
'Restore previous autofilter, if any
If currentAutoFilterMode Then
.AutoFilter.ShowAllData
Else
.AutoFilterMode = False
End If
End With
End Sub
But certain CustomerID's don't have any values in last six columns. namely New volume Qty,New volume price these columns. Before generating PDF's I want to check these columns if anyone of records as values then we can show those columns else need not to show them in Pdf's file.
For example:
If you see for this CustomerID it doesn't have any value in columns New Volume qty2,3,4 and New Volume Price 2,3,4 so in this case I don't want these columns to shown in PDF of this particular customer ID.
Whereas in the below customer ID
Only New volume qty 3,4 and New volume price 3,4 doesn't have value . so I want to remove only these columns before saving them as PDF.
Headers for the mentioned column
Is there way I can do this using the above script. Can anyone help me with this.

Please, test the next code. You did not answer my clarification question and it allows creating the columns range to be checked. Than, creates an array of these columns numbers (arrCols), checks if no any value in each of such columns and place a cell of them in a range (rngHd), for such a case. Then, hides them before exporting and makes them visible after exporting:
Public Sub Create_PDFs()
Dim CustomerIDsDict As Object, CustomerID As Variant
Dim r As Long, currentAutoFilterMode As Boolean
Dim strCols As String, rngHd As Range, lastR As Long, arrCols, i As Long, iRow As Long
strCols = "O:V": arrCols = Evaluate("column(" & strCols & ")") 'place in an array the columns to be checked number
iRow = 5
Set CustomerIDsDict = CreateObject("Scripting.Dictionary")
'The code looks at data on the active sheet
With ActiveSheet
lastR = .Range("A" & .Rows.count).End(xlUp).row 'last row in A:A
'Save current UI autofilter mode
currentAutoFilterMode = .AutoFilterMode
If currentAutoFilterMode Then .AutoFilter.ShowAllData
'Create dictionary containing unique Customer IDs (column B) and associated Country (column B), keyed on Customer ID
For r = 5 To .cells(.Rows.count, "B").End(xlUp).row
CustomerIDsDict(.cells(r, "B").Value) = .cells(r, "C").Value
Next
'For each unique Customer ID
For Each CustomerID In CustomerIDsDict.Keys
'AutoFilter on column B (Field:=2) with this Customer ID
With .Range("A3")
.AutoFilter field:=2, Criteria1:=CustomerID
.Rows(2).EntireRow.Hidden = False
End With
'place the empty columns one cell in a Union range
For i = 1 To UBound(arrCols)
If WorksheetFunction.CountA(.Range(.cells(iRow, arrCols(i)), .cells(lastR, arrCols(i))).SpecialCells(xlCellTypeVisible)) = 0 Then
If rngHd Is Nothing Then
Set rngHd = .cells(3, arrCols(i))
Else
Set rngHd = Union(rngHd, .cells(3, arrCols(i)))
End If
End If
Next i
'Hide the empty columns, if the case:
If Not rngHd Is Nothing Then rngHd.EntireColumn.Hidden = True
'Save filtered data as PDF file "<Customer ID> <Country>.pdf" in same folder as this workbook
.ExportAsFixedFormat Type:=xlTypePDF, filename:=ThisWorkbook.Path & "\" & CustomerID & " " & CustomerIDsDict(CustomerID) & ".pdf", _
Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
rngHd.EntireColumn.Hidden = False 'make all checked columns visible
Set rngHd = Nothing 'reSet the range as Nothing for the future iterations
Next
'Restore previous autofilter, if any
If currentAutoFilterMode Then
.AutoFilter.ShowAllData
Else
.AutoFilterMode = False
End If
End With
End Sub
No error handling for the case of the column already hidden. Theoretically, such a situation cannot appear in normal using way. It ca be previously check if the column visibility...

Related

When a match is found, copy row from one sheet to the row where the match was found in macro

I have a table on the MainDashboard that is updated by a macro. It copies data from selected sheets and updates this main table. Here is my code but I am stuck. I need it to:
Loop through every sheet in Sheet List
Loop through every value in the first column of the tables on each sheet
Check to see if these IDs are in the first column of the Main Dashboard table
If yes, Copy everything on that row and paste it over the same row where the value was found on the main Dashboard table
If no, add it to the bottom of the row
When finished have a MsgBox that says, you have modified x entries and added x new entries
Sub Update()
Dim SheetList As Variant
Dim x As Long
Dim TaskListTable As Range
Dim TaskList As ListObject
Dim SortColumn As Range
Dim TaskId As Integer
Dim LastRow As Range
Dim MDLastRow As Range
'What I want the Excel program to do before I start
With Application
.ScreenUpdating = False
.StatusBar = "Running..."
End With
'List Sheet Names into an Array Variable
SheetList = Array(S1, S2, S3, S4, S5, S6, S7, S8, S9, S10, S11, S12, S13, S14)
'Loop through list
For x = LBound(SheetList) To UBound(SheetList)
'Code will fail unless you activate the sheet first
SheetList(x).Activate
'Loop for b15 in column 1 down for every row to last row
LastRow = Range("B" & Rows.Count).End(xlUp).Row
MDLastRow = Range("B" & Rows.Count).End(xlUp).Row
For Each TaskID In Range("B15": LastRow)
If WorksheetFunction.Match(Range("B15:MDLastRow"), Then
SheetList(x).Range("TaskID").End(xlRight).Copy
'PASTE TO ENTIRE ROW WHERE THE MATCH WAS FOUND
End If
'Else add row to the bottom
SheetList(x).Range("TaskID").End(xlRight).Copy
MainDashboard.Range("B" & Rows.Count).End(xlUp).Offset(1, 0).PasteSpecial xlPasteAll
Next x
MainDashboard.Activate
'MsgBox
'You have Modified X tasks
'You have Added X tasks
'Sort the table by the latest Date
Set TaskList = MainDashboard.ListObjects("Task_List")
Set SortColumn = Range("Task_List[DATE]")
With TaskList.Sort
.SortFields.Clear
.SortFields.Add Key:=SortColumn, SortOn:=xlSortOnValues, Order:=xlAscending
.Header = xlYes
.Apply
End With
'What I want the Excel program to do after I have finished
With Application
.ScreenUpdating = True
.StatusBar = "Complete"
.CutCopyMode = False
End With
End Sub
Thanks in advance

Copy row of data based on criteria AND "label" that copied data in last column

I have working code that checks for a criteria in each row, and if met, copies that whole row of data over to a different workbook. But! I need to be able to add text to the last column of the copied data (Column S) that essentially labels what criteria was met that made the code copy it over because I will soon be expanding to check for multiple different criteria.
So for every row that meets the criteria and gets copied, I want to add "Criteria1" next to it in column S in the new workbook (it will always be column S that will be the first available column).
I have mangled this code together through inheritance and all of your help, so I don't really even know where to begin.
Private Sub Workbook_AfterSave(ByVal Success As Boolean)
Dim CoderBook As Workbook
Dim Referrals As Worksheet
Dim Review As Workbook
Dim Crit As Worksheet
Dim LastRow As Long
Dim NextRow As Long
Dim i As Long
Set CoderBook = Workbooks.Open("Coder Referrals.xlsx")
Set Referrals = CoderBook.Sheets("Sheet1")
Set Review = ThisWorkbook
Set Crit = Review.Sheets("Criteria")
'Search code
LastRow = Crit.Cells.Find(What:="*", After:=[A1], SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
Application.ScreenUpdating = False
'Loop search code
For i = 2 To LastRow
'Specialized Criteria1 Check
If Crit.Range("F" & i) <> Crit.Range("G" & i) Or _
Crit.Range("I" & i) <> Crit.Range("J" & i) Then
'If meets Criteria1 check, then copy appropriate rows to CoderBook Referrals sheet
Referrals.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).EntireRow.Value = Crit.Rows(i).Value
End If
Next i
'End loop code
CoderBook.Close SaveChanges:=True
Application.ScreenUpdating = True
End Sub
Split the or into two statements:
For i = 2 To LastRow
j = Referrals.Cells(Rows.Count, 1).End(xlUp).row + 1
'Specialized Criteria1 Check
If Crit.Range("F" & i) <> Crit.Range("G" & i) Then
'If meets Criteria1 check, then copy appropriate rows to CoderBook Referrals sheet
Referrals.Rows(j).EntireRow.Value = Crit.Rows(i).Value
Referrals.Range("S" & j).Value = "Criteria1"
End If
If Crit.Range("I" & i) <> Crit.Range("J" & i) Then
Referrals.Rows(j).EntireRow.Value = Crit.Rows(i).Value
if Referrals.Range("S" & j).value = vbNullString then
Referrals.Range("S" & j).Value = "Criteria2"
Else
Referrals.Range("S" & j).Value = Referrals.Range("S" & j).Value & ", " & "Criteria2"
End if
Next i

VBA Renaming sheets based on varible in a for loop and storing new variables

I'm trying to do the following tasks.
Create X-amount of new sheets in DestWorkbook based on row numbers in the Insert_Data_Sheet table. I've solved this
Rename the sheet according to the D-Column data starting from "D2". So I would like to to rename the first sheet "1865727" and the second sheet "1872188" etc. I've solved this
Store the data in D-column in a seperate variables. No luck with this yet
Here is pictures of the data:
https://pasteboard.co/HABwijq.jpg
https://pasteboard.co/HABwEhE.jpg
Full Code:
Public Sub TermSwap()
Application.ScreenUpdating = False
Dim DestWorkbook As Workbook, AC_Live_Workbook As Workbook, AC_Maturity_Workbook As Workbook
Dim Insert_Data_Sheet As Worksheet, AC_Live_Sheet As Worksheet, AC_Maturity_Sheet As Worksheet, Booked_Sheet As Worksheet
Dim i As Long, d As Long, lastRowA_AC_Live As Long, lastRow_AC_Maturity As Long, NumberOfPages As Long
'Dim Swap_Link_Tid As Long
'I will use these in the end when importing the AC Reports
'AC_Live_Filename = Application.GetOpenFilename(, , "AVAA AC LIVE RAPORTTI")
'AC_Maturity_Filename = Application.GetOpenFilename(, , "AVAA AC MATURITY RAPORTTI")
'Insert filename from above lines as a parameter in the end
Set DestWorkbook = Workbooks("TermSwap")
Set AC_Live_Workbook = Workbooks.Open(FileName:="C:\Users\z000479\Desktop\Makrot\Term Swap makro\Harjoitustiedostot\ALL_COLUMNS_FI_180817.xlsx")
Set AC_Maturity_Workbook = Workbooks.Open(FileName:="C:\Users\z000479\Desktop\Makrot\Term Swap makro\Harjoitustiedostot\ALL_COLUMNS_FI_180820.xlsx")
Set Insert_Data_Sheet = DestWorkbook.Sheets("Insert_Data")
Set Booked_Sheet = DestWorkbook.Sheets("booked")
Set AC_Live_Sheet = AC_Live_Workbook.Sheets("Result")
Set AC_Maturity_Sheet = AC_Maturity_Workbook.Sheets("Result")
'Finds the last row in A-Column in the AC_Live_Sheet and AC_Maturity_Sheet
lastRow_AC_Live = AC_Live_Sheet.Cells(AC_Live_Sheet.Rows.Count, "A").End(xlUp).Row
lastRow_AC_Maturity = AC_Maturity_Sheet.Cells(AC_Maturity_Sheet.Rows.Count, "A").End(xlUp).Row
'Create X-amount of new sheets in DestWorkbook based on row numbers in the Insert_Data_Sheet table.SOLVED
' Rename the sheet according to the D-Column data starting from "D2". SOLVED
' Store the data in D-column in a seperate variables. UNSOLVED
NumberOfPages = Insert_Data_Sheet.Cells((Insert_Data_Sheet.Rows.Count), "A").End(xlUp).Row - 1
Dim target_range As String
For d = 2 To NumberOfPages + 1
target_range = Insert_Data_Sheet.Range("D" & d).Value
DestWorkbook.Worksheets.Add(After:=DestWorkbook.Worksheets(DestWorkbook.Worksheets.Count)).Name = target_range
Next d
' AC LIVE Starts here:
' Show all cells
If AC_Live_Sheet.FilterMode Then
AC_Live_Sheet.ShowAllData
End If
'Delete row 2
AC_Live_Sheet.Range("2:2").Delete
'Autofiter ON. Filters LIVE_DEAL and SWAP_LINK_TID. Change SWAP_LINK_TID to a variable.
'Range syntax here is Range ("$A$1:$DS$" & lastRow)
If Not AC_Live_Sheet.AutoFilterMode Then
AC_Live_Sheet.Range("A1").AutoFilter
AC_Live_Sheet.Range("$A$1:$DS$" & lastRow_AC_Live).AutoFilter Field:=1, Criteria1:= _
"LIVE_DEAL"
AC_Live_Sheet.Range("$A$1:$DS$" & lastRow_AC_Live).AutoFilter Field:=7, Criteria1:= _
"1889087"
End If
'Copy pastes visible cells to Booked_Sheet("A1")
With AC_Live_Sheet
.UsedRange.SpecialCells(xlCellTypeVisible).Copy Destination:=Booked_Sheet.Cells(1, 1)
End With
' AC_MATURITY starts here
' Show all cells
If AC_Maturity_Sheet.FilterMode Then
AC_Maturity_Sheet.ShowAllData
End If
'Delete row 2
AC_Maturity_Sheet.Range("2:2").Delete
'Autofiter ON. Filters LIVE_DEAL and SWAP_LINK_TID.
'Range syntax here is Range ("$A$1:$DS$" & lastRow)
'I need to change SWAP_LINK_TID to a variable
If Not AC_Maturity_Sheet.AutoFilterMode Then
AC_Maturity_Sheet.Range("A1").AutoFilter
AC_Maturity_Sheet.Range("$A$1:$DS$" & lastRow_AC_Maturity).AutoFilter Field:=1, Criteria1:= _
"LIVE_DEAL", Operator:=xlOr, Criteria2:="=MAT_DEAL"
AC_Maturity_Sheet.Range("$A$1:$DS$" & lastRow_AC_Maturity).AutoFilter Field:=7, Criteria1:= _
"1889087"
End If
'Copy pastes visible cells to Booked_Sheet("A1")
With AC_Maturity_Sheet
.UsedRange.SpecialCells(xlCellTypeVisible).Copy Destination:=Booked_Sheet.Cells(6, 1)
End With
'Closes AC Workbooks and activates the Booked_Sheet
' Error here. It asked the file to be saved. I want to ignore it.
AC_Live_Workbook.Close
AC_Maturity_Workbook.Close
Booked_Sheet.Activate
Application.ScreenUpdating = True
End Sub
The following is to show how you might load the unique column D numbers into a dictionary as its keys and loop that dictionary's keys to add your new sheets. You could do your filter in the same loop, again using the current key of the dictionary for filtering or use it later. This is not intended to be copy-paste-work but to show you the parts you could use.
Option Explicit
Public Sub test()
Dim valuesDict As Object, arr(), i As Long, lastRow As Long
Set valuesDict = CreateObject("Scripting.Dictionary")
With ThisWorkbook.Worksheets("Sheet1")
lastRow = .Cells(.Rows.Count, "D").End(xlUp).Row 'find last row of your numbers
Select Case lastRow
Case Is < 2
Exit Sub
Case 2 '< Load your number into an array
ReDim arr(1, 1)
arr(1, 1) = .Range("D2")
Case Else
arr = .Range("D2:D" & lastRow).Value
End Select
End With
For i = LBound(arr, 1) To UBound(arr, 1) 'Add unique values to the range
valuesDict(arr(i, 1)) = 1
Next
Dim key As Variant
For Each key In valuesDict.keys
If Not Evaluate("ISREF('" & key & "'!A1)") Then 'If sheet doesn't exist add it. Credit to #Rory for this method.
ThisWorkbook.Worksheets.Add
ActiveSheet.NAME = key
End If
Next key
'Other code.......
For Each key In valuesDict.keys
AC_Live_Sheet.Range("$A$1:$DS$" & lastRow_AC_Live).AutoFilter Field:=7, Criteria1:=key
Next key
'Other code
End Sub

Copy and sort data from one sheet to another, based on cell values

I have searched a lot of similar topics and have had some help but I cant find a way to do what I need (probably because of my limited experience with excel and vba), so here it goes:
I have a (Source)sheet 'offers' , which is populated daily, with the columns below:
columns: b c d e f g
header: offercode issue dt worktype customer sent dt confirmation dt
xxx.xx. 1/1/14 MI john 1/1/14 3/1/14
aaa.aa. 1/1/14 MD bob 2/1/14
bbb.bb 2/1/14 SI peter 2/1/14 3/1/14
what I need is to copy all rows that get a confirmation date (not blank) in another sheet"production orders"(destination)
where I generate production order codes and input other kind of data :
columns: b c d e f g
header: offercode productioncode worktype start end confirmation dt
xxx.xx. 1/1/14 MI 5/1/14 3/1/14
bbb.bb 2/1/14 SI 6/1/14 3/1/14
note that column b and b & c contain formulas (generates offer codes)
my problem is that data is populated daily, and offers(Source Sheet) should be sorted by issue date and once they get confirmed(input confirmation date->non blank) they should be copied in the other sheet but sorted (or polulate the next empty row) by confirmation date eg:
columns: b c d e f g
header: offercode productioncode worktype start end confirmation dt
xxx.xx. XX.XXX. MI 5/1/14 3/1/14
bbb.bb BB.BBB SI 6/1/14 3/1/14
aaa.aa. AA>AAA MD 4/1/14
another issue is how often or when is the second (Destination Sheet) list refreshs with new data, my guess is that a control button click after every data entry instance would work (and make sure that the list is up to date)
thank you in advance,
Angelos
So, this is what is working for me right now, its all based on #simoco's code:
I am checking in it for operational consistency, but the code works fine.
It copies and pastes only the columns of (my) interest where I need it and then sorts a dynamic range.
Sub copycolumnsonly()
Dim sh1 As Worksheet
Dim sh2 As Worksheet
Dim lastrow1 As Long
Dim lastrow2 As Long
Dim j As Long
Dim i As Long
Dim rng As Range
'set correct name of the sheet with your data'
Set sh1 = ThisWorkbook.Worksheets("ÐÑÏÓÖÏÑÅÓ")
'set correct name of the sheet where you need to paste data'
Set sh2 = ThisWorkbook.Worksheets("ÅÍÔÏËÅÓ ÐÁÑÁÃÙÃÇÓ")
'determining last row of your data in file ÁÁÁÁÁÁÁÁ.xlsx'
lastrow1 = sh1.Range("C" & sh1.Rows.Count).End(xlUp).Row
'determining last row of your data in file ÂÂÂÂÂÂÂÂ.xls'
lastrow2 = sh2.Range("F" & sh2.Rows.Count).End(xlUp).Row
'clear content in sheet2
sh2.Range("F11:F" & lastrow2).ClearContents
sh2.Range("G11:G" & lastrow2).ClearContents
sh2.Range("N11:N" & lastrow2).ClearContents
'suppose that in sheet2 data starts from row #11
j = 11
For i = 0 To lastrow1
Set rng = sh1.Range("G11").Offset(i, 0)
'check whether value in column D is not empy
If Not (IsNull(rng) Or IsEmpty(rng)) Then
sh1.Range("B" & i + 11).Copy
sh2.Range("F" & j).PasteSpecial xlPasteValues
sh1.Range("g" & i + 11).Copy
sh2.Range("G" & j).PasteSpecial xlPasteValues
sh1.Range("K" & i + 11).Copy
sh2.Range("N" & j).PasteSpecial xlPasteValues
j = j + 1
End If
Next i
Application.CutCopyMode = False
'sorting the new list, recorded macro tweaked to use a dynamic named range
ActiveWorkbook.Worksheets("ÅÍÔÏËÅÓ ÐÁÑÁÃÙÃÇÓ").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("ÅÍÔÏËÅÓ ÐÁÑÁÃÙÃÇÓ").Sort.SortFields.Add Key:=Range( _
"G:G"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortNormal
With ActiveWorkbook.Worksheets("ÅÍÔÏËÅÓ ÐÁÑÁÃÙÃÇÓ").Sort
.SetRange Range("PRODUCTIONORDERS")
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End Sub
this is what I have come up with as a completly different approach,
I would greatly appreciate it if you could check it for error handling, or invalid input from users etc
(see comments in code)
`
Sub ActiveToLastRow()
Dim sh1 As Worksheet
Dim sh2 As Worksheet
Dim activerow As Long
Dim lastrow2 As Long
Dim rng As Range
Dim confirmation As Range
'set correct name of the sheet with your data
Set sh1 = ThisWorkbook.Worksheets("ÐÑÏÓÖÏÑÅÓ")
'set correct name of the sheet where you need to paste data
Set sh2 = ThisWorkbook.Worksheets("ÅÍÔÏËÅÓ ÐÁÑÁÃÙÃÇÓ")
'making sure the user selects the correct offercode via inputbox to get its rownumber --> see activerow variable
Set rng = Application.InputBox("dialeje prosfora", "epilogh prosforas", Type:=8)
'getting the information(confirmation date) via input box form the user
Dim TheString As String
Dim TheDate As Date
TheString = Application.InputBox("Enter A Date", "epibebaiwsh anathesis")
If IsDate(TheString) Then
TheDate = DateValue(TheString)
Else
MsgBox "Invalid date"
'need to end sub if user input is invalid
End If
'determining active row of your data in file ÁÁÁÁÁÁÁÁ.xlsx where data input occurs <-- user input via 1st input box
activerow = rng.Row
Set confirmation = sh1.Range("G" & activerow)
confirmation.Value = TheDate
'determining last row of your data in file ÂÂÂÂÂÂÂÂ.xls'
lastrow2 = sh2.Range("F" & sh2.Rows.Count).End(xlUp).Row
'determining what to copy and where
sh1.Range("B" & activerow).Copy
sh2.Range("F" & lastrow2 + 1).PasteSpecial xlPasteValues
sh1.Range("g" & activerow).Copy
sh2.Range("G" & lastrow2 + 1).PasteSpecial xlPasteValues
sh1.Range("K" & activerow).Copy
sh2.Range("N" & lastrow2 + 1).PasteSpecial xlPasteValues
Application.CutCopyMode = False
'activating destination sheet for testing purposes
sh2.Activate
End Sub`
It looks like you simply need to copy over only those rows with a value in the "Confirmation Date" column - if I read the above correctly.
If the sheet with the daily updates is called "First", and the resultant sheet with only the confirmed orders is called "Second", the following should do it...
Sub Macro1()
'
' Macro1 Macro
'
'
lastRow = 10 ' hard coded here; use whatever technique to get real value.
'Copy over the headers to the new sheet
Sheets("First").Select
Rows("1:1").Select
Selection.Copy
Sheets("Second").Select
Range("A1").Select
ActiveSheet.Paste
Application.CutCopyMode = False
Columns("A:F").ColumnWidth = 9
Columns("G:G").ColumnWidth = 12
Sheets("First").Select
' Range("G1").Select
Confirm_Count = 0
For Row = 1 To lastRow
If Len(Range("G1").Offset(Row, 0)) > 1 Then
Rows(Row + 1).Select
Selection.Copy
Sheets("Second").Select
Confirm_Count = Confirm_Count + 1
Range("A1").Offset(Confirm_Count, 0).Select
ActiveSheet.Paste
Application.CutCopyMode = False
Sheets("First").Select
End If
Next Row
End Sub

Trying to filter data and display it on another sheet, however the data isn't fully copied

im trying to filter some data(sheet="Imported Data") and paste the data that matches to a sheet("Test"). However somehow it doesn't fully work. I have asked this kinda question before, but I've been trying for 3hours now but I can't get it done!
What I want:
- There are 3 single cells which the user can fill in which are the criteria(Collection , System and Tag)
- Collection is a MUST fill in for the user, the others can be left blank if the user wants it so. The result must be the entire row(Column A,B and C)
- If one, two or three criteria are filled in the chosen criterias must all match to copy to the new sheet (so if one criteria is left blank, the result should be all all three criteria. But the one not filled in can be any value).
- If all criteria match, from the sheet="Imported Data" also the value of column E must be copied to sheet("Test"),
this value of column E must be the cell which is in the same row as the matched values.
If you have any questions, fill free to ask... it's a bit hard to explain.
Thanks for the help in advance! This is what I have now:
Option Explicit
Sub FilterButton()
Dim SrcSheet As Worksheet, DestSheet As Worksheet
Dim SourceRange As Range
Dim aCell As Range, bCell As Range
Dim iLastRow As Long, zLastRow As Long
Dim Collection As String, System As String, Tag As String
With Application
.ScreenUpdating = False
.EnableEvents = False
End With
'~~> Set your sheet
Set DestSheet = Sheets("Test")
Set SrcSheet = Sheets("Imported Data")
'~~> Find Last Row in Col A in the source sheet
With SrcSheet
iLastRow = .Range("A" & .Rows.Count).End(xlDown).Row
End With
'~~> Find Last "Available Row for Output" in Col A in the destination sheet
With DestSheet
zLastRow = .Range("A" & .Rows.Count).End(xlUp).Row + 1
End With
'~~> Set your ranges
Set SourceRange = SrcSheet.Range("A2:A" & iLastRow)
'~~> Search values
Collection = Trim(Range("lblImportCollection").Value)
System = Trim(Range("lblImportSystem").Value)
Tag = Trim(Range("lblImportTag").Value)
With SourceRange
'~~> Match 1st Criteria
Set aCell = .Find(What:=Collection, LookIn:=xlValues, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
'~~> If found
If Not aCell Is Nothing Then
Set bCell = aCell
'~~> Copy A:C. Then match for Crit B and Crit C and remove what is not required
DestSheet.Range("A" & zLastRow & ":" & "C" & zLastRow).Value = _
SrcSheet.Range("A" & aCell.Row & ":" & "C" & aCell.Row).Value
'~~> Match 2nd Criteria
If Len(Trim(System)) = 0 Or _
aCell.Offset(, 1).Value <> System Then _
DestSheet.Range("B" & zLastRow).ClearContents
MsgBox System & " Not Found"
'~~> Match 3rd Criteria
If Len(Trim(Tag)) = 0 Or _
aCell.Offset(, 2).Value <> Tag Then _
DestSheet.Range("C" & zLastRow).ClearContents
MsgBox Tag & " Not Found"
If Not DestSheet.Range("B" & zLastRow).ClearContents Or _
DestSheet.Range("C" & zLastRow).ClearContents Then
'~~> Copy E:E. Then match for Crit B and Crit C and remove what is not required
DestSheet.Range("D" & zLastRow & ":" & "D" & zLastRow).Value = _
SrcSheet.Range("E" & aCell.Row & ":" & "E" & aCell.Row).Value
End If
'~~> Increase last row by 1 for output
zLastRow = zLastRow + 1
Do
Set aCell = .FindNext(After:=aCell)
If Not aCell Is Nothing Then
If aCell.Address = bCell.Address Then Exit Do
'~~> Match 2nd Criteria
If Len(Trim(System)) = 0 Or _
aCell.Offset(, 1).Value <> System Then _
DestSheet.Range("B" & zLastRow).ClearContents
'~~> Match 3rd Criteria
If Len(Trim(Tag)) = 0 Or _
aCell.Offset(, 2).Value <> Tag Then _
DestSheet.Range("C" & zLastRow).ClearContents
'~~> Increase last row by 1 for output
zLastRow = zLastRow + 1
Else
Exit Do
End If
Loop
Else
MsgBox Collection & " not Found"
End If
End With
With Application
.ScreenUpdating = True
.EnableEvents = True
End With
End Sub
I think it would be simpler to use the AdvancedFilter method, but your data setup is important.
I have assumed that your original data has Five columns (A:E), with headers being in Row 1
I have further assumed that the headers in columns A:C are "Collection", "System" and "Tag"
I have also assumed there is nothing of importance on "Test" (If there is, instead of "clearing" the entire worksheet, you can alter the code to only clear the relevant part, perhaps the first four columns.
Set up a criteria range (three columns, two rows) on your Imported Data sheet with the same headings in Row 1 as in columns A:C of your data source. You could use Data Validation to force an entry; or you could code something within the macro itself. Or you could develop a UserForm to populate these cells
After your user fills in the criteria, the macro should copy the relevant data. If all three items are populated, it will delete column D, otherwise, it will delete columns D:E.
If I've made some wrong assumptions about how your data is set up, you may need to delete more columns after doing the Filter.
Option Explicit
Sub FilterButton()
Dim SrcSheet As Worksheet, DestSheet As Worksheet
Dim SourceRange As Range
Dim CriteriaRange As Range
Dim DestRange As Range
With Application
.ScreenUpdating = False
.EnableEvents = False
End With
'~~> Set your sheet
Set DestSheet = Sheets("Test")
Set SrcSheet = Sheets("Imported Data")
'~~> Set your ranges
Set SourceRange = SrcSheet.Range("a1").CurrentRegion
Set CriteriaRange = SrcSheet.Range("H1:J2") 'or wherever
Set DestRange = DestSheet.Range("A1")
'Activate Destination Sheet, Clear it, and run the filter
DestSheet.Activate 'Can only copy filtered data to active sheet
DestSheet.Cells.Clear
SourceRange.AdvancedFilter xlFilterCopy, CriteriaRange, DestRange
'Delete column D always, delete Column E if not three criteria
With DestRange.CurrentRegion
If WorksheetFunction.CountA(CriteriaRange.Rows(2)) <> 3 Then
Range(.Columns(4), .Columns(5)).Delete
Else
.Columns(4).Delete (xlToLeft)
End If
End With
With Application
.ScreenUpdating = True
.EnableEvents = True
End With
End Sub

Resources