Macro makes mistakes when ScreenUpdating etc. = False.

Charleton

New Member
Joined
Aug 30, 2017
Messages
23
Hello,

I have a .xlsm - and if I freeze the screen with code, it makes errors. Everything is run from sheet "Mainscreen".


My issue is it seems by freezing the screen, it does not give me all data. Only 2 of 8 rows it would give me without screen freezing, at most. It works perfectly without issue unless I try to freeze the screen. (This is an isolated part of a much larger project, and everything has been tested ALOT, - I can confirm the data, and the results. I have run it isolated, and in concert, and both ways, it works perfectly until this code is active in this module, or it is called by a subroutine with this code in it.)

Specifically, I have a variant number of rows on a sheet "NOC" with an array of data in columns B:K - B is the index column for my reports.

I need to copy all ranges B:K on this worksheet "NOC" and paste them starting with Row B12 on Worksheet "P5A" Worksheet , which will generally have more than 100 and less than 10000 rows. However, it would be nigh impossible for there to be 500 rows on the same "P5A" form. I also need to get the values from range "L:O" that row in Worksheet "NOC" into the range B5:B8 on the "P5A" worksheet. - This will always be the same for any rows with the same value in Column B, so it only needs to happen before the file is saved. I then need to save a copy of the P5A form into a folder, and blank out the P5A form and do the next one.

My Code works, until it doesn't - for some reason, when I try to add (and later remove, of course) the code to not make the screen change, it fails to capture all lines. It only catches two of eight lines for one value in column B. If I error out this code, it works flawlessly.

Everything works fine until I add in the true and false Application display and screen updating etc.

I would appreciate any help in troubleshooting this annoyance, or helping me streamline my code.


Code:
Public Sub PRODUCE_P5A()
 

'This Code messes up data accuracy???
'Application.DisplayAlerts = False
'Application.ScreenUpdating = False
'On Error Resume Next

'----- Works from the "Mainscreen" unless screen is not updating
 
‘Declare Variables
Dim StrINDEX As String ' Column B in all sheets being used
Dim P5ALastRow As Long ' To find last row where Column B has a value in sheet P5A
Dim NOCLastRow As Long ' To find last row where Column B has a value in sheet NOC
Dim i As Integer ' Count for For Next
 
‘Sort NOC worksheet by INDEX – Column B
    ActiveWorkbook.Worksheets("NOC").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("NOC").Sort.SortFields.Add Key:=Range( _
        "B2:B59571"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortNormal
    With ActiveWorkbook.Worksheets("NOC").Sort
        .SetRange Range("A1:O59571")
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With

'Ready Worksheet "P5A"
Worksheets("P5A").Range("B12:K10000").ClearContents
Worksheets("P5A").Range("B5:B8").ClearContents
 
‘------Create P5A NOC forms------
 
Sheets("NOC").Activate
Cells(2, 2).Select
 
StrINDEX = ActiveCell.Value
 
NOCLastRow = Worksheets("NOC").Cells(Rows.Count, 2).End(xlUp).Row ' Find Bottom Row
P5ALastRow = Worksheets(“P5A”).Cells(Rows.Count, 2).End(xlUp).Row ' Find Bottom Row
 
For i = 2 To NOCLastRow + 1
 
P5ALastRow = Worksheets("P5A").Cells(Rows.Count, 2).End(xlUp).Row ' Find Bottom Row
 
    If Cells(i, 2) = StrINDEX Then
                Range(Cells(i, 2), Cells(i, 11)).Copy
                 Worksheets("P5A").Range("B" & P5ALastRow + 1).PasteSpecial xlPasteValues
   
    Else
   
                Worksheets("P5A").Range("B5") = Worksheets("NOC").Range("L" & i)
                Worksheets("P5A").Range("B6") = Worksheets("NOC").Range("M" & i)
                Worksheets("P5A").Range("B7") = Worksheets("NOC").Range("N" & i)
                Worksheets("P5A").Range("B8") = Worksheets("NOC").Range("O" & i)
   
                Sheets(P5A).Select
                Application.CutCopyMode = False
   
                Sheets(P5A).Copy
                 ChDir "C:\OPS\P5A"
                 ActiveWorkbook.SaveAs Filename:="C:\OPS\P5A\" & StrINDEX & "-NOC.xlsx", FileFormat:= _
                xlOpenXMLWorkbook, CreateBackup:=False, ReadOnlyRecommended:=True
                ActiveWorkbook.Close
   
                Workbooks("WORKBOOK.xlsm").Activate
                Worksheets("P5A").Range("B12:K1000").ClearContents
                Worksheets("P5A").Range("B5:B8").ClearContents
                Worksheets("NOC").Activate
                Cells(i, 2).Select
                 StrINDEX = ActiveCell.Value
                Range(Cells(i, 2), Cells(i, 11)).Copy
                 Worksheets(“P5A”).Range("B12").PasteSpecial xlPasteValues
   
    End If
   
Next i
 
 
 
   
    End If
   
Next i
 
 
Sheets("Mainscreen").Activate

'----- End of Working Code
 

'This Code messes up data accuracy???
'Application.DisplayAlerts = True
'Application.ScreenUpdating = True
'On Error Resume Next
 
 
 
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.
The problem is more likely caused by unqualified references and the use of Activate/Select both of which can have an impact on which sheet/range/workbook the code is executed on.

For example, which worksheet is Cells(i, 2) referring to here?
Code:
 If Cells(i, 2) = StrINDEX Then
 
Last edited:
Upvote 0
This
Everything is run from sheet "Mainscreen".
is not correct in that you have
Code:
Sheets("NOC").Activate
everything after that line is run from the NOC sheet unless otherwise specified.
 
Upvote 0
That is what the line before the For Next Loop establishes:

Sheets("NOC").Activate

The code executes accurately and perfectly as long as the screen updating is True. I need it to be false.


The problem is more likely caused by unqualified references and the use of Activate/Select both of which can have an impact on which sheet/range/workbook the code is executed on.

For example, which worksheet is Cells(i, 2) referring to here?
Code:
 If Cells(i, 2) = StrINDEX Then
 
Upvote 0
This Macro is called from a Button on the sheet "Mainscreen" - this is why it has the Sheets("Mainscreen").Activate line right before screen updating would come back on.

I am under the impression I need to activate other sheets to get this done, but it that not so? Thanks!

This is not correct in that you have
Code:
Sheets("NOC").Activate
everything after that line is run from the NOC sheet unless otherwise specified.
 
Upvote 0
You very rarely need to active or select anything. You can do it something like
Code:
With Sheets("NOC")
   StrINDEX = .Cells(2, 2).Value
   
   NOCLastRow = Worksheets("NOC").Cells(Rows.Count, 2).End(xlUp).Row ' Find Bottom Row
   P5ALastRow = Worksheets("P5A").Cells(Rows.Count, 2).End(xlUp).Row ' Find Bottom Row
   
   For i = 2 To NOCLastRow + 1
      
      P5ALastRow = Worksheets("P5A").Cells(Rows.Count, 2).End(xlUp).Row ' Find Bottom Row
      
      If .Cells(i, 2) = StrINDEX Then
         .Range(.Cells(i, 2), .Cells(i, 11)).Copy
         Worksheets("P5A").Range("B" & P5ALastRow + 1).PasteSpecial xlPasteValues
      
      Else
      
         Worksheets("P5A").Range("B5") = .Range("L" & i)
         Worksheets("P5A").Range("B6") = .Range("M" & i)
         Worksheets("P5A").Range("B7") = .Range("N" & i)
         Worksheets("P5A").Range("B8") = .Range("O" & i)
         
         Application.CutCopyMode = False
         
         Sheets("P5A").Copy
         ChDir "C:\OPS\P5A"
         ActiveWorkbook.SaveAs FileName:="C:\OPS\P5A\" & StrINDEX & "-NOC.xlsx", FileFormat:= _
            xlOpenXMLWorkbook, CreateBackup:=False, ReadOnlyRecommended:=True
         ActiveWorkbook.Close
         
         Worksheets("P5A").Range("B12:K1000").ClearContents
         Worksheets("P5A").Range("B5:B8").ClearContents
         
         StrINDEX = .Cells(i, 2).Value
         .Range(.Cells(i, 2), .Cells(i, 11)).Copy
         Worksheets("P5A").Range("B12").PasteSpecial xlPasteValues
      End If
   Next i
End With
Also turning screenupdatng to False will not affect your code & the code you posted cannot "work" as there are a number of problems with it.
 
Upvote 0
I changed all the worksheet references in the main workbook ".Activate" to ".Select" and it works flawlessly. Thanks for the update.

I know it may be hard to believe - but that gives me the accurate output I need, after over 100 test iterations. And I get the same ones as before, but now can have screen frozen.
What problems do you see wrong with my code(Curious/Learning, not Defensive tone)?
 
Upvote 0
Errors highlighted in red, with comments
Code:
Sheets("NOC").Activate
Cells(2, 2).Select
 
StrINDEX = ActiveCell.Value
 
NOCLastRow = Worksheets("NOC").Cells(Rows.Count, 2).End(xlUp).Row ' Find Bottom Row
P5ALastRow = Worksheets([COLOR=#ff0000]“P5A”[/COLOR]).Cells(Rows.Count, 2).End(xlUp).Row [COLOR=#0000ff]'incorrect quotes on sheet name
 [/COLOR]
For i = 2 To NOCLastRow + 1
 
P5ALastRow = Worksheets("P5A").Cells(Rows.Count, 2).End(xlUp).Row ' Find Bottom Row
 
    If Cells(i, 2) = StrINDEX Then
                Range(Cells(i, 2), Cells(i, 11)).Copy
                 Worksheets("P5A").Range("B" & P5ALastRow + 1).PasteSpecial xlPasteValues
   
    Else
   
                Worksheets("P5A").Range("B5") = Worksheets("NOC").Range("L" & i)
                Worksheets("P5A").Range("B6") = Worksheets("NOC").Range("M" & i)
                Worksheets("P5A").Range("B7") = Worksheets("NOC").Range("N" & i)
                Worksheets("P5A").Range("B8") = Worksheets("NOC").Range("O" & i)
   
                Sheets[COLOR=#ff0000](P5A)[/COLOR].Select [COLOR=#0000ff]' no quotes around the sheet name[/COLOR]
                Application.CutCopyMode = False
   
                Sheets[COLOR=#ff0000](P5A)[/COLOR].Copy[COLOR=#0000ff] ' no quotes around the sheet name[/COLOR]
                 ChDir "C:\OPS\P5A"
                 ActiveWorkbook.SaveAs FileName:="C:\OPS\P5A\" & StrINDEX & "-NOC.xlsx", FileFormat:= _
                xlOpenXMLWorkbook, CreateBackup:=False, ReadOnlyRecommended:=True
                ActiveWorkbook.Close
   
                Workbooks("WORKBOOK.xlsm").Activate
                Worksheets("P5A").Range("B12:K1000").ClearContents
                Worksheets("P5A").Range("B5:B8").ClearContents
                Worksheets("NOC").Activate
                Cells(i, 2).Select
                 StrINDEX = ActiveCell.Value
                Range(Cells(i, 2), Cells(i, 11)).Copy
                 Worksheets([COLOR=#ff0000]“P5A”[/COLOR]).Range("B12").PasteSpecial xlPasteValues [COLOR=#0000ff]'incorrect quotes on sheet name
   [/COLOR]
    End If
   
Next i
 
 
 
   
   [COLOR=#ff0000] End If [/COLOR][COLOR=#0000ff]' this line and...[/COLOR]
   
[COLOR=#ff0000]Next i[/COLOR] [COLOR=#0000ff]' this line are duplicated with no starting If or loop[/COLOR]
 
 
Sheets("Mainscreen").Activate
 
Last edited:
Upvote 0
Can you post the code that uses Select rather than Activate?

PS If you use neither it's not really necessary to turn off screen updating.
 
Last edited:
Upvote 0
Hello,

Sorry these mistakes are due to me having to edit my code before I put it up here; specifically I needed to use place holders for sheet names and variables. Those are typos. The reason for two "End If" and "Next i" is because of code redactions.
 
Upvote 0

Forum statistics

Threads
1,224,747
Messages
6,180,719
Members
452,995
Latest member
isldboy

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