Code adjustment required

Oraekene

New Member
Joined
Sep 20, 2022
Messages
46
Office Version
  1. 2013
Platform
  1. Windows
I tried using an AND operator with the IF script in vba but for some reason its not working. Here's the code
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:

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Do you have formulas in
Sheets("filter").cell(4, 4) AND in
Sheets("filter").cell(5, 4).Value

If so show us the formulas please.
 
Upvote 0
Do you have formulas in
Sheets("filter").cell(4, 4) AND in
Sheets("filter").cell(5, 4).Value

If so show us the formulas please.
Sheets("filter").cell(4,4).value =
Excel Formula:
=IF(AND(B6="",B8<>"",B9<>"",B7=""), B9,(IF(AND(B6="",B8<>"",B10<>"",B7=""), B10-B8, (IF(AND(B4<>"",B5<>"",B8=""), B4)))))

Sheets("filter").cell(5,4).value =
Excel Formula:
=IF(AND(B6="",B8<>"",B9<>"",B7=""), B4+B8,(IF(AND(B7="",B8<>"",B10<>"",B6=""), B10, (IF(AND(B4<>"",B5<>"",B8=""), B5)))))
 
Upvote 0
I am about to sign off.
Can you try adding the line below before the if False check and see if it is seeing what you think it is seeing.
VBA Code:
  MsgBox "Agree:- " & agree & vbTab & "Accept:= " & accept

Let me know and I will have a look tomorrow. If you are in a position to share the workbook and the above didn't help then please share the link to a shared copy.
 
Upvote 0
I am about to sign off.
Can you try adding the line below before the if False check and see if it is seeing what you think it is seeing.
VBA Code:
  MsgBox "Agree:- " & agree & vbTab & "Accept:= " & accept

Let me know and I will have a look tomorrow. If you are in a position to share the workbook and the above didn't help then please share the link to a shared copy.
Thank you for the response! I'll test out the link and give feedback on how it works. Here is the data file Datasheet: Matrix Infinite
 
Upvote 0
I don't believe the issue is with the code. Please start with running your formulas in D4 and D5 through various scenarios and make sure it is doing what you are expecting.
I don't know anything about the intent of the formulas but it seems to me that it is returning false in most cases and the code is is branching to agree and accept option accordingly.
 
Upvote 0
Solution
I don't believe the issue is with the code. Please start with running your formulas in D4 and D5 through various scenarios and make sure it is doing what you are expecting.
I don't know anything about the intent of the formulas but it seems to me that it is returning false in most cases and the code is is branching to agree and accept option accordingly.
Sorry for the late response: the formulas work, i changed the cells of the formulas and for some reason they started working. No idea why. Again, i'm so sorry for the late response
 
Upvote 0

Forum statistics

Threads
1,215,039
Messages
6,122,799
Members
449,095
Latest member
m_smith_solihull

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