Need to convert VBA code to Excel online Scripting - excel

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.

Related

Copy sheet from closed workbook to active workbook sheet with explanation [duplicate]

This question already has an answer here:
Excel VBA to copy and paste as values from a closed workbook to an active workbook, Mac OS X
(1 answer)
Closed 2 years ago.
I know this is probably very basic but I cannot find a consistent code which works.
I am trying to copy the first and only sheet in a closed file on a shared drive location into the first sheet of the active workbook from which I want to run the macro from.
If anyone can please share any help with an explanation of the code of the steps that would be great.
Thank you
One of the most useful features for VBA Excel programmers is the Excel macro recorder. I frequently use the recorder to learn how Excel would write code to implement actions that I take via mouse clicks and menu selections.
Cut to the chase. Suppose my source workbook is called katy_movies.xlsx and contains a list of viewed/favorite movie titles and various cell formatting. Suppose I have that spreadsheet open and want to copy and paste it (with source formatting retained) into a blank worksheet in a different workbook. Here's almost the entire code that the macro recorder "wrote" based on my mouse clicks and menu selections (I made 2 adjustments).
Note: my original solution failed to open the closed source workbook. Here is code that does that and, as a bonus, closes the source after the copy/paste operation:
Sub Macro3()
'
' Macro3 Macro (recorded by excel macro recorder)
'
Workbooks.Open Filename:="\path\to\katy_movies.xlsx"
End Sub
Sub Macro2()
'
' Macro2 Macro
'
Macro3
Windows("katy_movies.xlsx").Activate
Cells.Select
Selection.Copy
Windows("Book1").Activate
Range("A1").Select ' I manually added this line to ensure a valid paste locn
' for an entire worksheet
Selection.PasteSpecial Paste:=xlPasteAllUsingSourceTheme, Operation:=xlNone _
, SkipBlanks:=False, Transpose:=False
Range("A1").Select ' Manually added this line to unselect all cells following
' paste operation.
Range("A1").Copy ' Effectively empty clipboard to stop Excel from whining
' about the next Close operation.
Windows("katy_movies.xlsx").Close ' close the briefly opened spreadsheet
' force focus back to original worksheet
Windows("Book1").Activate
End Sub
I can't stress enough the usefulness of the macro recorder....

How do I copy a range of cells relative to my active cell?

I am trying to write macro that selects a range of cells relative to my active cell and copies all the cells that are selected.
Currently the code I have selects these cells but doesn't copy them.
Sub CellCopy()
ActiveCell.Offset(0).Resize(5, 8).Select
ActiveCell.Copy
End Sub
Thanks, sorry for the very begginer post.
Instead of
Activecell.Copy
You need to use
Selection.Copy

Copy paste shapes from cells

I have made a code that creates labels and barcodes for printing.
Due to other reasons (for simplicity) I have placed some labels on a separate sheet that I then need to transfer to the real sheet.
I found some mention about the CopyObjectsWithCells but it's not working for me.
Application.CopyObjectsWithCells = True
Sheets("Robot").Range("A1:L" & Lastrow).Copy
Sheets("Etikett").Range("A" & intRad).PasteSpecial Paste:=xlPasteAll
I get the same result Range().Select then Selection.Copy.
Sheets("Robot").Shapes.SelectAll
Selection.Copy
Works. But it makes the pasted image one large image, not x small images/shapes. and gives a white background overlaying the other text on the sheet.
If I select the range in Excel and press Ctrl + C / V it copies the images as I want.
But with VBA it just won't work.
Example image
Whenever you have a problem, which is can be described with:
I have managed to do it manually in Excel, but I cannot find the correct VBA code.
a good solution is to use the macro-recorder option in VBA and see the code it generates, while you do the manual work in Excel. In your case, this is the code it makes, when it simply copies two shapes to another worksheet:
Sub Makro2()
ActiveSheet.Shapes.Range(Array("Rectangle 1")).Select
ActiveSheet.Shapes.Range(Array("Rectangle 1", "Rectangle 2")).Select
Sheets("Tabelle2").Select
Range("B4").Select
ActiveSheet.Paste
Range("M25").Select
End Sub
This code is really bad, but it works and it may give you good insights to work further.
After selecting and copying the range that contains the shapes, select the destination cell and then use ActiveSheet.Paste.
The option you are using PasteSpecial Paste:=xlPasteAll won't paste Shapes.

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

Run Time Error '1004': Select method of Range Class failed VBA 2003

I am trying to copy a column from one sheet to another. The code I am using is a recorded macro and it works fine until I connect it to a button. When I do so, it gives a
Run Time Error '1004': Select method of Range Class failed
Here is the code and I can see nothing wrong with it. When I hit debug it highlights the second line.
Sheets("Count").Select
Columns("C:C").Select
Selection.Copy
Sheets("Add Invintory").Select
Range("b1").Select
ActiveSheet.Paste
Sheets("Count").Select
Sheets("Count").Columns("A:A").Select
Columns("A:A").Select
Selection.Copy
Sheets("Add Invintory").Select
Range("A1").Select
ActiveSheet.Paste
I have no clue what the problem is. Please help
You should always avoid using .Select They are a major cause of errors. You may want to see How to avoid using Select in Excel VBA
Sub Sample()
Sheets("Count").Columns("C:C").Copy _
Sheets("Add Invintory").Columns("B:B")
Sheets("Count").Columns("A:A").Copy _
Sheets("Add Invintory").Columns("A:A")
End Sub
I think the issue is that you have written the code in another sheet's code module. If I'm in Sheet1, and write e.g.
Sheets("Sheet2").Select
Columns("A:A").Select
...then Excel assumes you are referring to the Columns on Sheet1 as it treats the current sheet as a default. Therefore, you've told Excel "select Sheet 2" then "select a column on Sheet 1"...which it can't do so it gives you an error message. The best solution would be not to use 'Select'...but you will still see in Siddharth's code that he has had to refer to sheet addresses explicitly
Your original code would have worked if placed in the ThisWorkbook module. Locations for entering code are explained towards the end of this Excel help video
When you are putting vba code into the "view sheet code" .. There definitely helps to use Application.Run ... to run macro..
I had problem i directly input macro to sheet code.. for selection in another sheet it claimed runtime error 1004.. So i created macro separately and then i put Application.Run my macro from sheet code.
Works out perfectly ;)
This Application.Run also helps when you have too big macro that excel claim it cant be so big. You can easily divide to several parts and then just run applications one by one.. ;)

Resources