vba inserting/copying range of rows next cell formulas collapse - excel

i am using basic vba code to insert Range(C3:E3").Insert xlShiftDown rows. I discovered that, formulas which are related to these cells, and are in different columns also collapse with inserting range.row (not entire.row).
I wonder if there is any workaround with vba code(i mean using insert with different mode) or it is part of excel game we need to figure out to solve our problem without inserting rows.
Appreciate any guidance or opinion why it could be so in excel ahead

Can you try cut or paste?
Range("C3:E3").Select
Selection.Cut
Range("C10").Select
Selection.Insert Shift:=xlDown
Range("C3:E3").Select
Selection.Cut
Range("C10").Select
ActiveSheet.Paste

Related

Need to convert VBA code to Excel online Scripting

Good morning all,
Can someone help me by converting this simple VBA code to Excel Script code?
What the script does is it selects and entire row from the active cell and pastes it on the first row to the Template sheet.
Sub Copy()
ActiveCell.EntireRow.Select
Selection.Copy
Sheets("Template").Select
Range("A1").Select
ActiveSheet.Paste
Range("A1").Select
End Sub
Instead of all the copying and pasting, why not simply use this piece of code (obviously you need to run this while having a worksheet open, different than the "Template" one):
Worksheets("Template").Range("1:1").Value = ActiveSheet.Range("1:1").Value
More information on this can be found in this other StackOverflow post.

Running Excel using VBA from another program

I am using vba in a CAD program to export data, sort the data, and add data. The following macro is exactly what I want excel to do. However I believe I am limited to having the CAD program tell Excel what to do through VBA. This macro copyies a formula and pastes it to all the populated cells below it in the column.
MACRO CODE:
Range("B1").Select
Selection.Copy
Range(Selection, Selection.End(xlDown)).Select
ActiveSheet.Paste
iLogic Version of Code:
oBook.WorkSheets(1).Name = "Order List"
oBook.WorkSheets(2).Name = "Cut List"
wSheet1 = oBook.WorkSheets("Order List")
wSheet2 = oBook.WorkSheets("Cut List")
wSheet2.Activate
wSheet2.Range("B1").Select
wSheet2.Selection.Copy
wSheet2.Range(Selection, Selection.End(xlDown)).Select
wSheet2.Selection.Paste
Unfortunately I seem to be missing something to translate between Inventor and Excel, but I don't know enough to even know if that's the issue.
Any advice is very much appreciated as I am still very new to VBA.
Ok. I had some code that I had copied off of a forum but didn't understand the functions going on. I believe this is what you are referring to "Application"?
wSheet2.Columns("G:G").select()
oExcelApp.Selection.cut()
wSheet2.Columns("B:B").Select()
oExcelApp.Selection.Insert(Microsoft.Office.Interop.Excel.XlDirection.xlToRight)
Although I don't understand what the
"(Microsoft.Office.Interop.Excel.XlDirection.xlToRight)" refers to. I understand the part about the direction and where it inserts just not whats before it.
I do agree with #DisGruntledDraftsman about not using Select and Activate. However, if you are just trying to get some working code that gets the job done, some simple tweaks can be done. Of course this isn't ideal but, it should work though.
row_Count = wSheet2.Range("B1048576").End(xlUp).Row
wSheet2.Activate
wSheet2.Range("B1").Select
Selection.AutoFill Destination:=wSheet2.Range("B1:B" & row_Count), Type:=xlFillDefault

Pasting artihmetic mean VBA

Let's say I have two Excel Workbooks(in reality I have one sheet results and maybe a hundered other workbooks containing data). I would like to create a macro that allows me to take the arithmetic mean of a selection and paste that into my active cell. I have written a macro that allows me to paste copied values between different workbooks, really simple:
Sub PasteVal()
Selection.PasteSpecial xlPasteValues
End Sub
Trying to do the arithmetic mean copying does not work, however:
Sub PasteMean()
ActiveCell.PasteSpecial (Application.WorksheetFunction.Average(Selection))
End Sub
Any help would be appreciated
Thanks.
did you try ?
activecell.value=Application.WorksheetFunction.Average(Selection)
casually stumbled upon this post. consider the xlPasteSpecial method with
XlPasteSpecialOperation Enumeration.
xlPasteSpecialOperationAdd
xlPasteSpecialOperationDivide
xlPasteSpecialOperationMultiply
xlPasteSpecialOperationNone
xlPasteSpecialOperationSubtract
With Worksheets("Sheet1")
.Range("C1:C5").Copy
.Range("D1:D5").PasteSpecial _
Operation:=xlPasteSpecialOperationAdd
End With

How to prevent Excel VBA paste special values from changing data type from number/date to text

I am encountering a problem where Excel VBA paste special values is changing the data type to text when it carries out the paste values operation, which then breaks downstream formulas that expect to see a number/date instead of text.
At a high level, the process I have is the following:
starting with functional worksheet, duplicate and rename it;
perform operations on the newly created sheet including copy, paste special values;
formulas that depend on the pasted data are now broken because data type has been changed to text.
The code that is doing the copy paste special values looks like this:
Workbooks("myFile.xlsm").Sheets(pageFocus).Range(refreshCopyRange).Copy
Workbooks("myFile.xlsm").Sheets(pageFocus).Range(pasteRange).PasteSpecial (xlPasteValues)
Is there some kind of modifier or override to PasteSpecial (xlPasteValues) that will stop Excel VBA from changing the data type?
Many thanks in advance for any help!
You need to do a second paste command for formatting:
Workbooks("myFile.xlsm").Sheets(pageFocus).Range(pasteRange).PasteSpecial xlPasteValues
Workbooks("myFile.xlsm").Sheets(pageFocus).Range(pasteRange).PasteSpecial xlPasteFormats
Here is a sample:
https://social.msdn.microsoft.com/Forums/office/en-US/b593e52c-910c-4d24-b738-65878fe8a50d/how-to-copypaste-range-values-and-formats?forum=exceldev
You can use this
.PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:= _
xlNone, SkipBlanks:=False, Transpose:=False

VB Excel PasteSpecial requiring clipboard content?

I'm having a problem with VB PasteSpecial.
This code works perfectly in Excel VB (given that you have selected cells with data)
Selection.Copy
Range("A1").Select
Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=True
Application.CutCopyMode = False
However, I'm using a third-party software (QlikView) that I extract the data from, which is then supposed to be copied into the Excel document. There is no problem with the normal paste but it MUST be transposed.
Obviously, since I dont have any content in the workbook to copy, I don't use
Selection.Copy
But because I don't copy anything from the document first (even though there are table data in the copy memory), this call returns bad argument exception (this also happens if I copy cells in that VERY workbook first and then just call the macro for transposing it).
Runtime error '1004' returned. PasteSpecial method of Range class failed.
Yes, I can paste it into the document, then cut it from the area, move it to the correct place and transpose it, but that is bad coding.
Have any of you experienced this and got a way to get this working ?
You will have to use the method as you mentioned above. You can also try this
Range("A1").Select
ActiveSheet.PasteSpecial Format:="Text", Link:=False, DisplayAsIcon:=False
Application.CutCopyMode = False
But you will have to copy it again and transpose it. Other wise there is no direct way you can transpose it.
The reason that you cannot use PasteSpecial to transpose the data is due to the format of the data as it resides in the clipboard after you copy it from QlikView.
When you copy data from a QlikView table (which I assume you are copying from), it copies it to the clipboard in three formats: HTML, Unicode and standard (code-paged) text:
Comparing this with Excel's clipboard formats:
As you can see, when copying data in Excel, it stores the data in the clipboard in its own format and as such knows how to transpose the cells if required. For QlikView, the clipboard just contains plain text, therefore Excel does not know how to transpose this and as a result the PasteSpecial call fails.
If you are copying from a table in QlikView to Excel, I would recommend performing the transposition already in QlikView if you can by using a "Pivot Table" chart in QlikView (as you can drag the columns and rows around how you wish). Otherwise you will have to use Siddharth's code and transpose it once it's in Excel.

Resources