I found the CopyValues procedure below somewhere on the internet (with less / no comments). I added the others easily to handle in place copy and copying formulas either relative or not.
However there is a scenario I'm trying to crack but I'm not seeing an elegant solution, that is replacing Pastespecial with transpose = true.
The only thought I had was to assign the range to an array, pivot that and then write it back to the range... I did not find a way to pivot an array quickly.
Hopefully someone has an elegant solution or I may just have to tolerate the copy/pastespecial.
However there is a scenario I'm trying to crack but I'm not seeing an elegant solution, that is replacing Pastespecial with transpose = true.
The only thought I had was to assign the range to an array, pivot that and then write it back to the range... I did not find a way to pivot an array quickly.
Hopefully someone has an elegant solution or I may just have to tolerate the copy/pastespecial.
Code:
Public Sub CopyValues(rngSource As Range, rngTarget As Range)
'Allows for copying one range to target range without actually using copy and paste
'Not only is it faster to not roundtrip the clipboard, other use of the clipboard will not corrupt execution...
'One Apparent weakness is does not work when Source and Target are the same
Set rngTarget = rngTarget.Resize(rngSource.Rows.Count, rngSource.Columns.Count)
rngTarget.Value = rngSource.Value
End Sub
Public Sub ReplaceValues(rngSource As Range)
'Inplace Paste Special Values: Allows for copying source range values and removing formulas
'Not only is it faster to not roundtrip the clipboard, other use of the clipboard will not corrupt execution...
rngSource.Value = rngSource.Value
End Sub
Public Sub CopyFormulasAbsolute(rngSource As Range, rngTarget As Range)
'Allows for copying one range to targe range without actually using copy and paste
'Not only is it faster to not roundtrip the clipboard, other use of the clipboard will not corrupt execution...
'Copies literal formulas, not relative
rngTarget.Resize(rngSource.Rows.Count, rngSource.Columns.Count).Formula = rngSource.Formula
End Sub
Public Sub CopyFormulasRelative(rngSource As Range, rngTarget As Range)
'Allows for copying one range to targe range without actually using copy and paste
'Not only is it faster to not roundtrip the clipboard, other use of the clipboard will not corrupt execution...
'Copies relative reference formulas, not absolute, or A1 move over 2 columns become C1...
rngTarget.Resize(rngSource.Rows.Count, rngSource.Columns.Count).FormulaR1C1 = rngSource.FormulaZR1C1
End Sub