XLS - Copy & Paste in VBA - PasteSpecial Method Fails - vba

I'm struggling with a nagging issue. I am trying to simply copy and paste a collection of cell forumlas in an XLS worksheet using VBA. The worksheet (wks1) is created and populated from an AccessDB and is working fine otherwise.
Error: "PasteSpecial Method of Range Class Failed"
wks1.Range("P5:S5").Copy
wks1.Range("P5:S10").PasteSpecial _
Paste:=xlPasteFormulas, _
Operation:=xlNone, _
SkipBlanks:=False, _
Transpose:=False
I've attempted a number of variations, but keep bumping into this err msg.
Any suggestions to get this working?

Do this instead:
wks1.Range("P5:S5").Autofill wks1.Range("P5:S10")
or
wks1.Range("P5:S10").formula = wks1.Range("P5:S5").Formula
For the paste special, it has been my experience that less is more:
wks1.Range("P5:S5").Copy
wks1.Range("P5:S10").PasteSpecial xlPasteFormulas
But when only values or formulas are wanted why include the clipboard? It is faster and cleaner to just assign them directly. So I would use the copy/paste when more than the values or formulas are wanted.

Related

Vlookup range with a defined variable

I'm consolidating data from several excel files, I have 22 files to consolidate; however, I need to specifically look up for a cell value.
In my VBA code I declared a variable called file=int(1) and I'm doing a Loop to open and consolidate each excel file to a consolidated file. All of my excel files are numbered from 1 to 22.
What I want to to is Vlookup, but the range of the Vlookup I want it to have the stored value in file while doing the loop.
For example
file = Int(1)
Do Until file = 22
ActiveCell.Formula = "=IFERROR(VLOOKUP($I$6,'[file.xls]Sheet1'!$B$4:$V$27,MATCH($F$26,'[file.xls]Sheet1'!$B$3:$V$27,0),FALSE),0)"
ActiveCell.Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
ActiveCell.Select
Selection.Offset(1, 0).Select
file = file + 1
Can you please help me?
Thanks!
Nice tidy little loop! OK, I think I have an idea what you're trying to do here. You want to have the [file.xls] in your formula update dynamically for each iteration of the loop. Since you're using the formula property, which takes a string, you can concatenate your stored file value right into it.
This should do the trick.
ActiveCell.Formula = "=IFERROR(VLOOKUP($I$6,'[" & file & ".xls]Sheet1'!$B$4:$V$27,MATCH($F$26,'[" & file & ".xls]Sheet1'!$B$3:$V$27,0),FALSE),0)"
Hope I understood properly, if not, we'll go another round!

VBA - Copy Values Only

Following on from my previous question, which was answered perfectly I have now written code for the remaining part of my problem, however I have now developed problems.
Part of the worksheet uses =RAND() to generate a random number. As this is a volatile function I needed to copy the output of the formula to a new location. If I was doing this manually I would do a copy -> paste special values so that I just go the numbers, and not the formula.
When trying this in VBA I get an Error 1004 during part of the code when I try to select the destination range for the paste special.
Here is the code:
' Copy Random Questions to Static Page for VLOOKUPS
With Worksheets("Quiz Generator")
Range("NEWQUEST").Copy
'Selection.Copy
End With
With Worksheets("Static Question List")
Range("TOPSTAT").Select
Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End With
I've tried various ways of doing this, using
RANGE().Select
Selection.Copy
RANGE().Select
Selection.Pastespecial Paste:=xlValues
Also using:
RANGE().Copy
Range().PasteSpecial Paste:=xlValues
In the original code I can get through to the
Range("TOPSTAT).Select
Before it throws the
Run-Time error '1004':
Application-defined or object-defined error
Pop-up
Any help would be gratefully received.
All of the defined ranges are correct and in Name Manager, and I've tried with cell ref's to see if it was the range name that was the issue.
Annoyingly this worked previously using the long-hand Select / Selection.Paste etc method, but since trying to tidy the code it stopped.
Thanks in advance.
I would do something like this, to
dump the range from NEWCREST as values into an array
dump the array to the first cell in TOPSTAT and resize as needed
(you don't need sheet names when working with range names unless you have local range names)
Code
Dim x
x = Range("NEWQUEST").Value2
Range("TOPSTAT").Cells(1).Resize(UBound(x, 1), UBound(x, 2)) = x

Macro to copy values only from one sheet to another

I've this VBA code
Sheets("log").Range("A125:f1000").Copy _
Sheets("data").Cells(Rows.Count, "A").End(xlUp).Offset(1)
and it copies perfectly from sheet log to data. The only problem I'm facing is that it copies formulas with it as well whereas I only want values. I want to use same VBA code with some modifications to paste values only.
Without using clipboard:
Sheets("data").Cells(Rows.Count, "A").End(xlUp).Offset(1).Value = Sheets("log").Range("A125:f1000").Value
Need to add PasteSpecial Paste:=xlPasteValues
Next time try Recording a macro and modifying the code
Sheets("log").Range("A125:f1000").Copy
Sheets("data").Cells(Rows.Count, "A").End(xlUp).Offset(1). _
PasteSpecial Paste:=xlPasteValues, _
Operation:=xlNone, SkipBlanks:=False, Transpose:=False

Macro - Copy and Paste

My company only uses MS Office 2003 products so I have to stick to it. Because of the nature of my job, i need to use a lot of "copy and paste" function. The source data is mostly from the website and i paste the data into a cell in Excel. The problem is clipboard keeps source formatting and it reflects on the cell when i paste it. That is really troublesome to remove the source format by selecting option "Paste as Text" every time i user copy and paste. SO i decided to use Macro. The Macro works perfectly when i try to copy and paste from website to excel, but when i copy and paste from Excel to the same work Sheet it throws an error.
This is the code i use for copy and paste from website to excel without source formatting.
Sub Paste_without_any_formatting()
ActiveSheet.PasteSpecial Format:="Text"
End Sub
I want to add another code for copy and paste from excel to the same work Sheet like this.
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
How can i put these two codes together? I want to make these happen magically when i press Ctrl+V. Can anyone help me?
This is the most simplest way to achieve it.
Sub Sample()
On Error GoTo Whoa1
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Exit Sub
Pastetext:
On Error GoTo Whoa2
ActiveSheet.PasteSpecial Format:="Text"
Exit Sub
Whoa1:
Resume Pastetext
Whoa2: '<~~ If both Paste method fails then show message. Ex: Image in Clipboard
MsgBox err.Description
End Sub
There is one more way where you try to ascertain the clipboard data type and then paste accordingly.

Copy and paste from one workbook to another

I have code in one workbook, this should open another workbook, copy and paste into the workbook with the code. I can select the data but can't paste it.
I have tried many different variations of code getting errors or it doesn't do anything. An example is run in template.xls, which is where I want to paste the data:
Set dlsheet = appexcel.Workbooks.Open(strPath & "downloadedData.xls")
With dlsheet.Sheets("Data")
.range("A1:H3").Select.copy
selection.copy
End With
I don't know how to use the selection since this will copy from the template, I tried using a full stop before selection.
I can copy the entire sheet from dlsheet into a new workbook, if someone could tell me how to copy it to the template and not a new workbook then this would also do the trick.
dlsheet.Sheets("Data").Copy
Set dlsheet = appexcel.Workbooks.Open(strPath & "downloadedData.xls")
dlsheet.Sheets("Data").range("A1:H3").copy
ThisWorkbook.ActiveSheet.Paste Destination:=ThisWorkbook.ActiveSheet.Range( "A1:H3")
Try this
Set dlsheet = appexcel.Workbooks.Open(strPath & "downloadedData.xls")
With dlsheet
.Sheets("Data").Range("A1:H3").Copy
.Sheets("Data").Range("A1").PasteSpecial Paste:=xlPasteValues, _
Operation:=xlNone, SkipBlanks:=False, Transpose:=False
End With