ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Creating a macro to swap cells but retain formatting (https://www.excelbanter.com/excel-programming/387508-creating-macro-swap-cells-but-retain-formatting.html)

JEFF

Creating a macro to swap cells but retain formatting
 
Greetings,

I have a macro that I am using to swap the contents of two cells. When I
use it I lose any character formatting that I have applied to the cell(s).
Does anyone have any insights into how to do a swap cell macro but retain
the formatting?

Sub SwitchCells()
'
' Macro created 4/13/2007 by willis

Range("k13") = Range("g13")
Range("g13") = Range("b13")
Range("b13") = Range("k13")
Range("k13") = ""
End Sub

Best regards!
Jeff



Gary''s Student

Creating a macro to swap cells but retain formatting
 
use Copy:

Sub jeff()
Dim rk As Range
Dim rg As Range
Dim rb As Range
Set rk = Range("K13")
Set rg = Range("G13")
Set rb = Range("B13")
rg.Copy rk
rb.Copy rg
rk.Copy rb
rk.Clear
End Sub

--
Gary''s Student - gsnu200715


Vergel Adriano

Creating a macro to swap cells but retain formatting
 
Jeff,

maybe something like this

Sub SwitchCells()

Range("G13").Copy Range("K13")
Range("B13").Copy Range("G13")
Range("K13").Copy Range("B13")
Range("K13").ClearFormats
Range("K13").ClearContents

End Sub



--
Hope that helps.

Vergel Adriano


"JEFF" wrote:

Greetings,

I have a macro that I am using to swap the contents of two cells. When I
use it I lose any character formatting that I have applied to the cell(s).
Does anyone have any insights into how to do a swap cell macro but retain
the formatting?

Sub SwitchCells()
'
' Macro created 4/13/2007 by willis

Range("k13") = Range("g13")
Range("g13") = Range("b13")
Range("b13") = Range("k13")
Range("k13") = ""
End Sub

Best regards!
Jeff




JEFF

Creating a macro to swap cells but retain formatting
 
Thank you very much!

I have merged cells that I am working with. In this case I assume I have to
unmerge first before I swap. Does anyone have any advice?

<snip
Range("B13").MergeCells = False
</snip

Thanks!

"Vergel Adriano" wrote in message
...
Jeff,

maybe something like this

Sub SwitchCells()

Range("G13").Copy Range("K13")
Range("B13").Copy Range("G13")
Range("K13").Copy Range("B13")
Range("K13").ClearFormats
Range("K13").ClearContents

End Sub



--
Hope that helps.

Vergel Adriano


"JEFF" wrote:

Greetings,

I have a macro that I am using to swap the contents of two cells. When I
use it I lose any character formatting that I have applied to the
cell(s).
Does anyone have any insights into how to do a swap cell macro but retain
the formatting?

Sub SwitchCells()
'
' Macro created 4/13/2007 by willis

Range("k13") = Range("g13")
Range("g13") = Range("b13")
Range("b13") = Range("k13")
Range("k13") = ""
End Sub

Best regards!
Jeff







All times are GMT +1. The time now is 03:19 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com