So, I've been trying to figure this one out for a while...
I have a column of data that has several different background colors assigned to each cell. For example, A2 is blue, A3 is orange, A4 through A10 is yellow, and then A11 is blue again.
I would like to copy and paste these background colors across all the rows in the spreadsheet; however, if I simply copy and paste special > formats, it also pastes the column width and other formatting options from column A that I do not wish to have changed on my spreadsheet.
I have done some research on this and I now know that each color is assigned a number and I can have the number returned using a function or a macro (I'm not really certain of the difference or if there even is one) and then use that value to then set the background color of each row accordingly.
The problem is that even after reading articles like this and this, I still can't seem to figure out how to put the information together to do what I want to do.
Can anyone interpret this for me and possibly produce a code that does what I am needing?
Thanks in advance!
-Amy Kate
Last edited by NBVC; 02-22-2010 at 12:51 PM. Reason: Mark Solved per OP request
This code will transfer the colour of the selected cells to the whole row.
To input the code, use ALT & F11 to start the VBA editorCode:Sub copy_colours_across_rows() Dim rCell As Range For Each rCell In Selection rCell.EntireRow.Interior.ColorIndex = rCell.Interior.ColorIndex Next End Sub
Go to Insert, Module
Paste the code
Close the VB editor
In excel, select the cells from which you want to transfer the colour.
Go to Macros, run macro, and select copy_colours_across_rows
Sarcasm - because beating the **** out of someone is illegal.
Wow, that was way more simple than what I was trying to do! So what word(s) do I need to change if I want to do the same for the font color instead?
Change this:
For this:Code:rCell.EntireRow.Interior.ColorIndex = rCell.Interior.ColorIndex
If you want to find out how to adjust formatting with VBA, record a macro in which you alter the formatting, then take a look at it in the VBA editor.Code:rCell.EntireRow.Font.ColorIndex = rCell.Font.ColorIndex
Sarcasm - because beating the **** out of someone is illegal.
So do you know how I could tell it to only do that down as many rows as there is data? Right now, I have it set to go down 2000 rows just so nothing is left off, but it takes forever and most of the time there is only around 100 rows.
Presuming your data starts at A1, and has no gaps
Code:Sub copy_colours_across_rows() Dim rCell As Range, rRange as Range set rRange = Range(Range("A1"), Range("A1").End(xlDown)) For Each rCell In rRange rCell.EntireRow.Interior.ColorIndex = rCell.Interior.ColorIndex Next End Sub
Sarcasm - because beating the **** out of someone is illegal.
Slight modification to Sweep's code. If you ever have blank cells between the first and last row, selection of the range will omit rows below the empty cell and thus cause some rows to not get the color change.Presuming your data starts at A1, and has no gaps
This code mod will select the range to the last used row in column-A and will not be affected by blank cells in between.
Code:Option Explicit Sub copy_colours_across_rows() Dim rCell As Range, rRange As Range, lrow As Long lrow = Cells(Rows.Count, 1).End(xlUp).Row Set rRange = Range(Cells(1, 1), Cells(lrow, 1)) Application.ScreenUpdating = False On Error Resume Next For Each rCell In rRange rCell.EntireRow.Interior.ColorIndex = rCell.Interior.ColorIndex Next Application.ScreenUpdating = True End Sub
Palmetto
Do you know . . . ?
You can leave feedback and add to the reputation of all who contributed a helpful response to your solution by clicking the star icon located at the left in one of their post in this thread.
Thanks sweep, that worked great, and thanks for the modification Palmetto.
Can someone mark this solved for me, since it is an old post?
Just go to the top of YOUR first post, select Thread Tools, Go Advanced, then choose the [SOLVED] prefix.Can someone mark this solved for me, since it is an old post?
Palmetto
Do you know . . . ?
You can leave feedback and add to the reputation of all who contributed a helpful response to your solution by clicking the star icon located at the left in one of their post in this thread.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks