Copy and paste into next available row ignoring formatting - excel

I'm looking to copy information from one page to another into the next available row. The number of rows of data will change between saves. I currently have the following code:
Sub CFSNCAll()
r = 1
Sheets("CFSNC_All").Range("C12:T298").Copy
If Sheets("CFSNC_All").Range("C12") = Sheets("Sheet1").Range("A65536").End(xlUp) _
Then r = 0
Sheets("Sheet1").Range("A65536").End(xlUp).Offset(r, 0).PasteSpecial _
Paste:=xlPasteValues
Application.CutCopyMode = False
End Sub
This works perfectly in an excel sheet with no formatting. My Excel sheet has borders, conditional formatting in two columns in the range, and gridlines turned off. Currently, when you run the macro the second time, the data pastes into the right sheet, but on the 298th row. I think it's registering information in the previous cells from formatting. Is there a way to ignore the formatting and ensure that it pastes below the entry?

Related

VBA Copy/Paste a Range in Next Available Column Then Add a Single Day to One of the Newly Pasted Cells Repeatedly

I'm trying to create a button with an attached macro that will copy a range with formulas and data, then paste it to the next available column, and finally add one day to one specific cell. The problem I'm having is that I want to do it over and over, with the each new pasted cell adding one date from the previously pasted one. The idea is that I click the button, it pastes the orginal range plus one day added to that specific cell, then I can add data to that new pasted block, then click the button and get a new pasted block with the next specific cell having one day added to it.
So far I have this:
Sub PasteToNextEmptyColumn()
Application.ScreenUpdating = False
Range("A4:C14").Copy
ActiveSheet.Cells(4, Columns.Count).End(xlToLeft).Offset(0, 1).PasteSpecial xlPasteColumnWidths
ActiveSheet.Cells(4, Columns.Count).End(xlToLeft).Offset(0, 1).PasteSpecial xlPasteAll
Range("E9").Value = DateAdd("d", 1, (Range("E9")))
Application.CutCopyMode = False
Application.ScreenUpdating = True
End Sub
I'm a little unsure on what you're trying to accomplish, but if this helps.
With a date you can treat it like a value and just put:
Range("E9") = Range("E9") + 1 ' this will input the date in the same cell E9
Maybe ensure the date is inn DD/MM/YYYY or another recognised format.
It seems as though the pasting is correct, if you wanted the date to be input alongside the new data added that is a little different using the last column formula you've already used.

Pasting only Background/Number Formatting and Values with Merged Cells

I am attempting to copy a specific region's contents and copy the Current Region back to a summary page.
I have unfortunately run into a bit of an issue with pasting the data back to the summary sheet.
Desired Outcome
Paste the following from my copied region to the summary page:
Maintain Merged Cells
Maintain Cell Highlight Colors
Cell Values
Copy Source
The source area has some formulas within it to gather some sheet data. See Picture Below:
Now whenever I go to paste the selected region, via means of my vba code
TitleBlockRange.CurrentRegion.Copy
nextEmptyCell.PasteSpecial (xlPasteFormats)
nextEmptyCell.PasteSpecial (xlPasteValues)
I get the following error:
I have tried every single variation of PasteSpecial() that I can think of!
Is there a method that I am missing here?
What method should I be using?
Thank you all in advance!
The curse of merged cells. But if you are stuck with them the code below will copy everything - just slower.
Sub CopyEverything()
' 153
Dim SrcRng As Range ' Source range
Dim Target As Range ' destination range
Dim i As Long ' loop counter: index of SrcRng.Cells
Set SrcRng = ActiveSheet.Cells(1, 1).CurrentRegion
Set Target = Worksheets("Sheet2").Cells(10, 1) _
.Resize(SrcRng.Rows.Count, SrcRng.Columns.Count)
Application.ScreenUpdating = False
With SrcRng
.Copy
Target.PasteSpecial xlPasteFormats
For i = 1 To .Cells.Count
With .Cells(i)
If .Address = .MergeArea.Cells(1).Address Then
Target.Cells(i).Value = .Value
End If
End With
Next i
End With
With Application
.CutCopyMode = False
.ScreenUpdating = True
End With
End Sub
Bear the following in mind.
To copy row heights you must copy entire rows.
To copy column widths you must copy entire columns.
The above task would be easier if you could copy entire rows or entire columns, or the entire sheet. However, if you deploy the above code you already have the infrastructure to copy individual settings and it shouldn't take more than a few extra lines to add a transfer of those dimensions which are important to you.
As per my knowledge, there is no way to copy complete range & paste with same formatting (merged cells, highlight colours & values). To meet requirements, you can copy & paste merged cells individually & un-merged cells separately.
For merged cells, use below codes
Range("B4").MergeArea.Copy
range("").Pastespecial
Using Pastespecial without any conditions will paste xlformats, xlvalues.
So it seems that if you use :
nextEmptyCell.PasteSpecial (xlPasteAll
nextEmptyCell.PasteSpecial (xlPasteValues)
It will throw error '1004' as shown in the original post, however if you change it to the following:
nextEmptyCell.PasteSpecial (xlPasteAll)
nextEmptyCell.PasteSpecial (xlPasteValuesAndNumberFormats)
We are able to overwrite the formulas from the xlPasteAll
The only other issue now is the annoyance of the following "pop-up" whenever we get to the nextEmptyCell.PasteSpecial (xlPasteValuesAndNumberFormats) line of code.

Find cell & select/copy from active cell to last row including blanks

I'm attempting to find a column using the column header name, then select all the data from the column (including the blank cells) & paste into another range.
Currently I can only copy until the 1st blank cell. I have seen similar problems on the board but the solutions I have seen are coming from the angle of knowing which column it is in first & then finding the last row from the bottom of the worksheet.
Workbooks("PS & Config - Actuals & FC.xlsm").Worksheets(2).Range("A3").CurrentRegion.Find(What:="FFA Name").Select
Range(ActiveCell, ActiveCell.End(xlDown)).Select
Range("A3").Select
ActiveSheet.Paste
To find the column use Match wit the column header name. Once you have that column, set the values in the destination range to be the same as that column. Selecting, copying, and pasting in VBA are unnecessary as the values themselves can be moved--plus it adds load.
I don't have Office anymore so I'm running this from memory. I hope it helps.
dim wb as workbook, ws1, ws2 as worksheet, myCol, myRow as long
set wb=excel.thisworkbook 'assuming this code goes in that workbook
set ws1=wb.sheets(2) 'set variables
myCol=worksheetfunction.match("FFA Name",ws1.[a1:zz1],0) 'search through 1st row
myRow=ws1.[a3].currentregion.rows.count 'grab last row containing data in this set
'do not select, nor copy and paste, if it can all be done with VBA. This can.
set ws2=wb.sheets.add 'a little rusty on this line, you can get the method from the macro recorder
ws2.cells(1,1).resize(myrow).value=ws1.cells(1,myCol).resize(myrow).value
The reason your original code stops when there's a blank cell is because the .end() method simulates pushing the end key and then an arrow, which goes to the first/last contiguous cell with data.
Range(ActiveCell, ActiveCell.End(xlDown)).Select

Excel VBA copy filtered CurrentRegion visible cells but exclude last 3 columns

Having a small issue. I have a large piece of code that takes a reference number selected by the user and locates corresponding rows (there can be multiple rows or none to locate) on multiple other sheets by filtering and then copying the data.
This works well except it copies all visible data (columns A-N) when I really want it to copy columns A to K (as L to N on the paste sheet is where I have formulas that set reference numbers and therefore can't be pasted over).
I have tried a couple of changes to the below section of code including offset however it either ignores the offset (probably because I'm using xlCellTypeVisible which I had to do as the data that needs to be copied can be across multiple non sequential rows) or I get an error about selection method not supported.
Any thoughts?
sheet that is being copied - DbExtract
Sheet that data is pasted to - DuplicateRecords
Thanks.
Sub UpdateInputWithExisting()
' Other code that works using set with values and active cell offset with values for other sheets
Sheets("TK_Register").Range("A1").CurrentRegion.AutoFilter field:=12, Criteria1:=RefID
Dim DbExtract, DuplicateRecords As Worksheet
Set DbExtract = ThisWorkbook.Sheets("TK_Register")
Set DuplicateRecords = ThisWorkbook.Sheets("EditEx")
DbExtract.Range("A1").CurrentRegion.SpecialCells(xlCellTypeVisible).copy
DuplicateRecords.Cells(31, 3).PasteSpecial xlPasteValues
On Error Resume Next
Sheets("TK_Register").ShowAllData
On Error GoTo 0
ActiveWorkbook.RefreshAll
Sheets("EditEx").Select
ActiveWindow.SmallScroll Down:=-120
Range("B14:M14").Select
MsgBox ("Record Retrieved. Make your changes and ensure you click 'Save Changes' to update the Master Registers")
End Sub
Need to use .Resize method. Replace this line:
DbExtract.Range("A1").CurrentRegion.SpecialCells(xlCellTypeVisible).copy
With this:
With DbExtract.Range("A1").CurrentRegion
.Resize(, .Columns.Count - 3).SpecialCells(xlCellTypeVisible).Copy
End With

Copying values from one Table to the first empty row in another Table

As part of my project I have a Table which includes lookup formulas in each column that are dragged down the whole table. Depending on the case only the first x rows return values. I included an iferror so that the lookups that don't return values return "".
Now I want to copy the rows of the table that return values to the first empty row in a different table in a different worksheet.
The code I have so far:
Sub Copy_Results()
Application.ScreenUpdating = False
Dim copySheet As Worksheet
Dim pasteSheet As Worksheet
Set copySheet = Worksheets("Sheet1")
Set pasteSheet = Worksheets("Sheet2")
copySheet.Range("Table1").Copy
pasteSheet.ListObjects("Table2").Range.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues
Application.CutCopyMode = False
Application.ScreenUpdating = True
End Sub
Now the big problem is that I want to be able to execute this macro multiple times, each time the values from Table 1 should be pasted below the preexisting values in table 2.
The point being that each time the lookup values change meaning I get new results I want to paste them in a table where all the results are documented.
Issues that I had so far:
The first Copy Paste usually works, but when I copy again the values get pasted way below the first ones outside of the table. Usually the full length of the table away. I guess this is because the whole copy table is filled with formulas.
The easiest way to do this is to restrict the cells that you are going to copy using the SpecialCells method:
https://msdn.microsoft.com/en-us/library/office/ff196157.aspx
In this case you only want to copy the formulas that have numbers as the values, so this would be the syntax:
SpecialCells(xlCellTypeFormulas, xlNumbers)
Put into your code it would be:
copySheet.Range("Table1").SpecialCells(xlCellTypeFormulas, xlNumbers).Copy
You can see this in action outside of your code by selecting the complete range in your source sheet then pressing F5, selecting the "Special" button at the bottom of the dialog that pops up, then select "Formulas" and "Numbers".
To make sure it pastes in the next available row, use the CurrentRegion property:
https://msdn.microsoft.com/en-us/library/office/ff196678.aspx?f=255&MSPPError=-2147217396
This code will tell you what the last row is in the used area defined by cell A1:
pasteSheet.cells(1,1).CurrentRegion.Rows.Count
I believe the paste command you're looking for will be close to this (hard to test exactly without your spreadsheet):
pasteSheet.cells(pasteSheet.cells(1,1).CurrentRegion.Rows.Count + 1, 1).PasteSpecial xlPasteValues

Resources