Please Tell me what is wrong with this Code

Siyanna

Well-known Member
Joined
Nov 7, 2011
Messages
1,146
Please tell me where i am going wrong. Thank You So Much

Code:
Sub UpdateTotal()
'Update Scores
Application.ScreenUpdating = False
Dim Sh As Worksheet
Dim Lrow As Long
Dim Rng As Range
Dim Lcol As Long
Set Sh = Sheets("Table")
Lrow = Sheets("Result").Range("B" & Rows.Count).End(xlUp).Row
Set Rng = Range("B2:B" & Lrow)
Lcol = Sh.Cells(2, Columns.Count).End(xlToLeft).Column + 1
For Each WS In Worksheets
    If WS.Name <> "Table" And WS.Name <> "Result" Then
        [COLOR=#ff0000]With Range(Rng)
          .Offset(0, 1).Formula = "=IF(AND(B2=Result!B2,D2=Result!D2),3,IF(AND(B2=D2,Result!B2=Result!D2),1,IF(OR(AND(D2>B2,Result!D2>Result!B2),AND(B2>D2,Result!B2>Result!D2)),1,0)))"
          .Value = .Value
[/COLOR]       End With
    End If
Next WS
'Update Summary Sheet
With Sh.Range(Cells(2, Lcol), Cells(27, Lcol))
   .Formula = "=INDIRECT(""'""&A2&""'!""&""F""&MATCH(1E+100,Result!F:F))"
   .Value = .Value
End With
Application.ScreenUpdating = True
End Sub
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
try
Code:
Sub UpdateTotal()
'Update Scores
Application.ScreenUpdating = False
Dim Sh As Worksheet
Dim Lrow As Long, Rng As Range, Lcol As Long, ws As Worksheet
Set Sh = Sheets("Table")
Lrow = Sheets("Result").Range("B" & Rows.Count).End(xlUp).Row
Set Rng = Range("B2:B" & Lrow)
Lcol = Sh.Cells(2, Columns.Count).End(xlToLeft).Column + 1
For Each ws In Worksheets
    If ws.Name <> "Table" And ws.Name <> "Result" Then
        With Rng
          .Offset(0, 1).Formula = "=IF(AND(B2=Result!B2,D2=Result!D2),3,IF(AND(B2=D2,Result!B2=Result!D2),1,IF(OR(AND(D2>B2,Result!D2>Result!B2),AND(B2>D2,Result!B2>Result!D2)),1,0)))"
          .Value = .Value
       End With
    End If
Next ws
'Update Summary Sheet
With Sh.Range(Cells(2, Lcol), Cells(27, Lcol))
   .Formula = "=INDIRECT(""'""&A2&""'!""&""F""&MATCH(1E+100,Result!F:F))"
   .Value = .Value
End With
Application.ScreenUpdating = True
End Sub
 
Upvote 0
Please tell me where i am going wrong. Thank You So Much

Code:
Sub UpdateTotal()
'Update Scores
Application.ScreenUpdating = False
Dim Sh As Worksheet
Dim Lrow As Long
Dim Rng As Range
Dim Lcol As Long
Set Sh = Sheets("Table")
Lrow = Sheets("Result").Range("B" & Rows.Count).End(xlUp).Row
Set Rng = Range("B2:B" & Lrow)
Lcol = Sh.Cells(2, Columns.Count).End(xlToLeft).Column + 1
For Each WS In Worksheets
    If WS.Name <> "Table" And WS.Name <> "Result" Then
        [COLOR=#ff0000]With Range(Rng)
          .Offset(0, 1).Formula = "=IF(AND(B2=Result!B2,D2=Result!D2),3,IF(AND(B2=D2,Result!B2=Result!D2),1,IF(OR(AND(D2>B2,Result!D2>Result!B2),AND(B2>D2,Result!B2>Result!D2)),1,0)))"
          .Value = .Value
[/COLOR]      End With
    End If
Next WS
'Update Summary Sheet
With Sh.Range(Cells(2, Lcol), Cells(27, Lcol))
   .Formula = "=INDIRECT(""'""&A2&""'!""&""F""&MATCH(1E+100,Result!F:F))"
   .Value = .Value
End With
Application.ScreenUpdating = True
End Sub

Thank You

For some reason it is not skipping over the sheets that i have tried to eliminate with the If statement.
 
Upvote 0
Thank You

For some reason it is not skipping over the sheets that i have tried to eliminate with the If statement.

Hi Mahmed1

Is there something similiar you are dealing with?

I have tried to activate the sheet but i guess Michael will be able to help better than me.

Thanks
 
Upvote 0
Hi Michael

Why is it not skipping over the sheets when trying to input the formula?

Please advise where i am going wrong

Lrow = Sheets("Result").Range("A" & Rows.Count).End(xlUp).Row
Set Rng = Range("A2:A" & Lrow)

WS.Activate
With Rng
.Offset(0, 5).Formula = "=IF(AND(Result!B2="""",Result!D2=""""),"""",IF(AND(Result!B2=""P"",Result!D2=""P""),""P"",IF(AND(B2=Result!B2,D2=Result!D2),3,IF(AND(B2=D2,Result!B2=Result!D2),1,IF(OR(AND(D2>B2,Result!D2>Result!B2),AND(B2>D2,Result!B2>Result!D2)),1,0)))))"
.Value = .Value
End With

Thank You
 
Upvote 0
Try activating the WS
Rich (BB code):
Sub UpdateTotal()
'Update Scores
Application.ScreenUpdating = False
Dim Sh As Worksheet
Dim Lrow As Long, Rng As Range, Lcol As Long, ws As Worksheet
Set Sh = Sheets("Table")
Lrow = Sheets("Result").Range("B" & Rows.Count).End(xlUp).Row
Set Rng = Range("B2:B" & Lrow)
Lcol = Sh.Cells(2, Columns.Count).End(xlToLeft).Column + 1
For Each ws In Worksheets
    If ws.Name <> "Table" And ws.Name <> "Result" Then
        ws.Activate
        With Rng
          .Offset(0, 1).Formula = "=IF(AND(B2=Result!B2,D2=Result!D2),3,IF(AND(B2=D2,Result!B2=Result!D2),1,IF(OR(AND(D2>B2,Result!D2>Result!B2),AND(B2>D2,Result!B2>Result!D2)),1,0)))"
          .Value = .Value
       End With
    End If
Next ws
'Update Summary Sheet
With Sh.Range(Cells(2, Lcol), Cells(27, Lcol))
   .Formula = "=INDIRECT(""'""&A2&""'!""&""F""&MATCH(1E+100,Result!F:F))"
   .Value = .Value
End With
Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,817
Messages
6,121,717
Members
449,050
Latest member
MiguekHeka

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