473,386 Members | 1,763 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

EXCEL Q: How to Color Shapes Based off Date Ranges in Various Cells?

4
OK first off hello! I am new to these forums and - though I dabbled a bit in Basic when I was younger - new to programming in general. I have searched using Google rather extensively but cannot figure out how to do what I'm attempting...perhaps because I am having a hard time applying programming principles because I am new to macros and VBA.

I use Excel (2013) quite a bit to help organize large amounts of info, but am starting to push my limits when it comes to the program. I'm essentially trying to color shapes in worksheet 2 (WS2) based off 3 different date ranges in various cells in worksheet 1 (WS1). WS1 contains the data, which, in column A, lists all the counties in my state. In WS1 column C there is a date, showing the date establishments in that county were visited. Each county name in column A has a corresponding date in column C. (If it matters for my question, column C is conditionally formatted so the dated cell automatically highlights red, yellow, or green, depending on the date in the cell. If the date in the cell is within 12 months of the day the spreadsheet was opened, the cell automatically highlights itself green. If over 12 months old but under 24 months, cell highlights yellow. If 24 months or older, cell highlights red.)

Now the shapes. The shapes are actually shapes of each county in my state, inserted into WS2 from a .wfm file. It's essentially a state map with county boundaries. Each shape (each county) is named as such: "Kittson," for Kittson county, "Itasca," for Itasca county, and so on and so forth.

After an employee visits all establishments in a given county, the employee opens up this shared Excel workbook and changes the date in WS1 column C from whatever it was to the current day's date. At this point the dated cell in WS1 changes from yellow or red to green...unless the dated cell was green to begin with.

My question: How do I make each shape (county) in WS2 automatically highlight itself red, yellow, or green based off the dates in cells in WS1 column C using the same or similar <12, <24, >24 months conditionally formatting rule mentioned earlier?

Don't laugh, bit this is what I've come up with:

Expand|Select|Wrap|Line Numbers
  1. 'NOTE if any of the cells change for county or zip respective cells, the following code MUST be adjusted
  2. ' accordingly else script will break!
  3.  
  4. Private Sub Worksheet_Change(ByVal Target As Range)
  5.  
  6.     Dim sh As Shape
  7.  
  8.     If Target.Count > 1 Then Exit Sub
  9.     If Not Intersect(Range("C2,C17,C18,C19,C20,C21,C22,C23,C24,C25,C26,C27,C28,C29,C30,C31"), Target) Is Nothing Then
  10.  
  11.         'Specify cell dates to look at and each cell date's corresponding couinty shape on state map
  12.         With Sheets("STATE")
  13.             Select Case Target.Address(0, 0)
  14.                 Case "C2": Set sh = .Shapes("Aitkin")
  15.                 Case "C17": Set sh = .Shapes("Becker")
  16.                 Case "C18": Set sh = .Shapes("Beltrami")
  17.                 Case "C19": Set sh = .Shapes("Benton")
  18.                 Case "C20": Set sh = .Shapes("Big_Stone")
  19.                 Case "C21": Set sh = .Shapes("Blue_Earth")
  20.                 Case "C22": Set sh = .Shapes("Brown")
  21.                 Case "C23": Set sh = .Shapes("Carlton")
  22.                 Case "C24": Set sh = .Shapes("Carver")
  23.                 Case "C25": Set sh = .Shapes("Cass")
  24.                 Case "C26": Set sh = .Shapes("Chippewa")
  25.                 Case "C27": Set sh = .Shapes("Chisago")
  26.                 Case "C28": Set sh = .Shapes("Clay")
  27.                 Case "C29": Set sh = .Shapes("Clearwater")
  28.                 Case "C30": Set sh = .Shapes("Cook")
  29.                 Case "C31": Set sh = .Shapes("Cottonwood")
  30.  
  31.             End Select
  32.         End With
  33.  
  34.         'Set shape color
  35.         Select Case Target.Value
  36.             Case Is >= Now, -25: sh.Fill.ForeColor.RGB = vbYellow
  37.             Case Is >= Now, -12: sh.Fill.ForeColor.RGB = vbGreen
  38.             Case Is <= Now, -25: sh.Fill.ForeColor.RGB = vbRed
  39.             Case Else: sh.Fill.ForeColor.RGB = vbWhite
  40.         End Select
  41.  
  42.     End If
  43.  
  44.     End Sub
  45.  
  46.  
The above doesn't include all counties because I noticed pretty quickly that for reasons maybe obvious to you it didn't work. As you can see I attempted to call out each individual shape and cell because I didn't think a loop would work since WS1 column A doesn't include only counties. Yes, I lied earlier - WS1 column A contains mostly counties, except for those counties with 100 or more establishments. For THOSE counties the county is still named in, say, cell A2, but then A3 through A10 has zip codes with their respective dates in column C. Zip codes are not represented on my county state map. Those counties further separated by zip could remain not colored in WS2.

Last lie (I'm sorry) WS1 and WS2 have different names; WS1 and WS2 are named "Counties & Zip Codes" and "STATE," respectively, if this matters.

Lord knows I'm asking a lot. Any feedback would be greatly appreciated : )
Apr 24 '15 #1
8 3336
zmbd
5,501 Expert Mod 4TB
Never having done this myself in excel, conditional formatting of the cells is often enough for my work, it will take me a little bit to determine if this is possible. My thought is along the same as yours that the shape object properties are the way to go in this; however, I think that you may be stuck with the color that is embedded in the imported shape.
It might also help to give your cells in WS1:C# range names. My thought here is that you can then use the on change event with a select case against the range name to select the correct shape (once again if that is possible) for highlighting. The shapes I suspect are going to be handled a lot like they are in powerpoint or word... by shaperange.

Need to have a little bit more info on what you mean by highlight too... you can attach standard image files to your posts via the [Advanced] editor. If you use a third party site I may not be able to view them depending on which PC I'm at.


One thing to note: "Counties & Zip Codes" as an ampersand in the name... this is a no-no as the ampersand is a reserved token.

Although the following links refer to Access, the general premises are the same especially when it comes to VBA.
Access 2007 reserved words and symbols
AllenBrowne- Problem names and reserved words in Access



(( Just an aside: In Excel "Macros" and "VBA" are the same thing. In Access these are different programing languages. Thus, you can simply refer to VBA code in Excel and we'll follow right along. :) ))
Apr 24 '15 #2
gnider
4
Thanks for your reply, zmbd!

I've attached a .zip containing a .xlsm with the inserted .wmf I am using. The .wfm can also be found at http://www.mngeo.state.mn.us/maps/USmaps/index.html and is called "MN.WMF." I will attached my full workbook if it's determined I can. That maybe the most helpful.

Also to answer your question, by highlighting the shapes I mean fill color, really, the manual method being selecting the shape and then changing the fill color.

And the ampersand has been changed : ) Thanks again for guidance!
Attached Files
File Type: zip copy.zip (470.0 KB, 125 views)
Apr 26 '15 #3
zmbd
5,501 Expert Mod 4TB
As I suspected, we're going to have to treat the shapes in excel the same way we treat them in powerpoint...

SO here's the link leading to the bases from which I think we should start: ShapeRange Object

This link provides several examples as to how to handle the shape range. I suggest making a copy of your file and playing with these examples to see how they work in your project.

-z
Apr 27 '15 #4
zmbd
5,501 Expert Mod 4TB
gnider:
+ Visited the site you linked to last night from home.

+If you are using that one giant shape, then you will not be able to selectively shade a section of that shape. Or, did you separate the counties into their own shapes?
Apr 27 '15 #5
gnider
4
zmbd,

The shape is odd in that it seems to have multiple "layers" when first imported into Excel: one layer as one big shape and the second layer "hiding" behind the first layer. I essentially deleted the first large layer, enabling me to play around with, move if I choose, or fill each individual shape in the second layer.

So you answer your question it isn't really one giant shape, or it doesn't have to be - it's multiple shapes lined up like a puzzle to form the image of a state map, but by county. Each county is its own shape!

I'm taking a look at the article to which you linked this evening. Thanks!

EDIT: In response to the edit regarding the attachment - I understand : ) Actually I didn't feel comfortable about adding the .zip for your stated reason, but my workbook is mac-enable, making it a .xlsm, which I think is not one of the standard files able to be attached, and the requested image referenced above is a .wmf, which is not a standard image file a thus also not able to be attached. I did try, though : ). But that's also why I added the link. Thanks again!
Apr 27 '15 #6
zmbd
5,501 Expert Mod 4TB
OK,
I've played around with the image from the website link... thank you for pointing out the layers. I don't do much with WMF formats.

As I suspected, it works very much like the MSWord shapes.

Please copy and paste the following code into a new module in a copy of your workbook... from here the next step is to pass to your code the correct cell.


Two macros so that you can see how to both set and clear the fill.
I guessed at the names for the shapes being the county names.
Expand|Select|Wrap|Line Numbers
  1. Option Explicit
  2.  
  3. Sub SelectTwoandColour()
  4. '
  5.     ThisWorkbook.Worksheets("STATE").Shapes.Range(Array("Kittson")).Select
  6.     With Selection.ShapeRange.Fill
  7.         .Visible = msoTrue
  8. 'Standard red
  9.         .ForeColor.RGB = RGB(255, 0, 0)
  10.         .Transparency = 0
  11.         .Solid
  12.     End With
  13.     ThisWorkbook.Worksheets("STATE").Shapes.Range(Array("Lake_of_the_Woods")).Select
  14.     With Selection.ShapeRange.Fill
  15.         .Visible = msoTrue
  16. 'Standard blue
  17.         .ForeColor.RGB = RGB(0, 112, 192)
  18.         .Transparency = 0
  19.         .Solid
  20.     End With
  21.  
  22. End Sub
  23.  
  24. Sub SelectTwoandCLear()
  25. '
  26.     ThisWorkbook.Worksheets("STATE").Shapes.Range(Array("Kittson")).Select
  27.     Selection.ShapeRange.Fill.Visible = msoFalse
  28.     ThisWorkbook.Worksheets("STATE").Shapes.Range(Array("Lake_of_the_Woods")).Select
  29.     Selection.ShapeRange.Fill.Visible = msoFalse
  30. End Sub
  31.  
Apr 27 '15 #7
gnider
4
I see! This fills the correct shapes with the colors called out in code, which is great.

With my first bit of code at the top of this thread, every county called out was turning red, not red, yellow, or green based off its corresponding date in column C of WS1. This is where I was having trouble initially, but I like the way your code handles the fill color better I think. Now I just need to utilize correctly!

That said, I'm shooting to have the fill color of each shape "match" the date in its corresponding cell in WS1. I was attempting to do this earlier with the following code:

Expand|Select|Wrap|Line Numbers
  1.         Select Case Target.Value
  2.             Case Is >= Now, -24: sh.Fill.ForeColor.RGB = vbYellow
  3.             Case Is >= Now, -12: sh.Fill.ForeColor.RGB = vbGreen
  4.             Case Is <= Now, -25: sh.Fill.ForeColor.RGB = vbRed
  5.             Case Else: sh.Fill.ForeColor.RGB = vbWhite
  6.         End Select
  7.  
This, I had hoped, would color everything 24 months ago to the present yellow, 12 months ago to the present green, and anything older than 25 months red. Unfortunately it didn't.

Why?

Next, I wanted Excel to do this for my team automatically, which is why I like your code better. But my next question: is it better to look at the date (or a date range) in a cell and fill a shape with color based off the date range, or would it be better/easier to look at the actual color the dated cell is highlighted and tell each shape to fill itself that same color? Remember each dated cell in WS1 column C is conditionally formatted to highlight itself red, yellow, or green depending upon the date in that cell, so that "automatic" part is already done using conditional formatting.

Thanks again! Trying to learn more about ShapeRange Object in Excel, range names and how to use them, and TODAY, NOW, and EDATE!
Apr 28 '15 #8
zmbd
5,501 Expert Mod 4TB
personally, I'd check the the Interior.Color property of the cell. Easier to do with named ranges.
Apr 28 '15 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

12
by: Steve Elliott | last post by:
I have a query set up to gather together data between two specified dates. Shown in the query column as: Between #24/09/2004# And #01/10/2004# Is it possible to enter several different date...
2
by: junkaccount | last post by:
Hello, Using Access 2000 I would like to create one report that returns sales data for various date ranges input by the user i.e. weekly, monthly, etc. This report is bound to a query that pulls...
67
by: PC Datasheet | last post by:
Transaction data is given with date ranges: Beginning End 4/1/06 4/4/06 4/7/06 4/11/06 4/14/06 4/17/06 4/18/06 4/21/06 426/06 ...
1
by: Matt | last post by:
Hi all, I have a database with a table storing a list of names, invoice dates, and invoice amounts. What I'm looking to do is to create a sum of the invouice amounts based on a range of...
10
by: kyosohma | last post by:
Hi, I am working on a timesheet application in which I need to to find the first pay period in a month that is entirely contained in that month to calculate vacation time. Below are some example...
7
by: Talis | last post by:
Hey guys, Perhaps you can help a fellow programmer out. I have an application that requires me to find the various dates between two date ranges. I am given a startdate and an endDate and I...
16
by: Alex30093 | last post by:
OK This is my 1st post to any forum, but I rely on forums all the time. Short story is I find myself needing to become a Access DBA for work. Situation: I want to use one table of events that...
8
by: xzmilan | last post by:
Totally new to using loop through's but I have a feeling it's what I need. I have two tables tbl1 has a user ID and a change date, a date they updated their information tbl2 has the user ID...
1
by: benny1983 | last post by:
Hi fantastic members, I have created a monstrous calculator for work within my organisation. I have a user inoput screen to get a start and end date so I am pulling variables from there. What i...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.