PasteSpecial in secondary Excel window changes worksheet in primary window - vba

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?

Related

How do I stop the copy range using VBA?

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.

Excel Macro - Run Against Another Workbook

Good Morning All,
I'm just trying to understand how to run a macro that is in one workbook but apply the macro procedure/changes into another workbook that is open.
What I am trying to achieve is one workbook is say the Template that will always be open. I have a macro in that Template file that works through a directory looking for xlsm files and opens them one at a time. What I want to do is when the workbook is opened another macro is called in the Template file which updates connection string details in the other open workbook.
I have the macros ready and they work, but I want to run them against another workbook without having to copy the code into it.
Is this at all possible?
Thanks in advance.
Make a variable to store your workbook, then use it to refer to it. Something like this:
'place this at the top of the module so that the variable can be used by all macros
Private wb As Workbook
'place this in your "browse and open" macro in place right after you open a workbook
Set wb = ActiveWorkbook
'now you can do whatever you want by referring to wb
wb.Worksheets("Sheet 1").Range("A1") = "Cell A1"
wb.Worksheets("Sheet 2").Range("C3").EntireRow.Delete
wb.Close
You could also continue referring to ActiveWorkbook, but you have to make sure that it actually remains active all the time you want to work on it. If in the meantime you want to do something on your template workbook, you can refer to it as ThisWorkbook.
Sub h()
'ask for row number >>
k = InputBox("witch row?")
' you now define the worksheet >>
Sheet2.Select
'now you see that in this case i used a selected cell as point of reference>>
Sheet2.Range("a" & k).EntireRow.Delete
End Sub
In MS EXCEL Sheet1, go to "Insert tab", to "shapes",>>
select a rounded cornered box, add a text to it so its intuitive,>>
change the colors of text and background, then :>>
select the shape you created with the mouse with a "right click">>
and from the list that appears select "Assing Macro", and select macro "h"
if all goes to theese indications, we have the next step >>
if u click it it runns the sub. and therefor you will be presented with a inputbox, where you specify the row number you want to be deleted.
there you go!
p.s. hoping im clear.

Excel 2010 PasteSpecial method of range class failed

I am attempting to understand why a certain bit of code will not work in Excel 2010.
I have a file where cells A1:K200 are part of a table (the Format as Table option form the Home tab), but rows 2-13 are hidden for use with data validation combo boxes. Header row and row banding options are enabled. What I want to do is copy (via the user pressing Ctrl-C) and paste (via user pressing Ctrl-V) data from another workbook and then have a macro function in Worksheet_Change that will transpose the data (based on the value of a checkbox on the worksheet) and paste only the values. Columns A-H will be filled out with other data.
I have disabled events and screen updating, set up a range for cells I14:I200 as StdCost for an intersect function and then have the following code to trap changes to the above range. optTranspose is a checkbox I have placed on the sheet and Target is the variable set up by Excel as part of the Worksheet_Change function.
If Not Application.Intersect(Target, StdCost) Is Nothing Then
If Application.CutCopyMode = xlCopy Then
Application.Undo
.Range("I" & Target.Row).PasteSpecial Paste:=xlPasteValues, Transpose:=optTranspose
End If
End If
The above code will work, but based on my testing it will only work with a maximum of 4 values being pasted in OR it will work if I paste starting on row 15 or below. However, if I paste 5+ values I get the error from the subject
PasteSpecial method of range class failed
From my testing, what appears to happen is that before the undo operation kicks in, Excel automatically adds Column1 and Column2 to columns L and M, which then causes Target to take on "Column1" and "Column2" and thus ends up causing the error when I try and run it on the actual values I want to paste. If I convert the table to a normal range, everything appears to operate as expected.
I want to understand why "Column1" and "Column2" are being inserted by Excel, why it's only at 5+ cells from another spreadsheet that it becomes and issue and how I might be able to account for this in my code.
I want to understand why "Column1" and "Column2" are being inserted by Excel,
This happens when you paste data adjacent to an existing ListObject table.
The change event occurs (i.e., you "Paste" the data), the worksheet changes, and then, the Worksheet_Change event fires.
The error seems to happen because Application.Undo is undoing the Paste operation and further affecting the Application.CutCopyMode = False, so on the next line when you attempt to use the PasteSpecial method, there simply is nothing that can be pasted, because the Undo has cleared it.
Pastespecial seems to be very sensitive to the parameters it's fed.
I was experiencing exactly the same error message from code that had worked before migrating to Windows 10. I found this combination of parameters worked in my case:
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Can't claim that I found that combo of parameters- it came from yactici's self answer
Kudos to yactici for returning to his initial problem despite having a workaround and directly resolving the problem with pastespecial.

Excel VBA runtime error 1004

I'm trying to write a macro to paste special formulas but keep getting a runtime error 1004 "PasteSpecial method of Range class failed".
This macro comes directly from using the "Record Macro" provided by Excel.
Sub paste_formulas()
Selection.PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
End Sub
Here is the sequence of events:
Manually select a range of cells in CSV file A and copy (CTRL+C).
Switch to the XLS file B and select the cell were I want the copied data pasted.
Press Macros and run personal.xlsm!paste_formulas
That's when I get the error. Why does this work manually when I copy/paste-special formulas, but fail in the macro?
Note: I need this to work in the above sequence regardless of the selected range that I copy (will vary from time to time), and regardless of the location I paste formulas to (will also vary from time to time). In other words, hardcoding a fixed range for copy and/or paste won't work for me.
Thanks in advance for any help understanding why my code isn't working or providing a work-around.
The reason is very simple, When you copy from the CSV and then in your workbook, click on Macros in the Developer Toolbar, Excel clears the clipboard.
Excel has this habit of clearing the clipboard when you click on Developer | Macros. To demonstrate this, copy the cells from the same workbook. You will see the ant like border around the cells. Now in the same workbook, click on Developer | Macros. The Ant like borders will disappear :)
Set a shortcut key for your macro and use that. it will work :)

How do I open a worksheet in vba?

Hilariously the following code only works if the worksheet is actually selected in the excel window. I really want to finish this macro soon but can't seem to work out how to select a specific worksheet so it is open in excel? Many thanks if someone knows how. I have to use range and so on.
sheet.Range(Cells(firstRow, 2).Address(False, False), Cells(lastRow, 50)).Select
With Selection
.Copy
End With
sheet.Range(Cells(firstRow, 3).Address(False, False), Cells(lastRow, 51)).Select
With Selection
.PasteSpecial xlPasteValuesAndNumberFormats
End With
You can activate the worksheet by name or by 1-based index (the number -- 1st workbook, 2nd, and so on). The syntax is the same, either way.
This will activate the 3rd worksheet:
ActiveWorkbook.Sheets(3).Activate
This will activate the worksheet named stats:
ActiveWorkbook.Sheets("stats").Activate
Of course, you don't have to actually make the worksheet selected in the Excel window to work with it. Your code uses a variable called sheet, which I assume you've assigned to the active worksheet. Instead of doing that, you can set sheet = ActiveWorkbook.Sheets("stats"), and then work with the sheet even if is not in view.
Workbooks(x).Worksheets(x).Activate ?