(edit: 2 more solution restrictions added)
I have a table (listobject wise) in which I need to duplicate certain rows. I use SourceListRow.Range.Value2 = DestListRow.Range.Value2 for efficiency reasons (copying entire ranges at a time). All formula columns after copied cell ranges are automatically expanded in new rows and act on the copied data. I am using Excel 2010 here on Windows.
However, although I have already used this type of code for aeons, I just stumbled upon a weirdness when using Range.Value / Range.Value2: if you assigned it an empty string, the final cell value will not be an empty string, but it will be empty. That is: the data is not copied reliably, and the copy may differ from the source, especially if sequential formulas use ISBLANK, etc. On her. Therefore, the same formula will have different results when working with a copy and a source.
See test code below. Open a new, empty Excel workbook, go to VBA, add a new module and add the following code:
Sub Test()
ActiveSheet.Range("a1").Formula = "="""""
ActiveSheet.Range("b1").Formula = "=isblank(a1)"
ActiveSheet.Range("c1").Value2 = TypeName(ActiveSheet.Range("a1").Value2)
ActiveSheet.Range("a2").Value2 = ActiveSheet.Range("a1").Value2
ActiveSheet.Range("b2").Formula = "=isblank(a2)"
ActiveSheet.Range("c2").Value2 = TypeName(ActiveSheet.Range("a2").Value2)
ActiveSheet.Range("a3").Value2 = ""
ActiveSheet.Range("b3").Formula = "=isblank(a3)"
ActiveSheet.Range("c3").Value2 = TypeName(ActiveSheet.Range("a3").Value2)
ActiveSheet.Range("a4").Formula = ActiveSheet.Range("a1").Formula
ActiveSheet.Range("b4").Formula = "=isblank(a4)"
ActiveSheet.Range("c4").Value2 = TypeName(ActiveSheet.Range("a4").Value2)
Call ActiveSheet.Range("a1").Copy
Call ActiveSheet.Range("a5").PasteSpecial(xlPasteValues)
ActiveSheet.Range("b5").Formula = "=isblank(a5)"
ActiveSheet.Range("c5").Value2 = TypeName(ActiveSheet.Range("a5").Value2)
End Sub
Then run it and look at the sheet;
- B1 tells FALSE (as it should - the cell is not empty), and C1 says "String" (the cell value is really an empty string);
- B2 TRUE, A1 as-is; C2 "Empty", "String", ;
- , B3 TRUE, ; C3 "Empty", ;
- Range.Formula OtherRange.Formula ( String B4), , !
- , GUI A5, , , Copy/Paste-As-Val is-a-empty-string...
?
- Range.Copy/Range.PasteSpecial(xlPasteValues) , ;
- .Formula , ;
- , , , value = "", , ...
- , (a.k.a. ListObject); , , , kludge.
- Range.Find Range.Replace , Find/Replace.
!