Excel VBA Advanced Filter Problem

Asked

Viewed 357 times

-1

I made a program in VBA to calculate the cost in my company. For this I used two connections with the Oracle database. What’s strange is the fact that when I try to use the advanced filter manually, it works. However, when I try to use the VBA (written through macro using the filter manually), it does not work.

Sub custo()

'atualizacao da data
For Each c In Worksheets("BD2").Range("B1:B152")
 If c.Value <> "MOV_DATMOV" Then
 c.Value = ">" & DateAdd("yyyy", -1, Date)
 Else
 c.Value = "MOV_DATMOV"
 End If
Next c

For Each d In Worksheets("BD2").Range("C1:C152")
 If d.Value <> "MOV_DATMOV" Then
 d.Value = "<" & Date
 Else
 d.Value = "MOV_DATMOV"
 End If
Next d

'filtro de todas as despesas
'desp 001
Sheets("BD1").Columns("J:T").AdvancedFilter Action:=xlFilterCopy, _
        CriteriaRange:=Sheets("BD2").Range("A1:C2"), CopyToRange:=Sheets("CP").Range("A2"), _
        Unique:=False

The spreadsheets I use in loops are:

inserir a descrição da imagem aqui

inserir a descrição da imagem aqui

inserir a descrição da imagem aqui

  • Sorry, I made a huge mistake of thinking I had posted in the international. I have already done the translation

1 answer

0

Do a test after changing the range containing the criteria for the data source sheet:

change this line

CriteriaRange:=Sheets("BD2").Range("A1:C2")

for

CriteriaRange:=Sheets("BD1").Range("A1:C2")

Still, if you want to paste the filtered data into the spreadsheet BD2

alter

CopyToRange:=Range("A2")

for

CopyToRange:=Sheets("BD2").Range("A2")

If it doesn’t work I suggest you provide a sample of the data and desired result.

  • Thanks for the quick reply, I want to copy the result in the "CP" spreadsheet, just the code below: CopyToRange:=Sheets("BD2").Range("A2") would actually be: CopyToRange:=Sheets("CP").Range("A2") However, this change did not solve the problem. The other suggestion made the filter work, suggesting that the problem is in the "BD2" spreadsheet. My guess would be that the function date is causing some problem with cell values, perhaps leaving all null. However I have no idea how to solve.

  • 1

    I managed to find the solution by exchanging: c.Value = ">" & DateAdd("yyyy", -1, Date) to c.Value = DateAdd("yyyy", -1, Date)&#xA; c.Value = ">" & c.Value Something to do with the difference between our date system and the American system that VBA uses Thanks for the help

Browser other questions tagged

You are not signed in. Login or sign up in order to post.