Hi all! I am trying to filter a data file till there is only one row of data (excluding headers) and then I copy the relevant cells in that row. However, there will be some instances where my filter criteria is not enough to filter till one row of data. If that is the case, I want the user to key in their desired data row and the code should copy the relevant parameters from that row. Here is my code.
This is my contingency code when an inputbox is used. If the user keys in "2", the code should copy cells from the second row of data. But that is not working. An error pops up saying "no cells found'. How do I edit my code to copy cells from the desired filtered row based on user input?
Thank you!
Code:
Sub ELNParametersCopyPaste()
Dim csv As Workbook
Dim pdttype As Worksheet
Dim chartbuilder As Workbook
Dim rngDest As Range
Dim copyRange As Range
Dim lastRow As Long
Set chartbuilder = Workbooks("Product Write Up Assistant (5 Jan).xlsm")
Set csv = Workbooks("CSV.csv")
Set Research = Workbooks("Investment Rationale & Summary.xlsx")
csv.Activate
' Turn off any autofilters that are already set
csv.Activate
ActiveSheet.AutoFilterMode = False
' Filters for Requesters, Product Type & Ticker 1
lastRow = Cells(Rows.Count, "AM").End(xlUp).Row
ActiveSheet.Range("$A$1:$AM" & lastRow).AutoFilter Field:=36, Criteria1:=Array("Hau Tat CHEN", "Rohit JAISINGH", "Wei Lek YEO"), Operator:=xlFilterValues
ActiveSheet.Range("$A$1:$AM" & lastRow).AutoFilter Field:=14, Criteria1:=chartbuilder.Sheets("ELN").Range("C5")
ActiveSheet.Range("$A$1:$AM" & lastRow).AutoFilter Field:=2, Criteria1:=chartbuilder.Sheets("ELN").Range("E1")
'After all the filters are in place, this will inform the user that there are multiple rows of data and provide an inputbox for the user to key in which row of data is preferred.
Dim RowCount As Integer
Dim Ans As Integer
'counts the number of rows that are visible, -1 to remove the header count
csv.Activate
RowCount = ActiveSheet.AutoFilter.Range.Columns(1).SpecialCells(xlCellTypeVisible).Cells.Count - 1
HANDLE1:
If RowCount > 1 Then
Ans = InputBox("For the Product Type and Ticker(s) that you have given, there are multiple similar products. Which row of data would you like to proceed with?")
If Len(Ans) = 0 Then 'Checking if Length of input is 0 characters
MsgBox "Please enter a valid number!", vbCritical
GoTo HANDLE1
Else: 'rest of code here
' Copy strike
Set copyRange = ActiveSheet.Range("R2:R" & Ans +1)
copyRange.SpecialCells(xlCellTypeVisible).Copy
chartbuilder.Activate
Set pdttype = Worksheets("ELN")
copyRange.SpecialCells(xlCellTypeVisible).Copy
pdttype.Range("C7").PasteSpecial Paste:=xlPasteValues
End If
End If
This is my contingency code when an inputbox is used. If the user keys in "2", the code should copy cells from the second row of data. But that is not working. An error pops up saying "no cells found'. How do I edit my code to copy cells from the desired filtered row based on user input?
Thank you!