I tried using an AND operator with the IF script in vba but for some reason its not working. Here's the code
The conditions are TRUE or FALSE based on the value of linked cells ('agree' and 'accept'). If the cells don't have numbers it should be FALSE and fill in C4 and C5, if the cells have numbers they should be TRUE and fill in D4 and D5. But for some reason it just doesn't work, no matter the condition it only fill in C4 and C5. Tried everything i've seen and still not working, i don't know what to do
VBA Code:
Sub somethingiswrong()
Application.ScreenUpdating = False 'send this to the beginning of the sheet, along with other macro optimization techniques
Dim ws As Worksheet
'set dynamic range
lRow = Range("A1048576").End(xlUp).Row
lCol = Range("XFD" & lRow).End(xlToLeft).Column
On Error Resume Next
Sheets("analysis").ShowAllData
On Error GoTo 0
Sheets("analysis").Cells.Clear
'sort range
For Each ws In ThisWorkbook.Worksheets
If ws.Name = "filter" Or ws.Name = "analysis" Or ws.Name = "analysis2" Or ws.Name = "report" Or ws.Name = "presentation" Then
Else
On Error Resume Next
ws.ShowAllData
ws.Rows.Hidden = False
ws.Sort.SortFields.Clear
ws.Range("A:F").CurrentRegion.Sort Key1:=ws.Range(Sheets("filter").Range("B11").Value), Order1:=xlAscending, Header:=xlYes
'set advanced filter criteria
Sheets("filter").Range("B10") = ws.Cells(Rows.Count, Sheets("filter").Range("B11").Value).End(xlUp).Value
fltr = Sheets("filter").Range("B13").Value
agree = Sheets("filter").cell(4, 4).Value
accept = Sheets("filter").cell(5, 4).Value
Sheets("filter").Range("G2:G").Cells.Clear 'time and date columns in the filter sheet are fixed. all other filter criteria columns come after them
Sheets("filter").Range("H2:H").Cells.Clear 'their headers though can be changed to match the header title of any column on the data sheets whose column hold time and-or date values
If agree = false And accept = false Then
Sheets("filter").Cells(2, 7).Resize(Sheets("filter").Columns(fltr).SpecialCells(2).Count - 1).Value = ">=" & Sheets("filter").Range("C4").Value
Sheets("filter").Cells(2, 8).Resize(Sheets("filter").Columns(fltr).SpecialCells(2).Count - 1).Value = "<=" & Sheets("filter").Range("C5").Value
Else
Sheets("filter").Cells(2, 7).Resize(Sheets("filter").Columns(fltr).SpecialCells(2).Count - 1).Value = ">=" & Sheets("filter").Range("D4").Value
Sheets("filter").Cells(2, 8).Resize(Sheets("filter").Columns(fltr).SpecialCells(2).Count - 1).Value = "<=" & Sheets("filter").Range("D5").Value
End If
'filter sheets
ws.Range("A:F").AdvancedFilter xlFilterInPlace, criteriarange:=Sheets("filter").Range("G1:I3")
'copy the visible filtered cells into the analysis1 sheet
ws.Range("D:D").SpecialCells(xlCellTypeVisible).Copy Destination:=Sheets("analysis").Range("XFD" & lRow).End(xlToLeft).Offset(0, 1)
On Error GoTo 0
End If
Next ws
Application.ScreenUpdating = True
End Sub
The conditions are TRUE or FALSE based on the value of linked cells ('agree' and 'accept'). If the cells don't have numbers it should be FALSE and fill in C4 and C5, if the cells have numbers they should be TRUE and fill in D4 and D5. But for some reason it just doesn't work, no matter the condition it only fill in C4 and C5. Tried everything i've seen and still not working, i don't know what to do
Last edited by a moderator: