3

I have looked through other answers but it appears that my problems are addressed only partially, as the issues are across multiple issues.


BACKGROUND

Every month, a workbook is opened with links to external data and reports are run for each site. The file updates as each site is accessed from a drop down list. After the report is generated, each site has a filter applied, based on their particular contractual requirements: the filters are is separate columns, with cells either "" or "y".


ACTIONS TO DATE

I have built some code to loop through the drop down list (in cell "H1") and save the resulting worksheets to a new, separate location, under the name of the Site location in cell "H1". I have code to filter, based on the Site location in cell "H1". I have also written code to ask the user to pick the Save To folder location.


PROBLEM(S)

  1. The folder path generated by the dialog box FileDialog(msoFileDialogFolderPicker) only works for the first "For Each" loop.
  2. I do not know where to put the AutoFilter code into the "For Each" loop.

I think that #1 is due to the Folder path generated being a variable and I need to be able to declare it as a constant....?

For the AutoFilter, again, I think that the problem is accessing the name from the DDL whilst in the For Each loop: no idea how to solve that one.

I might be using the wrong approach to achieving the outcome I want or it may just be a small tweak that is needed.

The following code works, as in the file is saved to the selected Folder, for the first item but then attempts to save every subsequent file as "FALSE" but no filter is applied.

I have tried Calling the Sub FilterRows_SiteSpecific from within the For Each loop but it makes no difference to the output, the AutoFilter does not work.

Any help gratefully received.


Sub SelectFolderANDLoop()

    Application.ScreenUpdating = False

Dim sFolder As String
    ' Open the select folder prompt
    With Application.FileDialog(msoFileDialogFolderPicker)
    'This sets the starting point for Folder selection as "T:\Business Information" etc

        If .Show = -1 Then ' if OK is pressed
            sFolder = .SelectedItems(1)
        End If
    End With

    If sFolder <> "" Then ' if a file was chosen
        ' *********************

    Dim rngListSelection As Range

    Dim strListSelection As String
    Dim intColumn As Integer


    'Looks up each site from drop down lilst in cell H1

    For Each rngListSelection In Range(Range("H1").Validation.Formula1)
        Range("H1").Value = rngListSelection
        ' Copy tabs "Site Name" and "Data"
        Worksheets(Array("Site Name", "Data")).Copy
        ' Clears the entry for the site name
        Range("H1").Validation.Delete
        ' Copies the 2 worksheets to a new workbook
        ActiveWorkbook.SaveAs sFolder & "\" rngListSelection.Value & ".xlsx", xlOpenXMLWorkbook


        ActiveWorkbook.Close
    Next rngListSelection

    Application.ScreenUpdating = True
        ' *********************
    End If
End Sub




Sub FilterRows_SiteSpecific()

' VLookupForAutoFilter Macro
' Set VLookup calculation output as Integer variable

Dim intColumn As Integer

' Calculation must start on woksheet "Site Name" and looks up Site name in cell "H1"

On Error Resume Next
intColumn = Application.WorksheetFunction.VLookup(ActiveSheet.Range("H1"), _
Worksheets("Data").Range("B125:E145"), 4, False)

On Error GoTo 0
' If the site name is not found in the VLookup table, create error message

        Debug.Print intColumn

If intColumn = 0 Then
    MsgBox "The unit name is mistyped as a match was not found"
Else
End If

' The above was to create a variable to provide the column number_
' to use in the Autofilter function below

With Worksheets("Site Name").Range("A2:r149")
' Filters to exclude "Info Only" in "Target" column
    .AutoFilter field:=11, Criteria1:="<>Info Only"
' Filter to include "y" in column from VLOOKUP
    .AutoFilter field:=intColumn, Criteria1:="y"
With Worksheets("Site Name").Range("A8:r149")
' Filters to exclude "n/a" in "Actual" column
    .AutoFilter field:=10, Criteria1:="<>n/a"


End With
End With
End Sub
Cyril
  • 6,448
  • 1
  • 18
  • 31
chriswheel
  • 31
  • 1
  • You already have stored the user-selected path in the variable `sFolder` and I don't see that you modify that variable anywhere, so it shouldn't be changing. It does not need to be a constant. I don't think you need the `.Value` appended to the `rngListSelection`, the variable is a `Variant` and should hold the value assigned as the loop progresses. But the real issue is to find out what is changing the value of `sFolder`. Try dropping in some `Debug.Print` statements to check it along the way. Alternatively, you can add a "Watch" to `sFolder` and break when it changes. – PeterT Apr 25 '19 at 13:54
  • Thank you for the swift response PeterT, I will check what happens when I take out the .value and introduce some Debug.Print statements and let you know. – chriswheel Apr 25 '19 at 14:58
  • With the current line ActiveWorkbook.SaveAs sFolder & "\" rngListSelection.Value & ".xlsx", xlOpenXMLWorkbook the pop up message box informs me that there is a compile error: Syntax error. Any suggestions to resolve this? – chriswheel Apr 25 '19 at 15:07
  • Yes, remove the `.Value` from the `rngListSelection.Value`. It's not needed – PeterT Apr 25 '19 at 15:11
  • Thank you PeterT....still have the error message "Syntax error" for that .SaveAs line....grrr – chriswheel Apr 25 '19 at 15:19
  • You're missing a `&` after the `"\"` on that line. – PeterT Apr 25 '19 at 17:37
  • There is also a problem in how you're trying to iterate over the validation list. See [this answer](https://stackoverflow.com/a/30603284/4717755). Also, by using `Range("H1").Validation.Delete` you're not clearing the entered data, you're removing ALL data validation from that cell. – PeterT Apr 25 '19 at 17:50
  • Thank you PeterT, I will look at this tomorrow at work and let you know how I get on - I don't have the file "with me" :-) – chriswheel Apr 25 '19 at 18:23
  • @PeterT Does the suggested link also allow me ot perform AutoFilter using the current value in the drop down list (cell "H1") too?(https://stackoverflow.com/questions/30603004/iterate-through-an-excel-dropdown-validation-list/30603284#30603284) – chriswheel Apr 25 '19 at 18:25
  • That link shows you how to get the list of validation (filter) values from cell H1. You'll have to that value in your other filter Sub then. My suggestion is to send the filter value (from H1, with each loop iteration) as a parameter to your filter Sub. – PeterT Apr 25 '19 at 20:21

0 Answers0