VBA - Copy Values Only - vba

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

Related

Excel VBA Error 1004: Application-defined or object-defined Error

I have a macro that copies data from one sheet and pastes values to another. Everything seems good but I received the error:
Run-time Error 1004: Application-defined or object-defined Error
I simplified the code for your convenience because the whole macro is a set of similar codes:
Sub CopyPaste()
Sheets("Primary").Select
Range("A1").Select
Selection.Copy
Sheets("Result").Range("A2").End(xlToRight).Offset(, 1).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End Sub
The debugger shows the problem with the row:
Sheets("Result").Range("A2").End(xlToRight).Offset(, 1).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
I really don't understand what's wrong here. Almost the same code works with another Excel spreadsheet.
I went through similar questions here but couldn't find any that help me.
So, maybe it's easy for VBA Professionals but not for me. It's only my second week using VBA, for this reason any help is very appreciated.
As #DavidZemans said in comments...
If row two on the Result sheet is empty (or completely full) then xlToRight will get to column XFD and then try to offset one column off the sheet which causes the error.
It's better to start at XFD and look to the left - if the row is empty it will return the first column. So:
sheets("Result").cells(1,columns.Count).end(xltoleft)
Also, as you're only copying the value you can just say "set this cell value to equal that cell value".
Sub CopyPaste()
Sheets("Result").Cells(1, Columns.Count).End(xlToLeft).Offset(, 1) = Sheets("Primary").Range("A1")
End Sub
Edit: Nearly forgot - if column XFD is populated then this could also muck up (add a value to that column and press Ctrl+Left and it selects column A)

Macro : Copy Range to Next Consecutive Blank Column(s)

everyone. Newbie here.
I have tried to the best of my knowledge and ability to go through the hundreds of macro codes that allows the user to specifically click a button to copy and paste a range into the next blank column, and keep pasting new columns as long as the user clicks the button. Unfortunately, the small variations in the purpose of the codes created will differ. I am unable to understand why.
I have managed last night to hit the jackpot and make it work flawlessly, until it crashed for some reason just as I saved the file. It didn't work after that.
The objective: for analysis purposes, the range in C9:C43 will always be there as a base year data. If the user has one year worth of data, then no need to copy. If the user has 5 years worth of data, the user will click the button 5 times. The copy will include formats, formulas, and column widths.
Furthermore, considering that the range in column C is the base, cell C9 will resemble the Year i.e. 2015. If the user changes C9 to, say, 2010, the following column will have D9 = 2011, and so on.
I thought a dialogue box will be a better representation of the objective I'm working on, but seems to be far-fetched with my current understanding of Macros and VBA.
Here is the code I used last night that got it working before it crashed mysteriously after saving the file:
Dim rngSource As Range
Dim rngDestination As Range
Set rngSource = Range("C9:C43")
Set rngDestination = Cells(9, Columns.Count).End(xlToLeft).Offset(0, 1)
Range("C9:C43").Copy
Range("D9").Select
Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Selection.PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Selection.PasteSpecial Paste:=xlPasteColumnWidths, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Range("D9").Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = "=RC[-1]+1"
rngSource.Copy destination:=rngDestination
You guys are amazing and your work and cooperation is deeply appreciated.
Warm regards,
While this doesn't answer your question about freezing code, I believe it may help you with your fundamental problem of copying columns in a series
If you use cell A1 for a user to specify how many columns they would like to add, then a button press could call range.autofill with a series fill (this works for the year but I'm not sure what you have underneath that top row)
Sub Button_Click()
Dim x As Integer
x = Range("A1").Value
Range("C9:C43").AutoFill Destination:=Range("C9:" & Cells(43, x + 3).Address), Type:=xlFillSeries
End Sub

XLS - Copy & Paste in VBA - PasteSpecial Method Fails

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.

Macro Copy&Paste

I'm trying to create a macro that will copy data from one worksheet and place into another. This I can do with no problem. But, when I want to use the same macro in another row is where I have my problem. Basically what I want to do is copy cell D11 from sheet1 and place that in cell B4 on sheet2, etc (What I'm doing is obviously more complicated than that, but that doesn't matter here).
My problem is when I want to now run this macro and copy cell D12 from sheet1 and paste into B5 on sheet2 the value pasted jumps to B4. I understand that this happens because of where the VBcode is saying to paste the copied value.
My question is how to I just have it paste in whatever row I choose? Maybe based on what row/cell I have selected.
Current code, written by recording the macro
Sheets("sheet1").Select
Range("D11").Select
Selection.Copy
Sheets("sheet2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("B4").Select
I'm assuming the last line is where I need to make the change, but I'm not sure what to change.
Thank you! Any and all help is greatly appreciated.
As a general rule, try to avoid Selection Copy-Paste (detailed discussion is provided in: "Application.Calculation = xlCalculationManual" statement causing run-time error 1004 in VBA Copy-Paste procedure). Instead, use direct copy statement, which will solve you issue and significantly improve performance:
Listing 1.
Sub DirectCopySample()
Application.ScreenUpdating = False
Sheets("Sheet1").Range("D11").Copy Destination:=Sheets("Sheet2").Range("B5")
Application.ScreenUpdating = True
End Sub
Sub in Listing 1 performs direct copy from Cell: Sheets("Sheet1").Range("D11") into cell: Sheets("Sheet2").Range("B5").
Also, your initial Copy-Paste Sub could be simplified (it will also make it work, though Listing 1 is preferred)
Listing 2.
Sub CopyPasteSample()
Sheets("sheet1").Range("D11").Copy
Sheets("sheet2").Range("B5").PasteSpecial Paste:=xlPasteValues
End Sub
Hope this will help. Best regards,
You seem to have recorded a Macro and are trying to replay it. Here is a real VBA code (not a Macro recording type):
Sheets("sheet2").Range("B5") = Sheets("sheet1").Range("D11").Value
This is all!
BTW, your predicament comes from the fact that the PasteSpecial method copies into the currently selected cell. You've tried running this Macro several times and the Range("B4").Select line did the trick. If you insist on your approach the insert Range("B5").Select BEFORE the PasteSpecial.

excel paste special function in vba not pasting cell widths

I'm running a macro to copy from one sheet and past to another however, it is not pasting the cell width. If I do this manually it works i.e. copy the column and paste special > all. the vba code for the macro is located here: http://pastebin.com/K2sW1C8x
as you can see i'm using :
Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _False, Transpose:=False
The paste of the column widths need to be done in a separate action:
Selection.PasteSpecial Paste:=xlColumnWidths
This command occasionally threw an error in previous versions of Excel. If you get an error, use the number 8 in place of xlColumnwidths.