Macro - Copy and Paste - vba

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.

Related

Macro repeat at a time interval and works when spreadsheet is minimized

I wrote the below VBA. It is working but have the following problem. Any help would be appreciated.
Problem:
It does not paste exactly at the time interval assigned.
It stops working when the spreadsheet is minimized - and gives error on Worksheets("IV track").Select.
Sub CopyPaste()
'
' Workbooks("Option Chain.xlsm").Activate
Worksheets("IV track").Select
Range("A14").Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.Copy
Range("A19").End(xlDown).Offset(1, 0).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Sheets("Nifty Analysis").Select
Range("B4").Select
Call Test
End Sub
Sub Test()
Application.OnTime Now + TimeValue("00:15:00"), "CopyPaste"
End Sub
The error when the Excel is minimized is caused because Worksheets(...) has an implied ActiveWorkbook. in front of it. When you minimize Excel, none of the workbooks are active, meaning that you are trying to grab the spreadsheet from an undefined workbook.
The reason that your macro doesn't run at exactly the right timing is that Application.OnTime is not meant to run things at exact times. If there is anything preventing Excel from running the macro, it will not run it immediately. The time you give it is the earliest time that it will run the macro, not a guaranteed time.

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

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.

VBA Copy Paste is Removing Borders

I'm copying and pasting values from one workbook to another using VBA code. However, when I paste, the borders in the destination worksheet are being deleted. How can I maintain the borders when pasting?
Below is my code:
With wsSource
.Range(.Range("A2"), .Range("C2").End(xlDown)).Copy wsDestination.Range("A3")
End With
I have read that the PasteSpecial method could be of use, but I don't know how to implement it in the above code.
Thanks!
Try this
With wsSource
.Range(.Range("A2"), .Range("C2").End(xlDown)).Copy
wsDestination.Range("A3").PasteSpecial Paste:=xlPasteValues, _
Operation:=xlNone, SkipBlanks:=False, Transpose:=False
End With
Whenever in doubt, record a macro ;)