+ Reply to Thread
Results 1 to 20 of 20

VBA AutoFilter does not Work in run mode, only in debug mode

  1. #1
    Registered User
    Join Date
    11-17-2015
    Location
    Brazil
    MS-Off Ver
    Excel 2010
    Posts
    7

    VBA AutoFilter does not Work in run mode, only in debug mode

    Hi everyone!

    I have had such a headache with this. I have a very long VBA code that works with around 7 workbooks, it goes perfectly well, until I try to filter one of the sheets using a command I use constantly:

    Function CriarArquivos(operation As String, Concessionaria As String, PlanilhaBase As String) As Boolean
    Dim countLines As Long
    Dim countFiltro As Integer

    'Activate the correct Workbook and Sheet
    Workbooks(PlanilhaBase).Sheets(1).Activate
    'countLines gets the total number of rows with data in the file
    countLines = Range("A:A").Cells.SpecialCells(xlCellTypeConstants).Count

    Range("A1:J1").Select

    ' Filter by Concessionaria
    ActiveSheet.Range("$A$1:$J" & countLines).AutoFilter Field:=5, Criteria1:="" & Concessionaria

    ' Defines countFiltro as the total number of rows for the filtered list
    countFiltro = ActiveSheet.AutoFilter.Range.Columns(1).SpecialCells(xlCellTypeVisible).Count

    ' Start the code if at least one item was found.
    If countFiltro > 1 Then


    The variables:
    countFiltro which should return the number of rows filtered by the Autofilter but it is returning always 1

    The Activesheet, after the Macro has run, does not seem to be filtered at all

    Funny thing is that it works perfectly if I go into debug mode and keep F8 pressed.

    I am trying to debug this using Debug.Print. The conditional statement gets always FALSE in this way.

    I have turned internet upside down to find solutions, I have already tried to:
    Add DoEvents in case it was a processing issue,
    Tried to copy the whole code and data to another file in case the original one was corrupt.
    Removed all the WITH statements from the whole code, since it has been an issue found online...
    Restarted Excel
    Restarted the Computer

    Nothing works...

    Any thoughts ?

  2. #2
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,905

    Re: VBA AutoFilter does not Work in run mode, only in debug mode

    Hi, welcome to the forum.

    Have you tried recording a new macro to do what you're trying to achieve?
    I suggest you do that.

    After recording remove the filter and run that macro again and see if it works.

    Then all you all need to do is edit that macro to suit your needs.

    Boa sorte
    ---
    Hans
    "IT" Always crosses your path!
    May the (vba) code be with you... if it isn't; start debugging!
    If you like my answer, Click the * below to say thank-you

  3. #3
    Registered User
    Join Date
    11-17-2015
    Location
    Brazil
    MS-Off Ver
    Excel 2010
    Posts
    7

    Re: VBA AutoFilter does not Work in run mode, only in debug mode

    Thanks for the answer Keebellah,

    I did try that ... it did not work. =/

  4. #4
    Forum Contributor
    Join Date
    06-04-2013
    Location
    Moscow
    MS-Off Ver
    Office 365
    Posts
    100

    Re: VBA AutoFilter does not Work in run mode, only in debug mode

    Try to use somthing like this(without activation of objects):
    Please Login or Register  to view this content.
    I'm sorry my english...

  5. #5
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,905

    Re: VBA AutoFilter does not Work in run mode, only in debug mode

    @Aganove: could you paste the recorded macro code here?
    I suggest you use the [ code ] [ /code ] to identify the code correctly.

    It will show like this

    Please Login or Register  to view this content.
    I cannot use you sample becasue I have no idea what the data looks like.
    And when you say 'It does not work' what is the error or problem?
    If you cannot explain it in English explain it in Portuguese não tenho nenhum problema para perceber.

  6. #6
    Registered User
    Join Date
    11-17-2015
    Location
    Brazil
    MS-Off Ver
    Excel 2010
    Posts
    7

    Re: VBA AutoFilter does not Work in run mode, only in debug mode

    Hit The_Prist,

    Thanks very much, I tried as suggested, but now the variables countLines and countFilter are not retrieving the expected values...

    countLines is always returning 98, not sure why.
    countFiltro still returning 1

    Any ideas ?

    Regards,

    Aganove

  7. #7
    Registered User
    Join Date
    11-17-2015
    Location
    Brazil
    MS-Off Ver
    Excel 2010
    Posts
    7

    Re: VBA AutoFilter does not Work in run mode, only in debug mode

    Thanks for the lesson Keeellah
    Please Login or Register  to view this content.

  8. #8
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,905

    Re: VBA AutoFilter does not Work in run mode, only in debug mode

    But does it work now?

  9. #9
    Forum Contributor
    Join Date
    06-04-2013
    Location
    Moscow
    MS-Off Ver
    Office 365
    Posts
    100

    Re: VBA AutoFilter does not Work in run mode, only in debug mode

    attached the file wth macro.

  10. #10
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,905

    Re: VBA AutoFilter does not Work in run mode, only in debug mode

    @The_Prist: I think you forgot the file

  11. #11
    Forum Contributor
    Join Date
    06-04-2013
    Location
    Moscow
    MS-Off Ver
    Office 365
    Posts
    100

    Re: VBA AutoFilter does not Work in run mode, only in debug mode

    Quote Originally Posted by Keebellah View Post
    @The_Prist: I think you forgot the file
    Not. I wanted to ask the author to attach his file with macro See my signature
    Last edited by The_Prist; 11-18-2015 at 10:12 AM.

  12. #12
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,905

    Re: VBA AutoFilter does not Work in run mode, only in debug mode

    That's what you get when you get your grammar wrong
    Attach file with macro instead of attached file with macro

  13. #13
    Registered User
    Join Date
    11-17-2015
    Location
    Brazil
    MS-Off Ver
    Excel 2010
    Posts
    7

    Re: VBA AutoFilter does not Work in run mode, only in debug mode

    Please find attached the files.

    Test.xlsmTest2.xlsx

  14. #14
    Forum Contributor
    Join Date
    06-04-2013
    Location
    Moscow
    MS-Off Ver
    Office 365
    Posts
    100

    Re: VBA AutoFilter does not Work in run mode, only in debug mode

    Why you save the workbook(Concessionaria & ".xlsx") only in this block
    Please Login or Register  to view this content.
    but you trying to use name "Concessionaria & ".xlsx" after this block. Her not exist after this block - workbook has the default name(Book).

    I'm changed the code, but logic of this block i'm does not changed
    Attached Files Attached Files

  15. #15
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,905

    Re: VBA AutoFilter does not Work in run mode, only in debug mode

    I don't know what all this is doing but I had to make some small changes.

    I get the error with the images because I do not have that link, but the copying and pasting ...
    I assume its the filtered data form Test2 that you want pasted? True?

  16. #16
    Forum Contributor
    Join Date
    06-04-2013
    Location
    Moscow
    MS-Off Ver
    Office 365
    Posts
    100

    Re: VBA AutoFilter does not Work in run mode, only in debug mode

    Yes, i think.

  17. #17
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,905

    Re: VBA AutoFilter does not Work in run mode, only in debug mode

    Well, I played around. One 'concessionaria' gets created and if you step through it ti works, but if you let it run it somehow quits.
    I've attached my version of the Test and two results, 2nd one is incorrect though
    Attached Files Attached Files

  18. #18
    Registered User
    Join Date
    11-17-2015
    Location
    Brazil
    MS-Off Ver
    Excel 2010
    Posts
    7

    Re: VBA AutoFilter does not Work in run mode, only in debug mode

    The Prist:

    The way the system is designed, I there is a central sheet that triggers some standard procedures, one of these is this CriarArquivos. That's why there is this IF BLOCK>

    This procedure works this way:
    It filters the source, test2.xls according to the Concessionaria

    It creates a new workbook and saves it to the Concessionaria name
    It copies the result of the filter to this concessionaria sheet and saves it again and close it

    Then it loops to another concessionaria.

    Somehow it only works in debug mode...

  19. #19
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,905

    Re: VBA AutoFilter does not Work in run mode, only in debug mode

    Yes, I noticed that too.
    I don't have the time right now but I think the problem is that the autofilter is set and before the next one can be set it must be cleared and before the next filter is set.
    If you do it in debug mode or manually it works, but I guess when running it goes too fast and it skips.
    Also there is check if the target file already exists or set display alerts to false and overwrite an existing file.
    The OP has a lot of homework still to do

  20. #20
    Registered User
    Join Date
    11-17-2015
    Location
    Brazil
    MS-Off Ver
    Excel 2010
    Posts
    7

    Re: VBA AutoFilter does not Work in run mode, only in debug mode

    Interesting, isn't it ? I have other filters that are kept unchanged, so I took them out of the loop for performance and that's why the sheet is already loaded with some filters applied. I had to made this sample for you, so it could fit in the forum size, but the real sheet has around 13000 rows...

    I have done similar procedures many times without problem. I have also tried to add DoEvents after every sheet load and manipulation like workbook open, close, save, and filter application....

+ 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. [SOLVED] Code runs in debug mode only.
    By Kramxel in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-20-2015, 10:42 AM
  2. VBA Code Runs in Debug Mode But Returns Type Mismatch Error Outside Debug Mode
    By valerie.k.chiang in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 01-24-2014, 03:48 PM
  3. [SOLVED] Code skips on Run Mode but runs fine on F8 (Debug Mode)
    By RaquelAR in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 11-25-2013, 01:05 PM
  4. VBA code only works correctly in Debug mode
    By tpthatsme in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 02-09-2012, 12:51 PM
  5. Macro Works but not in Debug Step mode
    By Bob Smedley in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-31-2006, 12:35 PM
  6. Show value of variable on mouse-over in debug mode - bug?
    By Chet Shannon in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 01-09-2006, 04:55 PM
  7. Get in DEBUG mode on workbook open
    By Anthony Fok in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 07-28-2005, 01:30 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