Change Style of Selection.ColumnDifferences - excel

I created a macro to add a quick access icon to the Excel toolbar to perform the F5, Special, Column differences task.
How do I change the highlight style of that action? Currently it is a sort of greyish white and if there are multiple differences in a column (50+ rows) muliple differences are not easily recognizable.
I tried this modification but the highlight will stay after another cell is selected.
Sub column_difference()
Selection.ColumnDifferences(ActiveCell).Select
Selection.Style = "Bad"
End Sub

"Bad" is the name of a built-in style. If you change the name of the style to "Good" you will get another colour. If you were to create a style called "MyStyle" you could specify the colour yourself.
There is a "Styles" menu on the Home ribbon. On that menu there is a drop-down button called "Cell Styles". At the bottom of the dialog box that opens when you click that button there is a button called "New cell style". In the dialog box behind that button you can create a style of your own and call it "MyStyle". After that you can use the "MyStyle" name in your code. You can save the style with your workbook.
Needless to say, your code will only work in workbooks where MyStyle style exists. On the other hand, a Style is just a collection of specific cell formats. In the style setup dialog box you find a list of all the format properties a style affects. Your interest seems to focus on the cell's fill colour. Therefore, instead of changing the selected cells' Style you could just change their fill colour.
Select any cell and colour it the way you want. Then, with that cell still selected, enter ? ActiveCell.Interior.Color in the VBE's Immediate window. This will give you the number of your chosen colour. Then modify your code using that number.
Sub column_difference()
Selection.ColumnDifferences(ActiveCell).Select
Selection.Interior.Color = 255
End Sub
255 = vbRed. The above code will change the the Selection's fill colour to red but leave the font colour as it was before. The extra line Selection.Font.Color = vbWhite would change that, too. If you created MyStyle with those two properties you could apply both commands with a single, and shorter, line of code. Selection.Style = "Normal" would change everything back to the original, i.e. what that cell's formatting was when the worksheet was first created.

Related

Trying to check the background color on another sheet to decide if I accept the cell value

I am trying to modify a formula that currently works to pull a value from another excel sheet in the same workbook. There are three possible colors for the cell on the sheet being searched (yellow, blue, white). I only want cells that have a white background on the sheet being searched before returning the value. I have not found a way to test for cell background color in a target cell on another tab.
I looked at Cell("color", reference) but it only test for font color. Any help will be appreciated. Here is the working formula without a test for color.
=HLOOKUP(B10,'Savings Tracking'!$A$1:$ADX$1000,(MATCH(C10,'Savings Tracking'!$B$1:$B$1000,0)),FALSE)
Now I need to expand this for a color test before returning the value in the cell)
You'll have to use VBA for this. I assure you this is a pretty simple one though. In your workbook, choose the Developer tab (if it's not visible, then: http://www.addintools.com/documents/excel/how-to-add-developer-tab.html) and select Visual Basic. In the left pane (Project-VBAProject), you'll see your workbook name. Right-click and Insert > Module. In the new module, add the following code:
Public Function BackGroundColor(rng As Range)
BackGroundColor = rng.Interior.Color
End Function
Now you'll be able to use =BackGroundColor(some range) to return the cell color in long format.

Excel: adding hyperlink in a cell with existing text [duplicate]

Is it possible to create a hyperlink within an Excel cell which only uses a section of the cell text for the clickable link? I.E. would the below table mockup represent something that can be easily built in Excel 2010?
a mock up http://dl.dropbox.com/u/14119404/misc/Microsoft%20Excel%20-%20Book1_2012-04-16_14-24-47.jpg
I know that an entire cell can be made into a hyperlink easily, but not a specific part of the cell as far as I know.
By hyperlink I also refer to either
(a)another cell or,
(b)a web URL.
Thanks
After creating the hyperlink you could format the text in the cell so that only the words of interest are underlined/blue. The hyperlink will still work, but obviously you can still have only one link per cell, and clicking anywhere in the text will trigger the hyperlink.
For example:
Sub Tester()
Dim rng As Range
Set rng = ActiveSheet.Range("A1")
rng.Parent.Hyperlinks.Add Anchor:=rng, Address:="", SubAddress:= _
"Sheet1!A10", TextToDisplay:="this is long text"
With rng.Font
.ColorIndex = xlAutomatic
.Underline = xlUnderlineStyleNone
End With
With rng.Characters(Start:=9, Length:=4).Font
.Underline = xlUnderlineStyleSingle
.Color = -4165632
End With
End Sub
I needed to link to a filename displayed in a cell, so here is what worked for me:
ActiveSheet.Hyperlinks.Add Anchor:=Cells(row, column), Address:=file.Path, TextToDisplay:=file.Path
This isn't possible in Excel. Hyperlinks are associated with entire cells.
If you look at the documentation for the Excel hyperlink object, you can see that it's associated with a Range. If it were possible to associate hyperlinks with a span within the cell, the Hyperlink object would need to have an associated Range and Characters object.
The above one liner was very helpful... since I'm new, I couldn't comment. So here is my variation of the above that takes each row on a worksheet and builds a URL from a value on the row.
CHGRow = 3
Worksheets("Page 1").Select
Cells(CHGRow, 1).Select
Do Until Application.CountA(ActiveCell.EntireRow) = 0
URLVal = "https://our_url_here?some_parameter=" & Cells(CHGRow, cNumber)
URLText = Cells(CHGRow, cNumber)
ActiveSheet.Hyperlinks.Add Anchor:=Cells(CHGRow, cURL), Address:=URLVal, TextToDisplay:=URLText
CHGRow = CHGRow + 1
Cells(CHGRow, 1).Select
Loop
I'd just make your one row into two rows, merge the cells in the columns you need to have it appear to be a single row and when you get to the cell that needs the hyperlink then you put the words on the top cell and the link in the cell below. It will look fine as a non-techy workaround.
Here's a great smoke-and-mirrors solution I've used for creating hyperlinked strings within a larger block of text in an Excel spreadsheet cell. CAUTION -- if there are multiple editors for your worksheet, this is not advisable as hyperlinks can get misaligned with the text in their cells unless you can provide sufficient protection. A means of doing that is described in this procedure though may limit what contributors can do:
In your Excel spreadsheet, assuming you are not trying to protect ALL cells from editing, select all cells in the worksheet, then select Format Cells from the Home ribbon or the right-click popup menu. On the Protection tab, check then uncheck the "Locked" checkbox to ensure all cells are unlocked.
Now select the (first) cell that will contain the link.
Copy (don't cut) the text that you want to appear as a link to the clipboard.
Click anywhere in your spreadsheet (an unused area is best) and insert a text box from the Insert ribbon using either the Shape icon dropdown or the Text icon. Size and shape aren't important yet, just approximate the size of the link text.
Paste the clipboard contents into the text box.
Right click the text box and select Link to add a hyperlink to it, and specify the link target, whether a location in the current document or a URL.
Select the link text and format it how you want your links to appear, e.g. blue, underlined, etc., since inside a text box this apparently does not occur automatically as in a cell.
Right click the text box again and select Format Shape. From the Format Shape panel, perform the following in order to properly fit the shape around the text and eliminate white space and border:
(a) On the Fill & Line panel (1st icon), select the No Line option.
(b) On the Size & Properties panel (3rd icon), set all 4 margins to zero (0.00"), uncheck the "Wrap text in shape" checkbox, and check the "Resize shape to fit text" textbox."
(c) Under Properties, ensure the following are selected:
* Move but don't size with cells
* Locked
* Lock text
If you need the same hyperlink to occur in multiple cells or locations within the same cell (even if targets differ), clone the text box you did all this work on by selecting its (now invisible) border with a right click (warning - a left click will now take you to the link target instead!), copying it to the clipboard, and press Ctrl+V as many times as you need copies.
Right click on the text box (or one of them if you cloned it) and drag it to the cell where you want the link to appear, positioning it directly over the original text that matches your hyperlink, so as to visually cover it up and replace it (the original text serving as a spacer to make room for it). The steps taken in item 8 above should prevent it from covering up or clipping any text or punctuation surrounding the original text.
Select that cell, then from the Format menu on the Home ribbon, select "Lock Cell" to protect its contents from inadvertently changing and misaligning the text box with the corresponding text that its hiding.
Repeat steps 10 & 11 for each additional copy of the linked text box you created. If any of them requires a different link target, simply right click that copy of the text box, select "Edit Link", and update the target.
From the Format menu on the Home ribbon, select "Protect Sheet". Check the box labelled "Protect worksheet and contents of locked cells".
Check all the other boxes in that dialog also (assuming you are not trying to restrict users in any of those ways) except for the following:
* Format cells
* Format columns
* Format rows
* Edit objects
Leave these four checkboxes unchecked so as to protect your linked text boxes from being selected, deleted, moved, or misaligned with their underlying text. (Note: They should already move with their underlying cells if rows or columns are added, removed or resized, but without this protection they can still be impacted if their own row or column is resized.)
If you want to add a password, do so now. When finished, click OK to apply protection. You can selected "Unprotect Sheet" from the Format menu later to perform any necessary editing, but if the link cell(s), column(s) or row(s) are edited or resized, you may need to reposition the link text box(es) over the underlying text if it moved.
Test your hyperlink(s) and also what happens if you try editing the containing cell(s) or resizing the containing column(s) or row(s) to be sure the worksheet is ready for sharing!

Can the colour of a cell be changed once it has been selected?

I am doing a large amount of data entry but down a list which is already populated i.e. I am changing slightly the entries if they meet certain requirements.
I was wondering if anyone knew of a way to get a cell to change colour once it has been selected?
The work flow I want is:
Select cell at the top --> make alteration if necessary --> press enter to go down to next cell --> the cell changes from red to green.
The idea is that when I take a break or check different values in the same excel sheet I can very easily find where I left off without having write down the row number.
It's much easier to change colour of the cell when you select it (rather than after deselecting). To do this:
Right-click the sheet tab
Click 'view code'
Paste the following into the window that pops up:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Target.Interior.ColorIndex = 33
End Sub
Try it.

Excel VBA: Copying color from Cell.Interior.Color to MSForms.Label.BackColor

I have a userform in Excel 2003 which contains some MSForms.Label controls. I want to set the .BackColor property of each of these controls such that they match the color of some individual cells on a worksheet.
To do this I am reading the .Interior.Color property of those cells, converting it to hex and using that value to set the .BackColor property of the Label objects.
My problem is that the value coming out of .Interior.Color is almost always incorrect the first time it is read. I have tested this by running the following command in the VBA editor immediate window:
Print Hex([sourceCell].Interior.Color)
This command almost always gives the wrong value the first time round, but gives the correct value from the second time onwards. If I change the fill color of [sourceCell] and run the command again it will do the same thing, i.e. wrong value first time, right value second time onwards.
The cells in question are all filled with colors from the chart lines/fills color selection (i.e. the two bottom rows you can see in Excel's "Format Cells" dialog under the "Patterns" tab). These colors have been modified by me to give a custom set of colors and, tellingly, the "wrong" values seem to match Excel's defaults for the customized range of color picks (i.e. the default chart line/fill colors you get when you start a new workbook).
Has anyone else experienced this behaviour/have a workaround? When I try to read the values twice at runtime it doesn't work, i.e. it doesn't switch to the correct value. The code must be stopped and the userform reloaded to force the "correct" values to come out.
Kludge/workaround via Application.OnTime.
If I run the userform prep code to set the label objects' backcolors with the userform hidden, then set the main code to run from an immediate OnTime event (making sure to re-run the prep code as part of the OnTime) then I can force Excel to give me the correct "second time round" values when showing the form.
This works for me:
MyForm.MyControl.BackColor = Range(myrange).Interior.Color
.Interior.Color returns a Variant containing the RGB of the sampled range.
.Interior.ColorIndex always returns -4140 or something weird, probably because .ColorIndex is peculiar to Excel2003's color limitations.

Create a hyperlink within an Excel cell?

Is it possible to create a hyperlink within an Excel cell which only uses a section of the cell text for the clickable link? I.E. would the below table mockup represent something that can be easily built in Excel 2010?
a mock up http://dl.dropbox.com/u/14119404/misc/Microsoft%20Excel%20-%20Book1_2012-04-16_14-24-47.jpg
I know that an entire cell can be made into a hyperlink easily, but not a specific part of the cell as far as I know.
By hyperlink I also refer to either
(a)another cell or,
(b)a web URL.
Thanks
After creating the hyperlink you could format the text in the cell so that only the words of interest are underlined/blue. The hyperlink will still work, but obviously you can still have only one link per cell, and clicking anywhere in the text will trigger the hyperlink.
For example:
Sub Tester()
Dim rng As Range
Set rng = ActiveSheet.Range("A1")
rng.Parent.Hyperlinks.Add Anchor:=rng, Address:="", SubAddress:= _
"Sheet1!A10", TextToDisplay:="this is long text"
With rng.Font
.ColorIndex = xlAutomatic
.Underline = xlUnderlineStyleNone
End With
With rng.Characters(Start:=9, Length:=4).Font
.Underline = xlUnderlineStyleSingle
.Color = -4165632
End With
End Sub
I needed to link to a filename displayed in a cell, so here is what worked for me:
ActiveSheet.Hyperlinks.Add Anchor:=Cells(row, column), Address:=file.Path, TextToDisplay:=file.Path
This isn't possible in Excel. Hyperlinks are associated with entire cells.
If you look at the documentation for the Excel hyperlink object, you can see that it's associated with a Range. If it were possible to associate hyperlinks with a span within the cell, the Hyperlink object would need to have an associated Range and Characters object.
The above one liner was very helpful... since I'm new, I couldn't comment. So here is my variation of the above that takes each row on a worksheet and builds a URL from a value on the row.
CHGRow = 3
Worksheets("Page 1").Select
Cells(CHGRow, 1).Select
Do Until Application.CountA(ActiveCell.EntireRow) = 0
URLVal = "https://our_url_here?some_parameter=" & Cells(CHGRow, cNumber)
URLText = Cells(CHGRow, cNumber)
ActiveSheet.Hyperlinks.Add Anchor:=Cells(CHGRow, cURL), Address:=URLVal, TextToDisplay:=URLText
CHGRow = CHGRow + 1
Cells(CHGRow, 1).Select
Loop
I'd just make your one row into two rows, merge the cells in the columns you need to have it appear to be a single row and when you get to the cell that needs the hyperlink then you put the words on the top cell and the link in the cell below. It will look fine as a non-techy workaround.
Here's a great smoke-and-mirrors solution I've used for creating hyperlinked strings within a larger block of text in an Excel spreadsheet cell. CAUTION -- if there are multiple editors for your worksheet, this is not advisable as hyperlinks can get misaligned with the text in their cells unless you can provide sufficient protection. A means of doing that is described in this procedure though may limit what contributors can do:
In your Excel spreadsheet, assuming you are not trying to protect ALL cells from editing, select all cells in the worksheet, then select Format Cells from the Home ribbon or the right-click popup menu. On the Protection tab, check then uncheck the "Locked" checkbox to ensure all cells are unlocked.
Now select the (first) cell that will contain the link.
Copy (don't cut) the text that you want to appear as a link to the clipboard.
Click anywhere in your spreadsheet (an unused area is best) and insert a text box from the Insert ribbon using either the Shape icon dropdown or the Text icon. Size and shape aren't important yet, just approximate the size of the link text.
Paste the clipboard contents into the text box.
Right click the text box and select Link to add a hyperlink to it, and specify the link target, whether a location in the current document or a URL.
Select the link text and format it how you want your links to appear, e.g. blue, underlined, etc., since inside a text box this apparently does not occur automatically as in a cell.
Right click the text box again and select Format Shape. From the Format Shape panel, perform the following in order to properly fit the shape around the text and eliminate white space and border:
(a) On the Fill & Line panel (1st icon), select the No Line option.
(b) On the Size & Properties panel (3rd icon), set all 4 margins to zero (0.00"), uncheck the "Wrap text in shape" checkbox, and check the "Resize shape to fit text" textbox."
(c) Under Properties, ensure the following are selected:
* Move but don't size with cells
* Locked
* Lock text
If you need the same hyperlink to occur in multiple cells or locations within the same cell (even if targets differ), clone the text box you did all this work on by selecting its (now invisible) border with a right click (warning - a left click will now take you to the link target instead!), copying it to the clipboard, and press Ctrl+V as many times as you need copies.
Right click on the text box (or one of them if you cloned it) and drag it to the cell where you want the link to appear, positioning it directly over the original text that matches your hyperlink, so as to visually cover it up and replace it (the original text serving as a spacer to make room for it). The steps taken in item 8 above should prevent it from covering up or clipping any text or punctuation surrounding the original text.
Select that cell, then from the Format menu on the Home ribbon, select "Lock Cell" to protect its contents from inadvertently changing and misaligning the text box with the corresponding text that its hiding.
Repeat steps 10 & 11 for each additional copy of the linked text box you created. If any of them requires a different link target, simply right click that copy of the text box, select "Edit Link", and update the target.
From the Format menu on the Home ribbon, select "Protect Sheet". Check the box labelled "Protect worksheet and contents of locked cells".
Check all the other boxes in that dialog also (assuming you are not trying to restrict users in any of those ways) except for the following:
* Format cells
* Format columns
* Format rows
* Edit objects
Leave these four checkboxes unchecked so as to protect your linked text boxes from being selected, deleted, moved, or misaligned with their underlying text. (Note: They should already move with their underlying cells if rows or columns are added, removed or resized, but without this protection they can still be impacted if their own row or column is resized.)
If you want to add a password, do so now. When finished, click OK to apply protection. You can selected "Unprotect Sheet" from the Format menu later to perform any necessary editing, but if the link cell(s), column(s) or row(s) are edited or resized, you may need to reposition the link text box(es) over the underlying text if it moved.
Test your hyperlink(s) and also what happens if you try editing the containing cell(s) or resizing the containing column(s) or row(s) to be sure the worksheet is ready for sharing!

Resources