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
 

Some videos you may like

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.

gmhumphr

New Member
Joined
Mar 26, 2018
Messages
45
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 "|")
 

Watch MrExcel Video

Forum statistics

Threads
1,109,336
Messages
5,528,101
Members
409,802
Latest member
joeino

This Week's Hot Topics

  • Change military grades into rank
    Afternoon all Need help with formula that will change military rank (i.e. 1, 2, 3 into Amn, A1C, SrA). Running IF formula that does not work...
  • VBA COUNTIF SOLUTION
    Hi The following are the errors spread across the several columns from E to Q ie. 13 columns across several sheets with more than 500 rows per...
  • INSERT ROW WITH SPECIFIS TEXT IN A COLUMN
    Hi All! How can identify that that the row to be inserted has to be inserted before 1st row with specific text in column F. If I record the...
  • Auto-Create a monthly Sign in sheet for preschool students
    The image below is what each page looks like. Above is space for the "Child Name" "Month" "Class" School days are obviously Monday-Friday but...
  • VBA vlookup multiple results
    Hi folks, Hopefully someone out there can help. I have a list to vlookup which works (ish). the lookup only picks up the first instance of the...
  • Extract values for earliest/latest times
    I am trying to put together a formula to get the earliest start time, the latest end time from column A for each person in Column B-F without the...
Top