I have a chart with xvalues Green, Blue, and Red and I want to only add Data Labels to the Red Values. But the colors aren't always in the same order, so I cant use some thing like: Chart.SeriesCollection(n).Points(j).DataLabel.Delete
I want to be able to do something like this: If Chart.SeriesCollection(n).Points(j).DataLabel.XValues = "Red" Then Chart.SeriesCollection(n).Points(j).DataLabel.Delete
But the .XValues command in vba doesnt work like that. Any suggestions? Thanks in advance.
In your description, you say that you want to add data labels to the Red XValues, yet your code deletes them. So I am going to assume that you want to delete the data labels for the Red XValues. Change the chart reference accordingly.
Dim sr As Series
Dim n As Long
Dim j As Long
With Worksheets("Sheet1").ChartObjects("Chart 1").Chart
For n = 1 To .SeriesCollection.Count
Set sr = .SeriesCollection(n)
sr.HasDataLabels = True
For j = 1 To sr.Points.Count
If sr.XValues()(j) = "Red" Then
sr.Points(j).DataLabel.Delete
End If
Next j
Next n
End With
No need for VBA.
Here's my setup. Categories, including maybe "Red" in column A, Values in column B, and chart labels in column C. In cell C2 I'm using a formula like this:
=IF(A2="Red","Label Text Here","")
so there is only text in that column if the X value is "Red".
My chart plots columns A and B of the data range. I added data labels, then formatted the data labels to use Values from Cells, and selected C2:C7 for the cells with labels.
The nice thing is it's dynamic. If the data changes, the labels update without having to add the labels back and rerunning any code.
Related
I have a large table that only shows a single type of information: whether or not a species of plant was present at a particular study site. I have 500+ species listed in the first column, and 30 sites as column names. The table is populated with a simple "Y" or "N" to show presence. Example:
Scientific Name Old Wives Beach Dadi Orote N Airstrip
Abelmoschus moschatus N N N
Abrus precatorius Y N Y
Abutilon indicum N N N
However, the species list contains some species that do not occur at any sites, rendering a row full of "N"s, like the 1st and 3rd rows in the example above. I need to delete those rows in order to make the table more manageable.
Is there any way to achieve this without a long IF AND statement?
Inspired by pnuts' comment, in a new column, use the a COUNTIF() formula. For example, =COUNTIF(B2:AE2,"Y"), assuming the row/column headers are in row 1 and column A and the data is in the range B2:AE501+.
If you then select the entire range, including the headers and the new formula column and add filters, then you can select only the rows where the count of Y's is 0. Once you have only the 0's showing, you can select the entire rows and delete them (using Right-Click, Delete) without effecting the non-zero rows.
At this point, if you no longer need the count column, you can turn off the filter and delete the column but I wouldn't be surprised if you find the count comes in handy for some other reason.
Alternately, you could just use the filter to HIDE the 0 rows rather than delete them and that way to don't remove the data altogether but it's no longer in your way.
The code below is one way to do this, assuming there are no gaps in the data. The animated gif steps through to demonstrate how it works. You should remove the .select statements once you understand it.
Sub deleteIfAllN()
Dim plantR As Range, cell As Range, allN As Boolean
Set plantR = Range("A2")
While plantR <> ""
plantR.Select
Set r = plantR.Offset(0, 1)
allN = True
Do
r.Select
If r <> "N" Then
allN = False
Exit Do
End If
Set r = r.Offset(0, 1)
Loop Until r = ""
Set plantR = plantR.Offset(1, 0)
Rows(plantR.row - 1).Select
If allN Then Rows(plantR.row - 1).Delete
Wend
End Sub
You can use the Advanced Filter
Set up your data and criterion area as below
For the example you posted, the formula would be:
=COUNTIF($B8:$D8,"N")<>3
For 30 columns, just modify the range and the count.
Before
After
I chose to filter in place
Note that there is also an option to Copy to another location which would place the results of the filter in another location.
I am new completely new to coding in excel (2013) VB and I need to write some code to do the following:
I have a 2013 workbook with multiple tabs. I need to delete values (could be hard coded numbers, categorical data [e.g., "Yes/No"], or formulas) from only SOME of these tabs (let's say tabs X and Y) based upon cell fill color.
1) First, how do I obtain the color index or, more specifically the color index property (I might not be using the exact correct terminology here)? I assume I need to get this exact number from a function and then plug this number into some sub to delete the conditioned values.
2) Then, how do I use that value to write a sub (?) that will delete all the values in that specific cell color for tabs X an Y?
Any help would be much appreciated.
Thanks.
A cell's color property is held in the cell's interior object. To get the color from cell B3 in Tab 'X' you could do:
Dim cellColor as Long
cellColor = Sheets("X").Range("B3").Interior.Color
Now the cell's color is held in variable cellColor.
This color property is a little oddball though. It's a numeric representation of the RGB, taking the form: (R*256^2 + G*256 + B) Kind of like a bitmask, but at the byte level. Check out the function in the second answer here for a great way to get at these values if you need them.
Really your best bet is to paint a cell, then run a macro to tell you what the color code is.
Pretending like you want to delete everything in Sheet X Range A1:Z5000 that are colored red (65535), you could do something like:
Dim checkCell as Range 'cells are range objects
For each checkCell in Sheet("X").Range("A1:Z5000").Cells 'loop through all the cells assigning the cell in each iteration to variable checkCell
If checkCell.Interior.Color = 65535 Then 'is it red?
checkCell.ClearContents 'null out the value
End If
Next checkCell
I need to dynamically create and delete 1 - N number of listboxes on a worksheet. Because the page will be changing I can't know in advance where to place the control so a statement like ... Left:=10, Top:=10, Width:=106.8, Height:=154.6 won't work. I need to be able to programmatically place controls at C1 or C55 for example.
The research I've done found only examples of static creation at a fixed location and I also haven't been able to find anything that will give me the coordinates of a cell (ex: C1 = Left:=65, Top:87).
Thanks for your help.
Consider this:
Dim i&, n&, r As Range
n = 3
With ActiveSheet
For i = 1 To n
Set r = .Range("c1")(i)
.ListBoxes.Add r.Left, r.Top, r.Width, r.Height
Next
End With
This simply demonstrates how to use the dimensions and coordinates of a cell as the dimensions and coordinates of the control. It will be off a bit on height. You can adjust that.
Additionally you can use whatever mechanism you wish to identify the cells. I just did the three top cells of column C.
We are looking at doing some data import. There is a very large complex sheet which has some items grouped together using borders around cells in one column. The only indication that the items are grouped is the fact the group is surrounded by a border. Items ungrouped have no left and right border on the cell (may have top and bottom border as items above and below maybe grouped). As an initial exercise we want to add a column that displays true if an item is grouped. So if there is a border display a value like one. Does anyone know if this possible?
Use this custom VBA function:
Public Function GetBorder(ByVal Rng As Range, Idx As Integer) As Boolean
GetBorder = Rng.Borders(Idx).LineStyle <> xlNone
End Function
It takes two arguments: cell and index of border (7=left, 8=bottom, 9=top, 10=right). Returns TRUE or FALSE.
Now if you want to get info about bottom border of cell A1 you should:
=GetBorder(A1,8)
I have some data, which I have charted as a scatter plot. And I would like to iterate over the points on the chart, and programmatically using VBA determine their
cluster
budget
actual
varaince values
I know I can get (4) variance using the following code, but I am not sure how to get values 1: cluster, 2: budget, 3: actual
Sub t3()
Dim chart As chart
Dim series As series
Dim values As Long
Dim p As Double
Set chart = ActiveChart
Set series = chart.SeriesCollection(1)
p = series.Points.Count
For i = 1 To p
values = series.values(i)
Debug.Print " ", i, values,
Debug.Print ""
Next i
End Sub
Using this information for 1, 2, 3, 4, I will check the cluster, and color the point accordingly A: blue, B, Red, C Green
Then I need to put a legend on the left of the chart with the 3 cluster values and their colors: A: blue, B, Red, C Green
chart
What I want
I chart, colored according to cluster: A, B, or C, where each of A, B, C only appear once in the legend. However the cluster is appearing for each point instead :(
Getting the other values
I'd like to do calculations on each point on the scatter chart, however I am not sure how to get access to the other values (budget and actual), as I am only showing cluster and variance on my chart now.
I simply offset from the start position and stored the values in variables, but I feel there must be a better way of accessing other data in a worksheet to use in a chart? Instead of removing it from the data (when you Select Data >> for the chart, can you hide it and then use it in calculations?
To get the chart only to show each cluster ID once, use this approach. I've added three columns to the data range, one headed by each cluster ID. Select E2:G7, enter this formula
=IF(E$1=$A2,$D2,NA())
and press Ctrl+Enter to fill the selected range with the formula.
Select the shaded range (select one region, then hold CTRL while selecting another) and insert a scatter chart.
Each cluster has a unique legend entry and a distinct format in the chart.
For your other calculations, use formulas in the worksheet.
I don't know why you're calculating your life with VBA when what you need is built right into Excel.