VBA must run twice to work

kylemends

New Member
Joined
Apr 12, 2018
Messages
1
Hello! I've been trying to deal with this for hours and I just can't seem to find where the problem is.

First of all, the script checks if a cell range is empty or not. If it is empty, then it does something. If it is not empty, then it does another thing. The problem I have is that even though the range is empty, it must run twice for the "empty" action to work. The "not empty" always works as it should. What am I doing wrong here?

Thanks in advance!

Code:
Sub FilterIt2()

If WorksheetFunction.CountA(Sheets("Pesquisar").Range("B6:G6")) = 0 Then
    'se estiver vazia

    Sheets("Pesquisar").Range("A10:H1000").Clear
   
    Sheets("Base de Dados").Select
    Range("A1:G1000").Select
    Application.CutCopyMode = False
    Selection.Copy
    Sheets("Pesquisar").Select
    Range("B10").Select
    ActiveSheet.Paste

    Call NewTable
        
Else
    'se não estiver vazia
      
    Sheets("Pesquisar").Range("A10:H1000").Clear

    Sheets("Base de Dados").Range("A1").CurrentRegion.AdvancedFilter _
    Action:=xlFilterInPlace, _
    CriteriaRange:=Sheets("Pesquisar").Range("B5:G6"), _
    Unique:=False
    Set Rng = Sheets("Base de Dados").Range("A1").CurrentRegion.SpecialCells(xlVisible)
    
    With Sheets("Base de Dados") 'exibir todo o conteúdo da base de dados
        If .FilterMode Then
        .ShowAllData
        End If
    End With
        
    Rng.Copy
    Sheets("Pesquisar").Range("B10:H10").PasteSpecial Paste:=xlPasteAll 'definição de onde colar o conteúdo da Base de Dados
    Application.CutCopyMode = False
       
    Call NewTable
   
End If

End Sub
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
Reading this, the issue comes down to the condition of your IF statement. Your syntax looks fine the the COUNTA function, so its a matter of understanding why COUNTA is not returning 0 when you expect it to. This would imply that the cells are not truly empty (perhaps there is a space in one of the cells?). While debugging, consider ELSEIF instead of ELSE can help you, since when your COUNTA doesn't work, it goes to the second procedure by default.

Run the following and check the debug printout:

Code:
Sub check_empty()
Dim rng As Range
Dim c As Range


Set rng = Sheets("Pesquisar").Range("B6:G6")
For Each c In rng
    Debug.Print c.Address & "|" & IsEmpty(c) & "|" & c.Value & "|" & c.Formula & "|"
Next c


End Sub

This loops through each cell and tests if it is blank (T/F), and what the value or formula is (between pipes "|")
 
Upvote 0

Forum statistics

Threads
1,214,954
Messages
6,122,461
Members
449,085
Latest member
ExcelError

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top