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.
Related
I found a serious problem in my situation. Since I had the textboxes I decided to move them in the different parts of the sheet using cut & pastespecial option.
Unfortunately I noticed, that my shape is not a textbox anymore. Instead of it it has been turned into the image, where I cannot edit the text inside.
My code looks like this:
ActiveSheet.Shapes("TextboxLocation1").Cut
ActiveSheet.Range("AA70").PasteSpecial
Selection.Name = "TextboxLocation1"
ActiveSheet.Shapes("Textbox_Location2").Cut
ActiveSheet.Range("AA71").PasteSpecial
Selection.Name = "Textbox_Location2"
Is it possible to prevent the initial features of my shape before moving (cut & pastespecial) across the sheet?
I guess, that I shall avoid the "Selection" option and use "dim" instead...
How to avoid using Select in Excel VBA
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 would like to copy the text from a excel file in a specify range to another excel file in the same position
Here is the code that I tried
Sub OneCell()
ChDir "C:\Workfile"
Windows("simple av & cv template(Level).xls").Activate
Sheets("Table ENG SG").Select
Range("C9:C44").Select
Selection.Copy
Windows("Finalize").Activate
Sheets("sheet1").Select
ActiveSheet.Paste
End Sub
Do I need to define sth at the beginning of my program first or did I make any mistakes?
Assuming both your workbooks are open in the same instance of Excel which you can confirm by going to the View tab > Switch Windows and seeing if both workbook names are listed:
Sub ModifiedOneCell()
Workbooks("simple av & cv template(Level).xls").Sheets("Table ENG SG") _
.Range("C9:C44").Copy _
Destination:=Workbooks("Finalize").Sheets("sheet1").Range("C9:C44")
End Sub
The help for this can be found by opening the Object Browser in VBA (press F2), scrolling down to the Range object, clicking Copy from the list in the right pane then press F1.
The [space][underscore at the end of lines is the line continuation key combination in VBA allowing you to split long lines of code for readability.
If your workbooks are open in separate instances of Excel (i.e. only one is visible in Switch Windows), then copying to the clipboard and selecting Windows as you did in your code is the correct approach.
You would need to add
Range("C9:C44").PasteSpecial xlPasteAll
in place of
ActiveSheet.Paste
to get the result into the desired range
Ranges don't have a Paste method - only PasteSpecial.
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.. ;)
I'm having a bit of a trouble (once again...) with Excel VBA.
I just want to copy Shapes (which are in fact "templates" shapes, with pre-defined layout) from a Worksheet to another Worksheets.
When I record a macro, this is the generated VBA code :
Sheets("Layout").Select
ActiveSheet.ChartObjects("CHART_TEMPLATE").Activate
ActiveChart.ChartArea.Copy
Sheets("Example").Select
Range("A1").Select
ActiveSheet.Paste
Of course, I want to get rid of all ".Select" methods, for performance issue. For this reason, I can't Select a range, neither use ActiveSheet.Paste
Unfortunately, the Paste method only exists for Worksheet objects (like ActiveSheet.Paste), so I had to use the PasteSpecial method of Range objects.
So here's my code :
ThisWorkbook.Sheets("Layout").ChartObjects("CHART_TEMPLATE").Copy
ThisWorkbook.Sheets("Example").Range("A1").PasteSpecial Paste:=xlPasteAll
But, the PasteSpecial method copies Shapes as pictures...
Of course I don't want pictures, because I have to populate those Chartswith data.
Does someone have a clue here ?
Thanx,
The Paste method can take a destination as an argument. Have you tried something like this?
ThisWorkbook.Sheets("Example").Paste Destination:=ThisWorkbook.Sheets("Example").Range("A1")