excel paste special function in vba not pasting cell widths - vba

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.

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)

VBA Copy Paste Special

I am trying to copy a range of cells from Sheet "Stream" to the Sheet "General".
Somehow my paste function is not working. Any hint?
Stream.Range("F103:J103").Copy General.Cells(General.Range("F2:J2").PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:=_False, Transpose:=False)
Sheets("Stream").Range("F103:J103").Copy
Sheets("General").Range("F2").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
For value pasting, you need to first copy the range and then paste to destination.
Also you have to use Sheets() to identify different sheets.

Excel Macro is only copying "false" value of a logic statement

I used the Macro Recorder to create a macro to copy files from one worksheet to a second worksheet and reset a form to default values.
When the macro is run the "false" value ("NO") of a logic statement is copied regardless of the value in the cell at the time the macro is run. If I change the value in the false statement from "NO" to any other value (i.e. "Blue") it copies over the new value ("Blue").
Here is the formula for the logic statement:
=IF(AND(D15>VLOOKUP(C13,CCT,3,FALSE),D15<VLOOKUP(C13,CCT,4,TRUE)),"YES","NO")
where CCT is a list.
Here is the code for the macro:
Range("D17").Select
Selection.Copy
Sheets("Worksheet2").Select
Range("G3").Select
ActiveSheet.Paste
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Worksheet1").Select
You might try Worksheet.Activate after you select different sheets. Worksheet.Calculate might be in order as well.
I think that using Range... implies Activesheet.Range. Hope it helps

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 - 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.