Range.PasteSpecial causes an Application ScreenUpdate in Excel VBA - excel

I have written a VBA Macro for Excel that looks like the following:
Application.ScreenUpdating = False
Dim copyingRange, pastingRange As Range
copyingRange.Cells.Copy
pastingRange.PasteSpecial xlPasteValues
Application.ScreenUpdating = True
It is my understanding that Application.ScreenUpdating = False will force the application to stop screen updates however this is not the behavior I am seeing. I am copying a range from one sheet to another and the application switches views to and from very quickly, an undesirable effect.
Taking out the line pastingRange.PasteSpecial xlPasteValues causes the screen flickering to go away but obviously results in the macro not doing what I want.
Is the pastingRange.PasteSpecial xlPasteValues the problem here? I must be misunderstanding something here?

From comment, so this can be listed as Answered:
You can avoid copy/paste since you just want values and write:
pastingrange.value = copyingrange.value

Related

VBA - flickering after xlsheetvisible = true

I've got a problem and really need some help from you. I was trying to google the solution, but found nothing useful. My macro runs by clicking on a commandbutton1 which is located on sheet1. Important detail: a chart is also located on sheet1.
The only thing the macro does is switching sheet2 from 'hidden' to 'visible'.
Sub abc()
Sheets(2).Visible = Not Sheets(2).Visible
End Sub
THE PROBLEM: after switching the sheet2 from hidden to visible the screen is flickering.
Notes:
The problem occurs only on the sheets with charts. It seems like excel is doing some chart update.
It occurs only when sheet2 is being switched to "visible".
I've tried:
Application.EnableEvents = False
Application.ScreenUpdating = False
Application.DisplayStatusBar = False
Application.Calculation = xlCalculationManual
I deleted any other macros and events in the workbook.
I've tried to run a macro directly from VBA editor (not via commandbutton1).
I've tried debugging step by step.
Is there anyone who can help me with this issue?

VBA - Locking Cells Only Working In Some Cells and Not Others

this project is certainly testing my limits in VBA. I have yet another question. In this workbook, I have a button called "Add Projects" that takes a different sheet "Project Templates" and uses it to create a new sheet called "Add Projects". I'm trying to add some specific cell locking when this sheet is created. The idea is that users can alter certain areas, but not ones that have formulas or are auto-populated by an entry in a different cell.
The problem is, when I add the code, certain ones lock appropriately, one particular cell does a popup asking for a password (when it should just be locked like the others), and some let you still edit them despite them being included in the locked code.
I've checked the cell format, and the box for locked is selected. I've also gone to the Allow Users to Edit Ranges and removed everything from there (although those ranges weren't in there). I've moved the order of the ranges around, thinking maybe they were somehow overwriting each other even though I have a distinct cell set as the end range.
Public Sub Open_AddProjectSheet()
Application.ScreenUpdating = False
ActiveWorkbook.Unprotect Password:="Password"
Worksheets("ProjTemplate").Visible = True
If WorksheetExists("AddProject") = False Then
GetTSResourceInfo
GetTSProjectID
ActiveWorkbook.Unprotect Password:="Password"
Worksheets("ProjTemplate").Copy After:=Worksheets("ProjTemplate")
ActiveSheet.Name = "AddProject"
Worksheets("ProjTemplate").Visible = xlSheetVeryHidden
Sheets("AddProject").Activate
ActiveSheet.Unprotect Password:="Password"
ActiveSheet.Cells.Locked = False
ActiveSheet.Range("E5,C6,L2,L6").Locked = True
ActiveSheet.Range("E11:E190").Locked = True
ActiveSheet.Range("G11:H190").Locked = True
ActiveSheet.Range("K2:K6").Locked = True
ActiveSheet.Range("L11:R190").Locked = True
ActiveSheet.Range("L3:L5").Locked = False
ActiveSheet.Protect Password:="Password",UserInterFaceOnly:=True,Contents:=True
Else
answer = MsgBox("'AddProject' sheet already active. Would you like to pick up where you left? WARNING: Selecting 'No' will close the existing sheet and open a new one, any progress will not be saved.", vbYesNo)
If answer = 6 Then
Worksheets("AddProject").Activate
Else
*Same code as above*
End If
End If
Worksheets("ProjTemplate").Visible = xlSheetVeryHidden
ActiveWorkbook.Protect Password:="Password"
Application.ScreenUpdating = True
End Sub
When running this code, E5,C6,K2-K6,L2,L11-R190 are all locked correctly. L6 brings up a popup asking for a password to change this cell. All other ranges, E11-E190, G11-H190 are editable even though they should be locked.
Any ideas I'm greatly appreciative of. Thanks!
So, even though it didn't work before, we went back and tried to alter the Allow Users to Edit Ranges. Wiped all of them out again and the cell locking started working. I'm not going to question why it worked vs not working the last time it was tried, just glad it's working! That being said, the speed being different is still an issue.

Screenupdate vs Selection issue - VBA Excel

I'm facing an odd situation. I have a button on a sheet which runs many functions, being one of those opening another file:
If Not IsItOpen(ENDERECO2) Then
Workbooks.Open Filename:=ENDERECO1
End If
'ENDERECO2 has the file's name
'ENDERECO1 has the full path of the same file
'IsItOpen is a private function as follows:
'Private Function IsItOpen(Name As Variant) As Boolean
' On Error Resume Next
' IsItOpen = Not (Application.Workbooks(Name) Is Nothing)
'End Function
After opening the other workbook, when it isn't already opened, I bring focus to the first sheet, as I want the second one to be opened on the background. To do that, I use:
'At the very beggining of the code
Dim CEL As Range
Set CEL = Selection
'And at the end of it all
CEL.Select
All the described code works perfectly.
The problem I've been having: as this button runs many things at once, I wanted to add an "Application.Screenupdating = False" at the beggining and "... = True" at the end, so it won't flicker too much when calculating. The thing is that when I added the Screenupdating stuff, it will still open the second workbook as desired, but it won't bring the focus back to the main workbook. Instead, it stops at the recently opened workbook and there it stays.
What could be the interference of the Screenupdating on the CEL.Select command?
Any ideas?
Cheers
Thanks Taelsin. Guess when we don't know exactly why, we improvise lol. This worked fine:
If Not IsItOpen(ENDERECO2) Then
Application.ScreenUpdating = True
Workbooks.Open Filename:=ENDERECO1
Application.ScreenUpdating = False
End If
It is good enough. Cheers!

VBA Textbox Autofilter hides everything

I implemented a google like search box into my spreadsheet, using an activex textbox.. However, in the beginning it worked fine, but now it hides the whole used range and i only see blank cells. This really needs to work 100%, but i can't figure out what's the problem.
The code is as follows:
Sub TextBox1_Change()
Application.ScreenUpdating = False
Range("$O$7").AutoFilter Field:=14, Criteria1:=TextBox1
End Sub
I'd appreciate your help with this issue.
I don't see any statement to clear previous filters, for example:
Worksheets(1).AutoFilterMode = False
Also, there should be Application.ScreenUpdating = True before End Sub to restore screen updating.
If TextBox1.vlaue = "" Then Application.Autofiltermode= False

Launch a different sheet by Spin button

want to use a Spin button (ActiveX Control) to show a previous or next sheet. When the button is clicked the event is successfully triggered and the desired sheet is activated but it holds some elements (commands, graphs, etc.,) from the original sheet and shows these as an appending picture.
Sample code for Down button event :
Private Sub SpinButton_JumpToWeek_SpinDown()
Dim sh_num As String
Dim tmp_num As Integer
Application.ScreenUpdating = False
Application.EnableEvents = False
SpinButton_JumpToWeek.Value = Range("B27").Value - 1
tmp_num = SpinButton_JumpToWeek.Value
' Activate desired KTx sheet
sh_num = "KT" & tmp_num
Range("F27").Value = "" 'reset to blank
Sheets(sh_num).Activate
Application.ScreenUpdating = True
Application.EnableEvents = True
End Sub
To override this effect I have to manually select (activate) another sheet and then again select the desired sheet. I tried also to automatize this workaround with a macro, but unfortunately it does not work.
It is interesting that this problem do not occur if I execute code in Debug mode (using breakpoint and the stepping line by line).
Surprisingly, I do not have such problem if I try to show the previous/next sheet by writing a value (sheet name index) into a defined cell (i.e. using the Worksheet_Change event). The desired page is correctly shown. See photos.
Sample code for this evententer image description here:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim sh_num As String
Application.ScreenUpdating = False
Application.EnableEvents = False
If Range("F27").Value > 0 Then
' Activate desired KTx sheet
sh_num = "KT" & Range("F27").Value
Range("F27").Value = "" 'reset to blank
Sheets(sh_num).Activate
End If
Application.ScreenUpdating = True
Application.EnableEvents = True
End Sub
I need to use the Spin button because it is faster and allow me to skip some sheets (for example if these do not have data).
The problem occurs in both 32 and 64-bit Excel.
Do somebody an idea why this problem is occurring and a workaround? Do I have to change some settings or system properties in Excel? Any help would be highly appreciated.
#mehow
I append my commenst here due to large number of words.
I followed your suggestion and tried the example of a UserForm with inserted active control “Microsoft Office Spreadsheet”. I found out that this would not be a good solution for my case, because the response of such sheet is relatively slow (even on a fast PC like mine) when user inserts values into cells. Furthermore, this would greatly complicate my fairly simple *.xlsm workbook, which has more than 50 sheets (1 sheet for each week, the contents of these sheets are then linked to a main sheet), and completely meets my basic necessities (with the exception of this spin button of course).
In my opinion there is probably necessary to manipulate some kind of system property (like for e.g. Application.ScreenUpdating trick), but I do not have enough VBA knowledge to find it.
To clearly explain my question I would need to share my sample workbook, but I do not know how to upload it to this forum. Unfortunately, I am not able upload/show images successfully on this forum (due to my low rating), which would also help a lot.
BTW, I cannot see images on other questions of this forum, too. . Could this problem occur due to missing plugins in a web browser?
You can use a simple trick... before the "Application.screenupdating = true" you can insert the two lines:
ActiveWindow.SmallScroll Down:=-100
ActiveWindow.SmallScroll Up:=100

Resources