+ Reply to Thread
Results 1 to 9 of 9

Call Private Function under Private Sub

  1. #1
    Registered User
    Join Date
    09-23-2018
    Location
    USA
    MS-Off Ver
    2017
    Posts
    26

    Smile Call Private Function under Private Sub

    Hello there,

    I want to run these code both at the same time but I don't have much experience with VBA. Please help

    (code1)Private Sub Worksheet_Change(ByVal Target As Range)

    lr = ActiveSheet.UsedRange.Rows.Count

    If (Target.Column = Range("Response").Column) Then

    With Application
    .EnableEvents = False
    .ScreenUpdating = False

    End With
    Code(2) Private Function IsFiltered(SheetName As Worksheet) As Boolean

    Dim lr As Long
    Dim n As Integer

    Thank you so much! To save some space, I didn't put the completed code, just the first part of it. Thanks!

  2. #2
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: Call Private Function under Private Sub

    It's not clear what you're asking. If you just want to call the IsFiltered function, then maybe something like ...

    Please Login or Register  to view this content.
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

  3. #3
    Registered User
    Join Date
    09-23-2018
    Location
    USA
    MS-Off Ver
    2017
    Posts
    26

    Re: Call Private Function under Private Sub

    Hello,

    so I have a table with 10 columns, the first code will help me to set value based on the filters. All columns got filtered will return the same values. However, if I only use the first code, even when there's no filter used, all the value will be the same. That's why I created the second code, to say that if there's no filter used, if you pick the value for one row, it will apply to only that row, not for the whole column.

    I tried both of them it worked well separately. Could you please tell me how to connect them please? Thanks so much!

  4. #4
    Registered User
    Join Date
    09-23-2018
    Location
    USA
    MS-Off Ver
    2017
    Posts
    26

    Re: Call Private Function under Private Sub

    I think what you mention about calling the IsFiltered is correct. So where and how you put that "If Is Filtered(ActiveSheet) then:??

  5. #5
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: Call Private Function under Private Sub

    I can't tell you where to put it from just seeing part of your code. I would have to see all the code.

  6. #6
    Registered User
    Join Date
    09-23-2018
    Location
    USA
    MS-Off Ver
    2017
    Posts
    26

    Re: Call Private Function under Private Sub

    1st Code
    Private Sub Worksheet_Change(ByVal Target As Range)



    lr = ActiveSheet.UsedRange.Rows.Count

    If (Target.Column = Range("Response").Column) Then

    With Application
    .EnableEvents = False
    .ScreenUpdating = False

    End With

    'Declare range you are checking for visible cells - should be dynamic
    Dim r As Range

    'Declare output range variable
    Dim visibleCells As Range

    'Set range checking against
    Set r = Range(Cells(2, Target.Column), Cells(lr, Target.Column))

    'Set visible range
    Set visibleCells = r.SpecialCells(xlCellTypeVisible)

    'Loop to apply condition to each cell that is visible

    For Each cell In visibleCells
    cell.Value = Target.Value
    Next

    With Application
    .EnableEvents = True
    .ScreenUpdating = True

    End With

    End If

    If IsFiltered(ActiveSheet) Then

    End If




    End Sub

    2nd code

    Private Function IsFiltered(SheetName As Worksheet) As Boolean

    Dim lr As Long
    Dim n As Integer

    'Count Columns
    lr = SheetName.UsedRange.Columns.Count

    'Loop through columns to check if each column is filtered
    For n = 1 To lr

    If SheetName.AutoFilter.Filters(n).On Then
    'Skip to end - your data is filtered if this is True
    GoTo FiltersOn
    Else
    End If

    Next n

    'Code to Exit Sub - your data is not filtered
    IsFiltered = False
    Exit Function

    'Code for applying IsFiltered = True when data is filtered
    FiltersOn:
    IsFiltered = True

    End Function


    Thank you!

  7. #7
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: Call Private Function under Private Sub

    Maybe something like this...

    Please Login or Register  to view this content.

  8. #8
    Registered User
    Join Date
    09-23-2018
    Location
    USA
    MS-Off Ver
    2017
    Posts
    26

    Re: Call Private Function under Private Sub

    Please Login or Register  to view this content.
    Please Login or Register  to view this content.

    I tried to follow your instruction but it didn't work, maybe I put them in the wrong position? Could you please advise and help to combine that for me please. Many thanks!

  9. #9
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: Call Private Function under Private Sub

    Just copy my code in post #7. It calls the function.

    I don't know what you want to do when the sheet is not filtered. Where is says "not filtered code goes here", add code for what you want it to do when the sheet is not filtered.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. How to call Private Sub using Application.Run
    By chuttus in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 05-15-2018, 12:49 PM
  2. Call Macro from Private Sub
    By mgolda87 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-12-2016, 12:32 PM
  3. Call private sub with variable in its name
    By Willardio in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 02-10-2013, 11:56 AM
  4. Call Private Sub from Add-In
    By Vaw08 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 01-28-2013, 02:33 PM
  5. call private sub from module
    By maacmaac in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-13-2009, 10:02 AM
  6. how to call a Private Sub ?
    By dpenny in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-12-2007, 01:15 PM
  7. [SOLVED] IF Statements-call private sub
    By CrimsonPlague29 in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 05-11-2006, 12:00 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1