VBA USER FORM error 6 work around - vba

I currently have a bunch of Combo boxes that has a option for Yes, No or N/A
Then I Have a Command button that that runs a calculation then moves to the next page
Private Sub CommandButton2_Click()
Dim a As Long, b As Long
a = IIf(Cbx1_1.Value = "Yes", 1, 0) + IIf(Cbx1_2.Value = "Yes", 1, 0) +_
IIf(Cbx1_3.Value = "Yes", 1, 0) + IIf(Cbx1_4.Value = "Yes", 1, 0)
b = 4 - IIf(Cbx1_1.Value = "N/A", 1, 0) - IIf(Cbx1_2.Value = "N/A", 1, 0)_
- IIf(Cbx1_3.Value = "N/A", 1, 0) - IIf(Cbx1_4.Value = "N/A", 1, 0)
OUTBX1.Text = Format(a / b, "00.00%")
MultiPage1.Value = 1
End Sub
Now the Only Problem that I am experiencing now is that if all the boxes are check as "N/A" then I get the 0/0 problem (Error 6 : Overflow)
I know that with Excel I would have used an ERRORIF formula to solve this. Is there a way that I can tell VBA that if:
=ERRORIF (b=0) then MultiPage1.Value = 1 else
OUTBX1.Text = Format(a / b, "00.00%")

Thank you to everyone for you help the final answer to this is coded as following:
Private Sub CommandButton2_Click()
Dim a As Long, b As Long
a = IIf(Cbx1_1.Value = "Yes", 1, 0) + IIf(Cbx1_2.Value = "Yes", 1, 0) + IIf(Cbx1_3.Value = "Yes", 1, 0) + IIf(Cbx1_4.Value = "Yes", 1, 0)
b = 4 - IIf(Cbx1_1.Value = "N/A", 1, 0) - IIf(Cbx1_2.Value = "N/A", 1, 0) - IIf(Cbx1_3.Value = "N/A", 1, 0) - IIf(Cbx1_4.Value = "N/A", 1, 0)
If b = 0 Then
MultiPage1.Value = 1
Else
OUTBX1.Text = Format(a / b, "00.00%")
MultiPage1.Value = 1
End If
End Sub

Related

vb.net chart: How to get AxisX.CustomLabels in sync with AxisX.MajorTickMark

As shown in the code, I get CustomLabels displayed, but they are not on the MajorTickMarks defined in the ChartArea. How do I get this in sync?
vb.net
Dim from_X, to_X As Date
from_X = myClass.get_DateOfWeek(CInt(yearkNo), CInt(weekNo), DayOfWeek.Monday)
'Last week from mainTable
weekNo = mainTable.Columns(mainTable.Columns.Count - 1).ColumnName.Split(CChar("/"))(0).Substring(2, 2)
yearkNo = mainTable.Columns(mainTable.Columns.Count - 1).ColumnName.Split(CChar("/"))(1).Substring(0, 4)
to_X = myClass.get_DateOfWeek(CInt(yearkNo), CInt(weekNo), DayOfWeek.Saturday)
Dim ints as integer = CInt(DateDiff(DateInterval.WeekOfYear, from_X, to_X, FirstDayOfWeek.Monday, FirstWeekOfYear.FirstFullWeek))
Dim xdate(ints) As Date 'is looped through and the date of the respective week is added.
newchart(chart1) 'create new chart
Dim chartArea1 As New ChartArea("Default")
chart1.ChartAreas.Add(chartArea1)
chart1.ChartAreas("Default").AxisX.IntervalType = DateTimeIntervalType.Weeks
chart1.ChartAreas("Default").AxisX.Interval = 1
chart1.ChartAreas("Default").AxisX.LabelAutoFitStyle = LabelAutoFitStyles.DecreaseFont
chart1.ChartAreas("Default").AxisX.LabelAutoFitMinFontSize = 7
chart1.ChartAreas("Default").AxisX.LabelStyle.Font = My.Settings.fontbold8
chart1.ChartAreas("Default").AxisX.LabelStyle.Angle = 90
chart1.ChartAreas("Default").AxisX.MajorTickMark.Enabled = True
chart1.ChartAreas("Default").AxisX.MinorTickMark.Enabled = False
chart1.ChartAreas("Default").AxisX.Minimum = from_X.ToOADate()'44443
chart1.ChartAreas("Default").AxisX.Maximum = to_X.ToOADate()'44828
chart1.ChartAreas("Default").AxisX.IsMarginVisible = False
chart1.Series.Add("K").Color = ColorTranslator.FromHtml("#297AB7") 'MattBlau colorx(0)
chart1.Series("K").Points.DataBindXY(xdate, yValues)
chart1.ChartAreas("Default").AxisX.CustomLabels.Clear()
For intVal As Integer = 0 To ints - 1
Debug.Print(intVal & " - " & Format(xdate(intVal), "yyyy-MM-dd"))
Select Case intVal
Case 0, 5, 10, 15, 20, ints - 2
chart1.ChartAreas("Default").AxisX.CustomLabels.Add(xdate(intVal).ToOADate(), xdate(ints - 1).ToOADate(), myClass.get_WeekNumber(xdate(intVal)) & "/" & xdate(intVal).Year)
End Select
Next
It looks now like here in the picture:
https://www.spearhead-home.com/Downloads/20220517_XAchseKWs.jpg
Found now a solution for me:
AxisX.IntervalType, AxisX.Minimum, AxisX.Maximum must match the series DataBindXY(xValues, yValues)
Dim ints as integer = CInt(DateDiff(DateInterval.WeekOfYear, von_X, bis_X, _
FirstDayOfWeek.Monday, FirstWeekOfYear.FirstFullWeek))
Dim xdate(ints) As Date 'is looped through and the date of the respective week is added.
Dim xInt(ints) As Integer 'is looped through and the numbers of the interval-count added.
chart1.ChartAreas("Default").AxisX.IntervalType = DateTimeIntervalType.NotSet
chart1.ChartAreas("Default").AxisX.Interval = 1
chart1.ChartAreas("Default").AxisX.Minimum = 0
chart1.ChartAreas("Default").AxisX.Maximum = ints - 1
chart1.ChartAreas("Default").AxisX.CustomLabels.Clear()
For intVal As Integer = 0 To ints - 1
Dim kw_run As String = ""
kw_run = myClass.set_WeekFormat(myClass.get_WeekNumber(xdate(intVal)), xdate( _
intVal), True)
'result looks like: 2022/20
chart1.ChartAreas("Default").AxisX.CustomLabels.Add(intVal, intVal + 1, kw_run, _
0, LabelMarkStyle.None)
Next
'Series
chart1.Series("mySeries").Points.DataBindXY(xInt, yValues)
'...

Excel VBA sum all cells with matching criteria

I've been trying to do an Overall Equipment Effectiveness (OEE), and for that I need to be able to sum all the good produced items and divide over the total production. So i get a quality índex...
The criteria are "today" , shift (1 or 2 or 3 ) , piece number
So far I've done this...
Private Sub CommandButton1_Click()
Dim last, i As Integer
Dim ref As String, turno As String, dia As String, estado As String, pecasap As String, pecaspr As String
'Sheets("analisegeral").Select
folha = estadoform.Label1.Caption
last = Application.ThisWorkbook.Worksheets(folha).Range("A65536").End(xlUp).Row
Application.ThisWorkbook.Worksheets(folha).Cells(last + 1, 5) = fimturnoform.ComboBox1 'peça
Application.ThisWorkbook.Worksheets(folha).Cells(last + 1, 6) = "FIM TURNO"
Application.ThisWorkbook.Worksheets(folha).Cells(last + 1, 7) = "FINALIZADO" 'estado trabalho (A DECORRER/FINALIZADO)
Application.ThisWorkbook.Worksheets(folha).Cells(last + 1, 9) = fimturnoform.TextBox1.Text 'fase
Application.ThisWorkbook.Worksheets(folha).Cells(last + 1, 12) = Now() 'HORA FIM TURNO
Application.ThisWorkbook.Worksheets(folha).Cells(last + 1, 14) = fimturnoform.TextBox3.Text 'peças aprovadas
Application.ThisWorkbook.Worksheets(folha).Cells(last + 1, 15) = fimturnoform.TextBox2.Text 'peças produzidas
'' Day (Now)
For i = 2 To last
dia = Cells(i, 3)
turno = Cells(i, 4)
ref = Cells(i, 5)
estado = Cells(i, 6)
pecasap
pecaspr
If (Day(Now) = dia And ComboBox2 = turno And ComboBox1 = ref And (estado = "FIM LOTE" Or estado = "FIM TURNO")) Then
End If
Next i
GoTo fim
fim:
End Sub

Value of type 'Single' can't be converted to 'System.Windows.Forms.DataGridViewCell'

For i = 0 To 2
If Niz1(i) > Niz2(i) Then
a = Niz1(i)
b = Niz2(i)
Call ZamjenaNiza(a, b)
Niz1(i) = Prvi
Niz2(i) = Drugi
End If
Next i
For j = 0 To 3
Me.DataGridView(j + 1, 1) = Niz1(j)
Me.DataGridView(j + 1, 2) = Niz2(j)
Next j
End Sub
Can anyone please help me with this problem? Can't find solution, not even on Visual Basic help page! It shows me error on this 2 code lines:
Me.DataGridView(j + 1, 1) = Niz1(j)
Me.DataGridView(j + 1, 2) = Niz2(j)
It says : Value of type 'Single' cannot be converted to 'System.Windows.Forms.DataGridViewCell'
Instead of trying to assign a Single value to a DataGridViewCell object, you should assign it to the .Value property of the object ..
Me.DataGridView(j + 1, 1).Value = Niz1(j)

If statement results overwriting each other VBA

I am experiencing a problem with the outputs from my loop. As the sub is running I can see that the results from the final IF statement are being overwritten by the results from the second one. My code is structured as follows:
for i = 1 to 5
for j = 1 to 50
for each events.value in eventArray
if events.value = arrayElem then
if cells(i,j).value = "x" then
type = "col1"
elseif cells(i,j).value = "y" then
date = "col2"
elseif cells(i,j).value = "z" then
num = "col3"
end if
count = count + 1
activeworkbook.worksheets("output").cells(count + 1, 1) = type
activeworkbook.worksheets("output").cells(count + 1, 2) = date
activeworkbook.worksheets("output").cells(count + 1, 3) = num
end if
next arrayElem
if cells(i,j).value = "a" then
name = "row1"
elseif cells(i,j).value = "b" then
size = "row2"
elseif cells(i,j).value = "c" then
height = "row3"
end if
activeworkbook.worksheets("output").cells(count + 2, 1) = name
activeworkbook.worksheets("output").cells(count + 2, 2) = size
activeworkbook.worksheets("output").cells(count + 2, 3) = height
next j
next i
Obviously these are dumby variables and results, but the overall structure is the same as the real code. I can see "name","size", and "height" being printed, but then they get replaced by "type", "date", and "num". How do I prevent this from happening? Each time a new event is found I need it to print its associated characteristics printed into a new row in the "output" sheet.
Consider the following simplified version of your code:
For i = 1 To 100
If x = y Then
rowNum = rowNum + 1
Cells(rowNum + 1, 1) = "A"
End If
Cells(rowNum + 2, 1) = "B"
Next
Each time through the loop you are writing out either one or two things (two if x = y is true, one if it isn't) but you are only incrementing the row number by zero or one (one if x = y is true, zero if it isn't). Even if you know that x will always equal y, you are still trying to write two rows of information out but only increasing the row counter by one.
Assuming you are not trying to replace the "B"s in my example with the "A"s from the next iteration through the loop, you should change the code to something like:
For i = 1 To 100
If x = y Then
rowNum = rowNum + 1
Cells(rowNum, 1) = "A"
End If
rowNum = rowNum + 1
Cells(rowNum, 1) = "B"
Next

Extract from multidimensional array

I have an array dat that shows Type = Variant/Variant(0 to 500, 0 to 0, 0 to 1)
There is a "column" of dates:
dat(0, 0, 0) = #1/1/2013#
dat(1, 0, 0) = #1/2/2013#
I want to extract this set of dates. I tried:
Dim dat As Variant
Dim dt As Variant
'stuff gets dat in the format described above
dt = Application.Index(dat, 0, 1, 1)
Unfortunately this gives me an Error 13 Type Mismatch. What am I doing wrong?
Use a Loop
Sub dural()
Dim dat(0 To 500, 0 To 1, 0 To 1) As Variant
dat(0, 0, 0) = #1/1/2013#
dat(1, 0, 0) = #1/2/2013#
Dim dt(0 To 500) As Variant
For i = 0 To 500
dt(i) = dat(i, 0, 0)
Next i
End Sub