VBA Color Operations

rlderi01

New Member
Joined
May 10, 2011
Messages
5
All,

I have manually-formatted cells colored colorindex =6, spread randomly throughout the sheet. I need to write a macro to select these colored cells, and use the offset function to move them (2, -1). Any help greatly appreciated!

RD
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Additional info- the destination cells will be blank, and nothing else will have to be moved other than the target yellow cells
 
Upvote 0
so you want to move the data in the yellow color cell, not to color? Also, do you want the source cell cleared out or leave the data in?
 
Upvote 0
hi, and welcome to MrExcel

smth like this maybe?

Code:
Sub test()
On Error Resume Next
For Each TmpRng In UsedRange
 If TmpRng.Interior.ColorIndex = 6 Then
  TmpRng.Interior.ColorIndex = 0
  TmpRng.Offset(2, -1).Interior.ColorIndex = 6
 End If
Next
End Sub

P.S. edition: just noticed Sous's question - i was under the impression you needed to move the coloring, not the cell value. my code moves the yellow color by offset(2,-1), not the content, of the cells.
 
Upvote 0
Here's my stab at it:

Code:
Sub foo()
Dim rng As Range, c As Range

Application.ScreenUpdating = False
Set rng = ActiveSheet.UsedRange
For Each c In rng
    If c.Interior.Color = 65535 Then
        c.Offset(2, -1).Value = c.Value
        c.Value = ""
        c.Interior.Pattern = xlNone
    End If
Next c

Application.ScreenUpdating = True
End Sub
 
Last edited:
Upvote 0
See my edited post above and let me know if that's close to what you're expecting.

Also, welcome to the board!
 
Upvote 0
sous2817- that works, only I need the source cell cleared and as of now it just copies the cell leaving the source populated. Thank you though that's already a lot more than I was able to do!
 
Upvote 0
I edited my post to clear both the source text and source color. Recopy it and give it another whirl and let me know if it works.
 
Upvote 0

Forum statistics

Threads
1,224,586
Messages
6,179,722
Members
452,939
Latest member
WCrawford

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top