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

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.

Related

Copying and pasting a cell into the next empty row in a column

I am copying and pasting some data into an excel sheet. I have three different cells that I need copied and pasted into their corresponding three new cells. I want to be able to paste them into the sheet, and run a macro that copies and pastes them automatically into the empty row and column that I specified. I'm going to need to copy and paste them into the next subsequent empty row out of the same column the next time, so I need to update it somehow or have a condition for it to parse down the column to the next empty cell. I have run this code shown and it gives me a "Subscript Out of Range Error" or a "Can't Jump to Sheet" error. I'm not quite sure what to do.
Sub CopyStuff()
Range("A2:C2").Copy
Sheets("Sheet11").Range("A" & Rows.Count).End(xlUp).Offset(1,0).PasteSpecial xlPasteValues
End Sub
Often times easier to use either name or codename depending on situation.
Either
Thisworkbook.sheet1.range("A1").value 'Name
' -or-
Thisworkbook.sheets(1).range("A1").value 'Index
' -or-
Thisworkbook.sheets("Sheet2").range("A1").value 'Codename
Anway... double check whether you're trying to reference sheets(11), sheet11 or sheets("Sheet11")
Sub CopyStuff()
Sheet11.Range("A" & Rows.Count).End(xlUp).Offset(1, 0).Resize(1, 3) = _
ActiveSheet.Range("A2").Resize(1, 3).Value
End Sub

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.

Insert a Row into Excel but keep its current formatting

I have a Sum on specific Cells (lets say "A1:A5") in my Excel worksheet. Usually, whenever I insert a row manually on "A1", The sum is expanded, so the new Range is "A1:A6". Now when I insert a row using VBA at "A1", my sum goes from "A2:A6", not "A1:A6". How can I insert a row but basically keep the current formatting (so that the sum is automatically expanded)?
My code I use for inserting:
dest.Rows(index).Insert shift:=xlShiftDown
dest.Rows(index).PasteSpecial xlPasteValues
where dest is the worksheet I want to expand.
Edit:
I tried to use a macro and use that code, that did sadly not work.
The generated code:
Rows("167:167").Select
Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
My new code:
dest.Rows(index).Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
dest.Rows(index).PasteSpecial xlPasteValues
That did not change the outcome.
Edit2:
I am going to provide a picture to clarify what I want to do:
Whenever I manually add a line (rightclick line 152 -> insert cells) this little table is expanded and everything is shifted down, so lets assume:
I have a SUM in cell B,180 that goes from B:50 to B:179, now the SUM is in cell B,181 and goes from B:50 to B:180. Also, the formatting of the table is used for the new line (as far as the table goes, so columns A:K are having the background/borders of the cells above.
When I use my code for that, it is just a new blank line inserted without keeping the formatting.
For me this worked:
Sub copyExpanded()
Dim rng As Range
Set rng = ActiveSheet.Range("A5")
rng.Offset(1).EntireRow.Insert
rng.EntireRow.Copy rng.Offset(1).EntireRow
End Sub

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

VBA User Form - Need it to copy formula from column F, 1 row above

I have a form I made for users to enter data. The data get's inserted into the next available row. That works well, but now I need the formula from the cell above in Column F to be copied into the new cell. Is there a code I could use this to work along with the copying of the input values to the sheet?
Any help with this would be greatly appreciated.
Edit: Method 2 is probably better suited for your purpose as it will adapt the cell references in the previous line to the next line.
Method 1: To set the formula of a cell, use
Sheets("sheet name").cells(row, column).Formula = ....
Row and column must be specified as integer values, I.e. For column F it would be 6. Assuming you have the number of the next free line in a variable called NextFreeLine:
Sheets("sheet name").cells(NextFreeLine, 6).Formula = Sheets("sheet name").cells(NextFreeLine-1, 6).Formula
Method 2: If you insist on literally copying the formula from somewhere else, try this (this will adapt the cell references of the formula for the previous line to the next one-- just like copy/pasting it using Ctrl-C, Ctrl-V):
Sheets("sheet name").cells(NextFreeLine-1, 6).Copy
Sheets("sheet name").cells(NextFreeLine, 6).PasteSpecial Paste:=xlPasteFormulas
The method you are using to put into 'the next available row' would have been nice to see but perhaps you can translate this for your own purposes.
'put the newval into the next available cell in column A
cells(rows.count, 1).end(xlup).offset(1, 0) = newval
'copy the formula in column F to the new row
cells(rows.count, 1).end(xlup).offset(-1, 5).resize(2, 1).filldown
Your value goes into the next available row based on column A. The same column is used in much the same manner but offset up a row and resized to two rows in height before executing the fill down command.

Resources