How do I disable the copy range using VBA after the copy/paste Sub is run?
Application.CutCopyMode = False does not work. The copy range is still selected and will paste in any cell if the enter button is pressed.
Related
I copy and paste formulas frequently, so I wrote a short macro to give me a keyboard shortcut:
Sub PasteFormula()
' Keyboard Shortcut: Ctrl+Shift+F
Dim TargetRange As Range
If Application.CutCopyMode Then
Set TargetRange = Application.ActiveWindow.Selection
TargetRange.PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
End If
End Sub
It works exactly as desired, with one undesired pecularity. Here's how to create a MWE:
Open a new workbook Book1.xlsb. Copy the VBA above into a module, and link up the keyboard shortcut Ctrl+Shift+F.
Use View -> Window -> New Window to create a second window, so that the windows' titles are "Book1:1" and "Book1:2".
In the window "Book1:2" create a second sheet "Sheet2" and open that sheet. Type something in one cell, copy it, go to another cell on the same window, and press Ctrl+Shift+F.
The macro will work--the formula from the first cell is copied, as a formula, into the second cell. However, the first window "Book1:1" changes to display Sheet2. How can I use VBA to run PasteSpecial in a secondary window without changing sheets in the primary window?
Clarification
I am working in just one workbook, "Book1.xlsb", open in two windows. That workbook has two sheets; I have Sheet1 open in the first window and Sheet2 open in the second window.
When I run a PasteSpecial in Sheet2 in the second window (Book1:2), the first window (Book1:1) switches to Sheet2 as well.
For contrast, if I run a PasteSpecial in the first window (Book1:1), the second window does not switch sheets.
I think this behavior is strange. So my questions are
Why does Excel do this, and/or
How can I make Excel not do this?
I have a macro which auto saves the workbook every 5 minutes to avoid losing data. The workbook is set up with a data connection to collect production data from a PLC controller and production operators can also enter notes into the workbook.
The problem is that if someone started entering a note but didn't confirm the entry by pressing enter or tab or by clicking on a different cell then the auto save macro will not execute and the workbook will not be saved until the focus changes to another cell.
I tried to change the active cell in the macro right before executing the save statement, but that didn't work. Is there a way to accept the entered (but not confirmed) cell contents before attempting to save the file? Is there another solution that I haven't thought about?
This is the macro code:
Sub AutoSaveAs()
dTime = Time + TimeValue("00:05:00")
With Application
.OnTime dTime, "AutoSaveAs"
.EnableEvents = False
.DisplayAlerts = False
ActiveCell.Offset(1, 0).Select 'This is the code where I tried to change the active cell
ThisWorkbook.SaveAs "//ThisFilePath/ThisWorkbookName"
.EnableEvents = True
End With
End Sub
You cannot run a macro while editing a cell. You need user to click another cell or press enter.
See this one of many search result:
https://stackoverflow.com/questions/26848266/start-vba-macro-when-editing-a-cellunles
How can I copy and paste a cell from one cell to another that also carries-over formatting on certain texts within the cell using VBA in Excel? For example,
how would I copy "Hello World" so it exactly appears that way in pasted cell?
Thanks!
Create macro and record your steps in Excel.Open the macro and view the VBA code
Range.PasteSpecial Method (Excel)
Option Explicit
Sub PasteSpecial()
'Copy and PasteSpecial a Range
Range("A1").copy
Range("A3").PasteSpecial
Application.CutCopyMode = False
End Sub
I am trying to copy a dynamic range (B12:Lxx)from one worksheet to another. I need the range being copied to stop at the first empty row (there is additional data further down the sheet which I don't want copied).
I am a very basic VBA user so if you could explicitly set out your instructions that would be handy.
Source: Worksheet "MyKPIs" with the dynamic range B12:Lxx (column L is set, row numbers are variable BUT must end at the first empty row)
Target: Worksheet "Month Template", cell B5
Trigger would be a command button
I have trawled through other articles but have failed to find anything that I could use.
Thanks,
Hayley
this will work. insert a command button on your worksheet. double click the button. paste in this code between sub and end sub.
Worksheets("MyKPIs").Range("b12").CurrentRegion.Copy Worksheets("Month Template").Range("b5")
it should look like this when you are through. then go to your worksheet on developer tab toggle off design mode then click the button.
Private Sub CommandButton1_Click()
Worksheets("MyKPIs").Range("b12").CurrentRegion.Copy Worksheets("Month Template").Range("b5")
End Sub
for those inexperienced with currentregion please look at the 2 samples below that have blank cells but the region is selected and you can easily see the beginning and ending points in the range and how an entire blank row or column forms the range.
Place a command button from the Forms toolbar on your "MyKPIs" sheet.
Then add this code:
Sub Button1_Click()
Dim myrange
Set myrange = Sheets("MyKPIs").Range("B12:L12")
myrange.Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy Worksheets("Month Template").Range("B5")
End Sub
Considering all cells in column B have data, this will copy all the cells in the range. It will stop at the first empty cell in column B. This should help you to start.
I have a problem in refreshing the cell's calculation.
In other words, I have many columns where i have a formula or macro in each one of them.
But the problem is that i should absolutely activate the option " automatic calculation" in the excel options or i should save then the new results appear.
Now, I would insert something in the macro that can refresh me the results instantaneously.
Thanks
To force all formulas to update, including custom vba formulas, execute the following in VBA:
Application.CalculateFullRebuild
This can also be executed from the VBA immediate window.
More documentation here:
https://learn.microsoft.com/en-us/office/vba/api/excel.application.calculatefullrebuild
You could simply press :
F9 to calculate the whole active workbook
Shift + F9 to calculate the active sheet
Anyway, here are the different options with .Calculate :
Sub Souma()
'Use this to calculate all the open workbooks
Application.Calculate
'Use this to calculate the whole sheet, called "Sheet1"
ThisWorkbook.Worksheets("Sheet1").Calculate
'Use this to calculate the cell A1 on the sheet called "Sheet1"
ThisWorkbook.Worksheets("Sheet1").Range("A1").Calculate
'Use this to calculate the range A1 to D10 on the sheet called "Sheet1"
ThisWorkbook.Worksheets("Sheet1").Range("A1:D10").Calculate
'Use this to calculate the column A on the sheet called "Sheet1"
ThisWorkbook.Worksheets("Sheet1").Columns(1).Calculate
'Use this to calculate the row 1 on the sheet called "Sheet1"
ThisWorkbook.Worksheets("Sheet1").Rows(1).Calculate
End Sub
Copy and paste the code below to your VBA project:
Private Sub Workbook_Open()
Application.Calculation = xlCalculationAutomatic
End Sub
This code should automatically run after you open the Excel file, enabling automatic calculation in all opened files.