I’m trying to create a macro that produces in-cell graphing as seen over at http://www.juiceanalytics.com/writing/more-on-excel-in-cell-graphing. I’m still fairly new to VBA and have little experience of using Worksheet Functions; this is what I’ve attempted so far.
If someone could help me resolve this it’d be great, because I’ve trawled Google and can’t find anything!
Sub GraphsInCell()
Dim inCellBar As String, barData As Long
barData = ActiveSheet.Range("E4:E" & FinalRow).Value
'repeats "|" string based on Column E values in order to
'produce a small in-cell bar
inCellBar = Application.WorksheetFunction.Rept("|", barData)
For Each i In ActiveSheet.Range("F4:F" & FinalRow)
With ActiveSheet.Range("F4:F" & FinalRow)
.Value = inCellBar
End With
Next
End Sub
This is a small snippet from a larger body of code, but everything else works fine. The offending code is all above.
I basically need to have the REPT function use the values in a relative cell as ‘number_times’ (eg =REPT(“|”, number_times)). Anyone know how to do this or what’s wrong with my code snippet?
Thanks in advance guys.
As background info, I’ve got a small table with the headings, ‘Sender’, ‘Emails’, ‘Letters’, ‘Faxes’, ‘Total’ and ‘Graph’. ‘Total’ is SUM of preceeding three columns and I want graph to contain the REPT function. Really simple set-up.
Final code sample from Lunatik’s answer and Dick Kusleika’s comment is below:
Sub GraphsInCell()
Dim c As Range
Dim TheRange As Range
Set TheRange = Range("E4:E20")
For Each c In TheRange.Cells
If IsNumeric(c.Value) Then
c.Offset(0, 1) = String(c.Value, "|")
End If
Next
End Sub
Not sure of the context of your code as it won’t work without additional variables being set, but I think this does what you are trying to achieve and should point in the right direction for updating your code.
Obviously you’ll need to use your own method for defining the range being used.
Also, although the code handles text or empty cells it will error on negative values. How this is might be best handled would probably depend on your needs.