VBA Paste function throwing error after VBA Clear function - vba

I have an advanced filter that is being used to sort a large data set.
The filter is dropping the filtered data into a separate sheet.
I have a VBA Macro that allows me to highlight the portions of the filter that I want to use and paste it into a range adjacent to the filter table.
Currently I am using very simple VBA.
The copy of the active selection and paste into the next open row after a specified Cell. The cell is a row of headers that corresponds to the headers of the table that the copy selection is being made from.
Sub CopyPaste()
Selection.Copy
ActiveSheet.Range("J6").End(xlDown).Offset(1, 0).Select
ActiveSheet.Paste
End Sub
The clear is very simple.
Sub ClearTable()
ActiveSheet.Range("J7:O100").Clear
End Sub
After the clear is run, I receive an error.
Get Run Time error '1004, Application-defined or object defined error.
EDIT: Clarification. .clear and .clearcontents both lead to the errored state If I attempt to paste after I clear the range.

With J7:O100 cleared, select J6 and tap [ctrl]+[down arrow]. Try and go one more row further down. That is where you are trying to paste and if you follow my directions the problem should be perfectly obvious.
Use,
Selection.Copy Destination:=ActiveSheet.Cells(Rows.Count, "J").End(xlUp).Offset(1, 0)
This looks from the bottom up and selects 1 row down not from the top down (which seems to be J1048576 and cannot be moved down a row let alone having room for the paste).

Related

VBA Code to copy and paste active column with merged cells inside

I'm trying to copy the active column and paste it next to it but the code selects the entire worksheet because it has merged cells in.
Sub CopyPaste()
Columns(ActiveCell.Column).Selection
Selection.Copy
ActiveCell.Offset(0,1).PasteSpecial Paste:=xlPasteAll
End Sub
Could you please help me adding the missing code to ignore merged cells?
This is yet another reason to avoid using Select in VBA for Excel. Your selection will expand with the merged cells. You can try this:
ActiveCell.EntireColumn.Copy ActiveCell.Offset(0, 1).EntireColumn
And again, you should find some way to avoid counting on the ActiveCell in your code, and use some fully qualified range.

How do I freeze the active row in excel with VBA?

Like the title says, I need to write a VBA code that copies the entire row i selected and pastes only the values so the results cannot be changed afterwards.
I have already managed to do this for the ActiveCell in the last file I worked on, but I only had to change one cell then. This is the code I used:
Sub Freeze()
ActiveCell.Copy
ActiveCell.PasteSpecial Paste:=xlPasteValues
End Sub
However, for this new file I have to copy the entire row and i don't want to select each individual cell. When I use this in the new file, it only works on the first cell. How can I make it work for the entire row?
Thanks.
Use the simple code below (try to avoid Selecting cells, and active cells if possible):
Sub Test()
Call Freeze(3, 7)
Call Freeze(4, 8)
End Sub
Sub Freeze(rowsource As Long, rowdest As Long)
Rows(rowsource).Copy
Rows(rowdest).PasteSpecial Paste:=xlPasteValues
End Sub
Paste:=xlPasteValues
You do not need to copy paste values unless you are trying to copy formats as well.
Try this. Replace the sheet names with the relevant sheet names. Also Replace X,Y with the relevant row numbers. If you are using multiple rows then change Rows(X) to Range("X:X") and similarly Range("Y:Y")
Thisworkbook.Sheets("Sheet1").Rows(X).Value = _
Thisworkbook.Sheets("Sheet2").Rows(Y).Value

Run time Error - 438

I have the following code in which I am trying to copy data from one sheet to another in same workbook. When I run the code I get Runtime error -438
Sub Copy()
Sheets("Sheet1").Range("A1:D20").Copy
Sheets("Sheet2").Activate
Range("E1").Select
ActiveSheet.Paste
Application.CutCopyMode = False
End Sub
Try the following code. You should not rely on Activate and Select.
Sub ZCopy()
Sheets("Sheet1").Range("A1:D20").Copy
Sheets("Sheet1").Paste Destination:=Worksheets("Sheet2").Range("E1")
Application.CutCopyMode = False
End Sub
Interesting Reads
MSDN
How to avoid using Select in Excel VBA macros
Do you have a particular need for copy and paste? This can be slow and inefficient. If you're just copying data from one sheet to another, you can set the values of one range equal to the values of another range and avoid the whole thing.
Sheets("Sheet2").Range("E1:H20").Value = Sheets("Sheet1").Range("A1:D20").Value
This will set the range from cells E1:H20 on Sheet2 to the same values as those in the range A1:D20 on Sheet1, which is effectively a copy and paste. I should add that this will work only for the values themselves.
If there is specific formatting (or formulas) that you need copied and pasted, this method won't work.

Excel 2010 PasteSpecial method of range class failed

I am attempting to understand why a certain bit of code will not work in Excel 2010.
I have a file where cells A1:K200 are part of a table (the Format as Table option form the Home tab), but rows 2-13 are hidden for use with data validation combo boxes. Header row and row banding options are enabled. What I want to do is copy (via the user pressing Ctrl-C) and paste (via user pressing Ctrl-V) data from another workbook and then have a macro function in Worksheet_Change that will transpose the data (based on the value of a checkbox on the worksheet) and paste only the values. Columns A-H will be filled out with other data.
I have disabled events and screen updating, set up a range for cells I14:I200 as StdCost for an intersect function and then have the following code to trap changes to the above range. optTranspose is a checkbox I have placed on the sheet and Target is the variable set up by Excel as part of the Worksheet_Change function.
If Not Application.Intersect(Target, StdCost) Is Nothing Then
If Application.CutCopyMode = xlCopy Then
Application.Undo
.Range("I" & Target.Row).PasteSpecial Paste:=xlPasteValues, Transpose:=optTranspose
End If
End If
The above code will work, but based on my testing it will only work with a maximum of 4 values being pasted in OR it will work if I paste starting on row 15 or below. However, if I paste 5+ values I get the error from the subject
PasteSpecial method of range class failed
From my testing, what appears to happen is that before the undo operation kicks in, Excel automatically adds Column1 and Column2 to columns L and M, which then causes Target to take on "Column1" and "Column2" and thus ends up causing the error when I try and run it on the actual values I want to paste. If I convert the table to a normal range, everything appears to operate as expected.
I want to understand why "Column1" and "Column2" are being inserted by Excel, why it's only at 5+ cells from another spreadsheet that it becomes and issue and how I might be able to account for this in my code.
I want to understand why "Column1" and "Column2" are being inserted by Excel,
This happens when you paste data adjacent to an existing ListObject table.
The change event occurs (i.e., you "Paste" the data), the worksheet changes, and then, the Worksheet_Change event fires.
The error seems to happen because Application.Undo is undoing the Paste operation and further affecting the Application.CutCopyMode = False, so on the next line when you attempt to use the PasteSpecial method, there simply is nothing that can be pasted, because the Undo has cleared it.
Pastespecial seems to be very sensitive to the parameters it's fed.
I was experiencing exactly the same error message from code that had worked before migrating to Windows 10. I found this combination of parameters worked in my case:
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Can't claim that I found that combo of parameters- it came from yactici's self answer
Kudos to yactici for returning to his initial problem despite having a workaround and directly resolving the problem with pastespecial.

Clear all data after copying it but leaving all functions and formulas

I'd like to either clear the sheet after copying the content of the cells but without deleting the Excel functions, or cut everything from the current region but again, leaving functions in columns H and I intact.
The code is as follows and works well but I don't know how to clear the sheet and leave the functions intact. The functions are in columns H and I
Sub archive_old_data()
'copy Admissions sheet into Archive sheet
Application.ScreenUpdating = False
MsgBox "Please make sure you've reviewed all patients before archiving data."
Worksheets("Admissions").Activate
Range("A1").CurrentRegion.Copy
Worksheets("Archive").Activate
Range("A2").End(xlDown).Offset(1, 0).Select
ActiveCell.PasteSpecial
Worksheets("Admissions").Activate
End Sub
I would appreciate some help.
I don't have to copy currentregion, I need whatever is in cells A2:E2, but the problem is that next time I copy it might be A2:E9, another time A11:E11
If you just want to remove all number-values from a range you can use:
Range("A1:D4").SpecialCells(xlCellTypeConstants, 1).ClearContents
To clear both text and numbers (but not formulas) use SpecialCells(xlCellTypeConstants, 3).
If you are not sure of the specific range then you might apply this to entire columns, or Cells for the entire sheet.
If there is the possibility that there won't be any values to delete then you should include some error-handling, otherwise the error 'No cells were found' appears.