VBA Copy Cells + Formatting without using the clipboard - vba

I'm trying to copy cells from one sheet to another without copying stuff to the clipboard but it must copy the formatting across.
Here are the ways I've tried at the moment, any ideas how to accomplish my needs?
LastRow = ThisWorkbook.Sheets("Nas").Range("A65536").End(xlUp).Row
'Option 1, works but it's using the clipboard :/
ThisWorkbook.Sheets("Nas").Range("A" & 6 & ":F" & LastRow).Copy
ThisWorkbook.Sheets("Test").Range("A" & 6 & ":F" & LastRow).Offset(-5, 0).PasteSpecial
'Option 2, works but doesn't take formatting (ie text, general, time, date... etc)
Set Src = ThisWorkbook.Sheets("Nas").Range("A" & 6 & ":F" & LastRow)
Set Dst = ThisWorkbook.Sheets("Test").Range("A" & 6 & ":F" & LastRow).Offset(-5, 0).Resize(Src.Rows.Count, Src.Columns.Count)
Dst.Value = Src.Value
'Option 3, works but doesn't take formatting (ie text, general, time, date... etc)
ThisWorkbook.Sheets("Test").Range("A" & 6 & ":F" & LastRow).Offset(-5, 0) = ThisWorkbook.Sheets("Nas").Range("A" & 6 & ":F" & LastRow).Values

Your example code defines the formatting to be carried across as "text, general, time, date... etc". While the number formatting (a subset of the Properties of a cell) can easily be accommodated, delving further into enumerating the vast number of properties and subproperties of a Cells object is counter-productive when all relevant (non-default) properties can be carried across easily with a copy/paste operation using the clipboard.
With ThisWorkbook.Sheets("Nas")
LastRow = .Range("A" & .Rows.Count).End(xlUp).Row
ThisWorkbook.Sheets("Test").Range("A6:F" & LastRow).Offset(-5, 0) = _
.Range("A6:F" & LastRow).Value
ThisWorkbook.Sheets("Test").Range("A6:F" & LastRow).Offset(-5, 0).NumberFormat = _
.Range("A6:F" & LastRow).NumberFormat
End With
Note that transferring the value of the cell across is performed with .Value or .Value2 not .Values.
I have seen this question popup occasionally and IMHO, the source of the question is likely a) a teacher that thinks this is a cutesy way to get students to appreciate just how many properties, subproperties and internal sub-subproperties a Range.Cells object (Range Members (Excel)) contains or b) some idiot with a fresh MBA that wants to prove they are smarter than the IT department. Enumerating through every possible formatting property a cell could contain is just a fool's errand (again IMHO) when the clipboard is available.
If you do attempt this yourself, don't forget Conditional Formatting and Comments, both of which can easily be attributed as part of a cell's formatting.

Related

PasteSpecial only values

I want to copy paste a range of cells into a destination sheet, but only their values, not the format. Unfortunately, my code pastes the format, too. I am providing the line of code for this purpose. (I know how to paste special values but I want, if it is possible, to apply this method for this method of copy paste. Normally, I use a different copy paste method, for which I know how to paste special, but for this time I wanted to keep the length of the macro as small as possible).
ws1.Range("C2:C" & lastrow2).Copy ws2.Range("A2")
Would recommend:
ws2.Range("A2:A" & lastrow2).Value = ws1.Range("C2:C" & lastrow2).Value
Try a direct value transfer.
with ws1.Range("C2:C" & lastrow2)
ws2.Range("A2").resize(.rows.count, .columns.count) = .value
end with
'alternately,
ws2.Range("A2").resize(ws1.Range("C2:C" & lastrow2).rows.count, ws1.Range("C2:C" & lastrow2).columns.count) = ws1.Range("C2:C" & lastrow2).value

MS Excel 2013 - crash after editing cell value

I am currently facing a very anoying issue with my Excel workbook, and I would like to ask you for help.
My workbook works as a quotation tool and generates a PDF afterwards.
First, you need to enter data from different sources (for e.g. PDFs, Excel Workbooks, etc.) to a Worksheet called Master. You can do some calculation with simple formulas and can export the results into another sheet called Calc_Overview. In this process, the data is also formatted to the right font, color and size.
With ActiveWorkbook.Worksheets("Master")
For i = 12 To .UsedRange.rows.Count
If .Cells(i, 2) <> "" Then
.Cells(i, 2).Copy Destination:=Target.Cells(k, 4)
.Range(.Cells(i, 4), .Cells(i, 5)).Copy _
Target.Range("F" + k, "G" + k).PasteSpecial xlPasteValues
.Range(.Cells(i, 6), .Cells(i, 7)).Copy _
Destination:=Target.Range("I" + k, "J" + k)
.Cells(i, 8).Copy _
Destination:=Target.Cells(k, 8)
k = k + 1
End If
Next i
End With
After this operation, the user can see a summary of his entered and calculated data. With another macro, which copies the rows into 3 different worksheets (depending on the kind), the calculation process is finished.
This is my copy function :
Function CopyTable(Typ As String, Counter As Integer, Count As Integer) As Integer
With Worksheets("Calc_Overview")
.Range("A" & Count & ":" & "D" & Count).Copy _
Destination:=Sheets(Typ).Range("A" & 5 + Counter)
.Range("G" & Count & ":" & "H" & Count).Copy _
Destination:=Worksheets(Typ).Range("E" & 5 + Counter)
End With
CopyTable = Counter + 1
End Function
Now my problem:
If you try to edit the data which was transfered into the final worksheet, it often happens that Excel crashes without any reason. After editing a cell value,pressing enter or clicking another cell will let Excel crash.
I already turned off the 'Automatically Flash Fill' and 'Show Quick Analysis' option, but the problem still occurs..
Has anyone a clue what might cause this problem? I already replaced the final worksheets by new ones without copying anything from the 'broken' ones - also without any success. I am using a Windows 10 PC (x64) with an 32-Bit Microsoft Office 2013.
I am really looking fowards to any tips or solutions regarding this problem!
Many thanks
Moritz
I have an update for you:
changing the copy method does not change anything - the problem still occurs.
This is how is looks after you edit the cell - the cell expands and excel is freezing without any reason with a very high cpu load

Run time error 1004 placing formula in cell

I have created a formula that works in a spreadsheet but not in VBA. What I have pasted here is only a subset of the entire formula; in total it exceeds 3,000 characters
If I run this, it works
LastRow = Range("a" & Rows.Count).End(xlUp).Row
Range("cI2:cI" & LastRow).Formula = "=CONCATENATE(CONCATENATE(MID(""00"",1,2-LEN(A2)),A2))"
If I run this, I get the run time error
LastRow = Range("a" & Rows.Count).End(xlUp).Row
Range("cI2:cI" & LastRow).Formula = "=CONCATENATE(CONCATENATE(MID(""00"",1,2-LEN(A2)),A2)), " & _
"CONCATENATE(MID(""00000000"",1,8-LEN(B2)),B2),"" "", CONCATENATE(C2,MID("" "",1,12-LEN(C2))),CONCATENATE(MID(""00"",1,2-LEN(D2)),D2) "
Without seeing the rest of the formula, I would suspect that it exceeds some of the internal limits Excel has for function. Possibilities that come to mind are:
Arguments in a function: 255
Nested levels of functions: 64
Length of formula contents: 8,192 characters
You can find more information on the Office support page here.
That said, I'm curious why you would need to build a 3000 character formula to place in the cell. This is what VBA is for - converting the formula into a user defined function is likely going to be the solution.

Insert new row in the same place in two sheets > copy formulas from above

I've created a tracking document for my workplace, but my limited knowledge of Excel has only taken me so far. Hoping there's someone out there who enjoys cleaning up messy workbooks...
I have a document with two connected worksheets: 'Annuals', covering a lot of detail about the forms I'm designing; and 'Summary', which is really intended for management use. All the information in the 'Summary' page is a cell reference to 'Annuals' and the 'Summary' page is normally password protected so that the formulas can't be overwritten. You can download a dummy copy of the larger document I'm hoping to implement: Dropbox Link We use Excel 2010 at work, although the attached document was last edited in OpenOffice Calc.
The basic function of this document is to present varying degrees of information per worksheet, with most raw information being manually provided. I would like it to be dynamic enough that the user doesn't need to understand the importance of keeping cell references - they can just press a button to add their task to the list, and the program will populate the rest behind the scenes.
Aside from the terrible way that I've visually indicated the row label "hierarchy" in column C, I'm also having issues when someone wishes to insert a new row in the 'Annuals' page; I have not found an effective way to insert a row on the 'Summary' page at the same time while maintaining dynamic formulas and formatting. It has been difficult to write a macro to do this because I only want certain cells referenced on the 'Summary' page (although I'm 100% sure there's a way to do this).
This is my attempt:
Public sAddress As String
Sub AddRow()
R = ActiveCell.Row
ActiveCell.Offset(-1).EntireRow.Insert Shift:=xlDown
Range("E" & R & ":G" & R).Copy Destination:=Range("E" & R + 1 & ":G" & R + 1)
Range("T" & R).Copy Destination:=Range("T" & R + 1)
End Sub
Sub DeleteRow()
ActiveCell.EntireRow.Delete
End Sub
Sub AddSummary()
On Error Resume Next
Worksheets("Summary").Activate
If sAddress > "" Then Sh.Range(sAddress).Select
R = ActiveCell.Row
ActiveCell.Offset(-1).EntireRow.Insert Shift:=xlDown
Range("E" & R & ":G" & R).Copy Destination:=Range("E" & R + 1 & ":G" & R + 1)
Range("T" & R).Copy Destination:=Range("T" & R + 1)
End Sub
Sub Variab()
sAddress = ActiveCell.Address
End Sub
Sub Everything()
Call AddRow
Call Variab
Call AddSummary
End Sub
Maybe a pivot table could be implemented? However, I have no idea how to make a pivot table actually work, let alone make it user-friendly or pretty. I tried - and failed - to make a pivot table that I could reference in the Summary page and hide from other users, but I didn't get very far.
I know that you're probably shuddering at my workbook, but if you can think of better methodology behind the information I'm trying to track (amongst many users, with varying degrees of computer literacy) then please let me know!
Click on Summary tab
Shift-click on Annuals tab
Insert the row.
That's it! It will insert a blank row on both sheets.
If you want to copy data from an existing row into the newly created row (as in your macro), select both tabs as above, highlight a row, copy it, and paste it into the new row. You will see that the copy command was applied to both sheets, just as if you had done the copy command on each sheet.

Summing cells from another sheet using offset

I'm trying to get this code to work for summing cells:
Worksheets("Sheet2").Range("C3").Offset(i, j).Formula = "=Sum("
&Worksheets("Sheet1").Range("A3").Offset(2*i,j).Address & ":" &
Worksheets("Sheet1").Range("A7").Offset(2*i,j).Address & ")"
It keeps giving me the right cells but from the wrong sheet. So for the first iteration I get sum(A3:A7) in cell C3 of Sheet2 but the A3:A7 stays referenced to Sheet2 not Sheet1.
Thanks!
You need to specify the name of the sheet in the formula too. Your code will work if you write it like this:
Worksheets("Sheet2").Range("C3").Offset(i, j).Formula = "=Sum(Sheet1!" & _
Worksheets("Sheet1").Range("A3").Offset(2 * i, j).Address & ":" & _
Worksheets("Sheet1").Range("A7").Offset(2 * i, j).Address & ")"
Try this code - it uses the External:=True parameter of .Addressto retrieve the full address. While this also includes the workbook name, Excel will remove this automatically so you end up with Sheet1!A3. Also note that I used the range A3:A7 as source as .Address can handle multi-cell ranges and you don't need to take care of it manually:
Sheets("Sheet2").Range("C3").Offset(i, j).Formula = "=SUM(" & _
Sheets("Sheet1").Range("A3:A7").Offset(2 * i, j).Address(External:=True) & ")"
Be aware that hard coding references such as A3 can lead to bugs in the long run, as the user (or even the developer at some stage) might modify the sheet structure. It is best practice to use named ranges, i.e. create a named range for each cell/range you refer to and then access it in VBA with SheetX.Range("rngStartCell") or similar!