Macro fails when run but works when stepping through - excel

I am working with a 2 axis chart showing volume with columns and YoY change with no visual representation only data labels. I have written a macro to uniformly move the YoY data labels above each column. The macro works perfectly when stepping through but fails when run.
Sub Move_Data_Point_1_chart()
Set chrt_obj_sig = ActiveSheet.ChartObjects("Volume & YoY")
Set chrt_sig = chrt_obj_sig.Chart
pnt_cnt = chrt_sig.SeriesCollection(1).Points.Count
chrt_sig.FullSeriesCollection(1).DataLabels.Position = xlLabelPositionInsideEnd
For i = 1 To pnt_cnt
chrt_sig_pt = chrt_sig.SeriesCollection(1).Points(i).DataLabel.Top
chrt_sig.SeriesCollection(2).Points(i).DataLabel.Top = chrt_sig_pt - 18
Next i
chrt_sig.FullSeriesCollection(1).DataLabels.Position = xlLabelPositionCenter
End Sub
Here is my expected output.
Correct Output
Here is the failed output.
Incorrect Output
Any suggestions for fixing this?
I have searched and found many similar occurrences but nothing I could apply to my specific situation.
Thanks in advance.

Related

Excel histogram bins adjustment with VBA doesn't seem to work

I'm trying to automatically create couple histograms for given data. The problem is that I can't change bins width with VBA. How to get around this?
(Excel Version 2210 Build 16.0.15726.20188)
I looked through the docs and tried the macro recorder and this is prototype code I came up with.
Sub generateHistograms()
Dim wsTab As Worksheet
Set wsTab = Worksheets("Tabele")
With wsTab.Shapes.AddChart2(-1, xlHistogram)
.Name = "Test"
.Chart.SeriesCollection.NewSeries
.Chart.FullSeriesCollection(1).Values = wsTab.ListObjects(1).ListColumns(3).DataBodyRange
.Chart.ChartGroups(1).BinsType = xlBinsTypeManual
.Chart.ChartGroups(1).BinWidthValue = 10
.Chart.ChartGroups(1).BinsOverflowEnabled = True
.Chart.ChartGroups(1).BinsOverflowValue = 90
End With
End Sub
Running the code line by line seems like BinsType, BinWidthValue, BinsOverflowEnabled and BinsOverflowValue doesn't do anything.

Excel VBA graph based on Series not working properly

I'm using the following VBA code in Excel in order to produce 2 graphs with a data picked up from the sheet and slightly elaborated. As I'm not using ranges directly for the graphs and need to use arrays for X and Y axes, I decided to go for Series. X has Date type and Y has Integer type. First it worked, but then the graphs started giving me wrong data, something random coming directly off the sheet. I suppose there could be something wrong in the way how the charts are declared. Here's the code, what could go wrong with it?
Dim crt1, crt2 As Chart
Dim sr1, sr2 As Series
ActiveSheet.Shapes.AddChart2(201, xlColumnClustered, 60, 50).Select
Set crt1 = ActiveChart
Set s1 = crt1.SeriesCollection.NewSeries()
sr1.Values = dataArray1
sr1.XValues = datesArray1
ActiveSheet.Shapes.AddChart2(332, xlLineMarkers).Select
Set crt2 = ActiveChart
Set s2 = crt2.SeriesCollection.NewSeries()
sr2.Values = dataArray2
sr2.XValues = datesArray2
This problem also persists when I comment the code for the second graph.
P.S. Excel says "Runtime error 424. Object required" underlining "sr1.Values = dataArray1" and then "sr1.XValues = datesArray1"

How to divide and get the percentage of the value of two different textboxes in userform?

i'm new with VBA and i'm currently working on creating an evaluation form for one of our LOBs. I'm having a hard time understanding what is wrong with my current code wherein it would not divide the value of two text boxes. when i tried to divide the value of one text box to a number it would work. however that is not what i'm looking for since the value in the text boxes are dependent on the form questions (there are questions that are NA).
I have tried the code below and its not working.
Sub CusImp2_div()
CusImp2.Value = Val(CusImp1.Value) / Val(CusImp3.Value)
CusImp2.Value = Format(Me.CusImp2.Value, "0.00%")
End Sub
the source of the values are...
Sub custotal_mulcal()
Me.CusImp1.Value = Val(CustTotal1.Value) * (0.33)
Me.CusImp1.Value = Application.WorksheetFunction.Round(Val(CusImp1.Value), 2)
Me.CusImp3.Value = Val(CustTotal2.Value) * (0.33)
Me.CusImp3.Value = Application.WorksheetFunction.Round(Val(CusImp3.Value), 2)
End Sub
when i run the formula and activate the userform i'm experiencing a debug problem and i can't seem to figure out what to do.
i expect the output to be a percentage of the output ex. 96%
i really hope that someone can help me with this and i'm actually frustrated because im runnning after a deadline.
thank you!
'I think I found the answer to this question. I found the formula below in Youtube and the form is working well now. The only limitation though is that the percentage would not change even though I tried to change my answers (I will look into how I can work around this one)..
Sub CusImp2_div()
Dim a As Double
Dim b As Double
Dim answer As Double
a = CusImp1.Value
b = CusImp3.Value
answer = a / b
If b <> 0 Then
answer = a / b
End If
Me.CusImp2.Value = answer
Me.CusImp2.Value = Format(Me.CusImp2.Value, "0.00%")
End Sub

Excel VBA - Get chart data range

I want to add data to a bunch of existing charts. Assume that each chart has a different number of data series and that the location of the raw data is somewhere in the same workbook. Here's what I'm starting with:
For iChart = 1 To iCount
ActiveSheet.ChartObjects("Chart " & iChart).Activate
intSeries = 1
Do Until ActiveChart.SeriesCollection(intSeries).Name = ""
Set rXVal = ActiveChart.SeriesCollection(intSeries).XValues '<- Object Required error
Set rXVal = Range(rXVal, rXVal.End(xlDown))
Set rYVal = ActiveChart.SeriesCollection(intSeries).Values
Set rYVal = Range(rYVal, rYVal.End(xlDown))
ActiveChart.SeriesCollection(intSeries).XValues = rXVal
ActiveChart.SeriesCollection(intSeries).Values = rYVal
intSeries = intSeries + 1
Loop
Next iChart
I know that ActiveChart...XValues = rXVal works, but I'm getting an "Object Required" error on the Set rXVal = ActiveChart....XValues line. I'm assuming that since a range went in to define the data series, I can get that range back out again and then add to it.
UPDATE
To clarify things a little, I have accelerometers in 8 places and FFT software setup to record peak vibration response in 4 separate frequency bands. This yields 32 data points per sample. When exporting, the software spits out an Excel workbook with 4 sheets; one for each frequency band. Each sheet has the accelerometer names going across and sample numbers going down.
I have succeeded using this syntax:
Dim rXVal() As Variant
rXVal = ActiveChart.SeriesCollection(intSeries).XValues
UPDATE
In this case you get an array, because your given statement (ActiveChart.SeriesCollection(intSeries).XValues) is an array and not a range. This is what you see in Locals window if you dig into Series object of ActiveChart.SeriesCollection(intSeries):
(in my dummy data I have rows named r1, r2, r3, r4.)
What I want to say, XValues does not have any property which would indicate its occupied range.
If you actually need a range, I would suggest getting it from the formula property. And the way I would suggest is replacing your error causing line with this one:
Set rXVal = Range(Split(ActiveChart.SeriesCollection(intSeries).Formula, ",")(1))
Next, I see you trying to get the range for Values. Similarly, use this:
Set rYVal = Range(Split(ActiveChart.SeriesCollection(intSeries).Formula, ",")(2))
Another thing.
The following lines will cause you an error finally:
intSeries = 1
Do Until ActiveChart.SeriesCollection(intSeries).Name = ""
...some code...
intSeries = intSeries + 1
Loop
Do change them with:
For intSeries = 1 To ActiveChart.SeriesCollection.Count
...some code...
Next
Yet another thing.
Consider using With and End With, as you repeat a lot ActiveChart.SeriesCollection(intSeries). Then your code will be much more readable, as you would just skip this long line! Wouldn't that be awesome???
This works fine for me:
Dim rXVal() As Variant
Dim rXValMin, rXValMax As Double
rXVal = ActiveChart.SeriesCollection(intSeries).XValues
rXValMin = WorksheetFunction.Min(rXVal)
rXValMax = WorksheetFunction.Max(rXVal)

VBScript - Either getting object required or type mismatch errors

I have scoured the web and this site looking for an answer on this, so I would really appreciate some help.
I'm creating a VBScript to do some modifications to a user-specified Excel spreadsheet. I have the first part of my script working fine, but the second part is driving me nuts. I need it to search the first column for a value and, if found, delete the row. Right now I'm not worrying about the deletion statement--I'm doing testing by seeing if I can get the For Each statement to run properly as well as the If Then statement. Here's the specific block of code:
For Each cell in objSheet.Columns("A:A").Cells
Set cell = objSheet.Columns("A:A").Cells
If cell.Value = "60802400040000" then
cell.font.bold = True
End If
Next
I have tried many variations of this and cannot find the right combination. Initially I was getting an "Object Required" messages, and after reading a number of posts, found that I needed to put in a Set statement for cell, which I did. Now I am getting a Mismatch Type error message.
The funny thing is, before I put in the Set statement, the code would execute, but it would throw the Object Required error when I closed the spreadsheet. After adding it, the error for the Type Mismatch pops up immediately.
Most examples I keep finding on the web are for VBA, and I try to modify them for VBS, which I don't know very well. Any assistance anyone can give me will be greatly appreciated.
You are redefining cell, cell is defined automatically in the For Each statement.
Delete this line
Set cell = objSheet.Columns("A:A").Cells
This is an example from Help, unfortunately Help doesn't have any examples that uses For Each, only For x = n to n and other means. For Each is the right thing to do.
Set r = Range("myRange")
For n = 1 To r.Rows.Count
If r.Cells(n, 1) = r.Cells(n + 1, 1) Then
MsgBox "Duplicate data in " & r.Cells(n + 1, 1).Address
End If
Next n
For vba to vbs, you have to create the object and use, as some objects are automatically available in VBA (like app object) - Set exceldoc = CreateObject("c:\blah\blah.xls) then to use Set r = exceldoc.worksheets(0).range("MyRange").
Also you have to use constant values not names as vbscript can't look them up.

Resources