Making Excel VB compare cells and then select all rows which contain the same data in a cell and re-format plus chart [closed]

StackOverflow https://stackoverflow.com/questions/16659376

Question

Simple question but I simply cannot find an answer. I find searching through the awkward excel visual basic manual with only beginner level knowledge very frustrating. Please help.

I have data from a load of stock trades. They are returned to me in the following format:

https://my.syncplicity.com/share/jjuqll2r00/Screen_Shot_2013-05-20_at_23.02.07

PLEASE CLICK LINK ABOVE FOR IMAGE - AM A BEGINNER WITHOUT REPUTATION TO POST DIRECT :)

Hopefully you can see that there is a column labelled market and then a list of different financial assets e.g. Gold.

I need excel to match all the rows with the same entry under the markets column and then colour the rows the same colour. I want it also to create a new table which has an aggregate of the data for that asset which I will then plot relative to the other assets in a pie chart.

The problem is the number of different assets can be unlimited, so I can't be direct with my codes: i.e.

dim rge as range

rge = activesheet.usedrange.find("Market", lookat:=xlpart)
rwnbr = rge.end(xldown).row
colnbr = rge.column

for i = 1 to rwnbr
 if cells(i,colnbr).value = "*Gold*" then cells.(i,colnbr).interior.colorindex = 3
 end if
 if ..................... = "*Xstrata*" then .......................
 end if
next i

The above would be no good since I would have to tell excel which assets should match. I need to automate it so that it colors the rows differently if they are different assets (and there can be any number of different assets so it would have to choose separate colours for each). I need it to then create a new summary table of the trades for that asset so it will show overall profit/loss and then put it into a pie chart.

This should be easy once I know how to code VBA to operate on a selection of identical (but not initially told) data.

Please please help!

Thank you.

Was it helpful?

Solution

Assuming the data is already sorted by Market column, as per your screenshot, in order to do the color formatting you could do something like:

Sub ColorMyTable()
Dim rng as range 'column range
Dim r as Long 'row iterator
Set rng = Activesheet.Range("E2",Range("E2").End(xlDown))

'## Assign some color to the last cell in this column
rng.Cells(1).Interior.ColorIndex = 39

For r = rng.Rows.Count to 1 Step - 1
    With rng.Cells(r)
    'Check to see if the cell is the same value as the one beneath it
        If .Value = .Offset(1, 0).Value Then
            '## Make them the same color
            .Offset(1,0).Interior.ColorIndex = .Interior.ColorIndex

            '
        Else:
            '## Code to apply a new semi-random color to the next row
            ' , when the values don't match.
            .Offset(1, 0).Interior.ColorIndex = .Offset(1,0).Row Mod 55

        End If
    End With
Next
End Sub 

This is the easiest, and also probably the ugliest, way I can think of to assign colors without any assignment in advance.

There is no check to see if the color has already been used, but I think this should come up with 55 different colors for you.

Unfortunately, unless you have some idea of what colors to use in advance, I can't think of a really easy way to assign colors. But here are a few more complicated methods you might try to adapt:

  • You could for instance use a single color's long value, and then apply a particular .TintAndShade factor based on how many unique Asset's there are in the column. Like, this: How to assign gradient colors to an excel column?
  • On a similar note, you could assign a few colors as public variables, or use the built in constants from the color Theme (assuming Excel 2007+) and use a VBA function to rotate through these colors, also applying .TintAndShade, if necessary, just like the above, but with a bit more tweakability to use multiple colors.
  • You could use a random number generator to assign the color values, storing used colors in a collection, and checking for duplicate colors (and forcing a new color, if needed), etc.

OTHER TIPS

Code that fails with naming range:

Dim ran9e As Range
Set ran9e = ActiveSheet.UsedRange
Range("A1").Select
Sheets.Add.Name = "Summary"

ActiveWorkbook.PivotCaches.Create(xlDatabase, ran9e, xlPivotTableVersion14).CreatePivotTable tabledestination:=Sheets("Summary"), _
tablename:="PivotTable1", defaultversion:=xlPivotTableVersion14
Sheets("Summary").Select
Cells(3, 1).Select
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top