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
Related
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.
I am running a function with a defined range and when trying to use End.(xlDown).Select I get a message "Select method of Range class failed".
I have edited the code below to show only the problem piece. After literally two hours of trying everything, I cannot get it to work.
The annoying part is that I have to use defined ranges since the function is part of a much larger Sub that doesn't work as intended once Select and Activate are used.
Function OutputFunction()
Dim rng8 As Range
Set rng8 = ThisWorkbook.Worksheets(5).Range("A2")
rng1.ClearContents 'Works like a charm.
rng2.Copy 'No problem here either.
rng8.End(xlDown).Select 'Fails misserably.
ActiveCell.Offset(0, 13).Select
Range(Selection, Range("N3")).Select
ActiveSheet.Paste
rng2.Copy destination:= rng8.parent.range(rng8.End(xlDown).Offset(0, 13), rng8.parent.Range("N3"))
"After literally two hours of trying everything, I cannot get it to work."
The first rule of Excel Macros: Never, ever, use SELECT in an Excel Macro.
The second rule of Excel Macros: Don't use Select in Excel Macros
The third.....
Try:
Option Explicit
Sub test()
Dim rng8 As Range
'Have in mind that you refer to a sheet based on it s index, NOT with its name!
'If sheets order change you will refer to another sheet
With ThisWorkbook.Worksheets(5)
Set rng8 = .Range("A2")
rng8.Select
.Range(rng8, rng8.End(xlDown)).Select
End With
End Sub
Try to use End(xlDown).Select in my personal macro. First I tested this in the original excel file that I wrote the macro in, and it worked in every step just fine. But the problem occurred when I used it in another file.
After some tests, I changed the .Select with .Activate and it worked. I'm not 100% sure whether we are talking on the same page or not, so tell me if this solved your problem, so I can improve my answer.
I have been struggling with this problem for awhile now.
I've written a VBA routine that is supposed to find a region on one worksheet, then cut and paste it to another worksheet. If I run the following code it doesn't work:
' This does not work
DSheet.Range(SummaryDataAddr, DSheet.Cells.SpecialCells(xlLastCell)).Cut
PSheet.Range(SummaryDataLocation).PasteSpecial Paste:=xlPasteValues
But if I change the code to copy the region from the first worksheet, paste it to the second worksheet, then go back to the first worksheet and explicitly delete the region, it works.
' But this does work. Why?
DSheet.Range(SummaryDataAddr, DSheet.Cells.SpecialCells(xlLastCell)).Copy
PSheet.Range(SummaryDataLocation).PasteSpecial Paste:=xlPasteValues
DSheet.Range(SummaryDataAddr, DSheet.Cells.SpecialCells(xlLastCell)).Delete
Has anyone seen this kind of problem before? Is there a way to use the cut function and get it all to work?
To access a the spreadsheet with the VBA code in it go here:
https://www.dropbox.com/s/satv6z95tlqw7lr/CutBug.xlsm?dl=0
The routine is called "CreateDLDataPivot" (it's a pared down version of a larger program I'm working on).
Thanks for any help!
PasteSpecial Paste:=xlPasteValues does not work with Range.Cut; it only works with Range.Copy.
Range.Cut only clears the cells, it does not delete the cells.
Try a direct value transfer and clear.
with DSheet.Range(SummaryDataAddr, DSheet.Cells.SpecialCells(xlLastCell))
PSheet.Range(SummaryDataLocation).resize(.rows.count, .columns.count) = .value
.clear 'use .clearcontents to retain formatting
end with
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.
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.. ;)