How i can use a criteria in cicle for i - vba

i want to use a criteria in my loop
but not work
UR = 3
ReDim arng(UR, 3) As Variant
For X = 0 To UR
arng(X, 0) = ConvDate(Cells(X , 8))
arng(X, 1) = ConvDate(Cells(X , 12))
arng(X, 2) = Iif(Cells(X , 12) = "", MsgBox("empty"), MsgBox("Full"))
Next X
even if the cell(X,12) is actually empty both messages show
Why?!?!?
isn't possible to use a criteria??
thank

Worked over a lil bit, try this
dim UR as integer
UR = 3
ReDim arng(UR, 3) As Variant
For X = 0 To UR
arng(X, 0) = ConvDate(Cells(X , 8))
arng(X, 1) = ConvDate(Cells(X , 12))
if Cells(X , 12) = "" then
MsgBox("empty")
else
msgbox("full")
end if
Next X

Related

How do I properly add items in a multilistbox?

I'm using vba to fetch equipment numbers and their corresponding information and putting them in a listbox. A user will enter in the equipment number they want and excel will fetch the info. However, when I click my 'Get Data' button the first time it works ok. When i do it the second time for another equipment number I get the message "Could not set the List property. Invalid property array index." Here's my code:
Dim value As Long
Public i As Integer
Private Sub GetDataButton_Click()
Dim num As Variant
value = EquipmentNumber.value
For Each num In Sheets("S1 Cvtg Eqt List").Range(Range("B1"), Range("B1").End(xlDown))
If num = value Then
MWOList.AddItem (num)
MWOList.List(i, 1) = (num.Offset(0, 1))
MWOList.List(i, 2) = (num.Offset(0, 2))
MWOList.List(i, 3) = (num.Offset(0, 3))
MWOList.List(i, 4) = (num.Offset(0, 4))
MWOList.List(i, 5) = (num.Offset(0, 5))
i = i + 1
End If
Next num
i = i + 1
End Sub
Try below, please note that I had changed not only "i" declaration, and value to public, but also the List column position starts from 0, so if this is 6 element table, then switch it back.
The reason you had error was in fact another "i" iteration "i=i+1" after the loop, the list rows also start from 0, therefore you added 2nd index, and tried to insert it on the third position.
Public value As Long
Public i As Integer
Private Sub GetDataButton_Click()
Dim num As Variant
value = EquipmentNumber.value
For Each num In Sheets("S1 Cvtg Eqt List").Range(Range("B1"), Range("B1").End(xlDown))
If num = value Then
i = MWOList.ListCount 'set i to available space
MWOList.AddItem
MWOList.List(i, 0) = (num.Offset(0, 1))
MWOList.List(i, 1) = (num.Offset(0, 2))
MWOList.List(i, 2) = (num.Offset(0, 3))
MWOList.List(i, 3) = (num.Offset(0, 4))
MWOList.List(i, 4) = (num.Offset(0, 5))
End If
Next num
EquipmentNumber = ""
End Sub

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

More Multidimensional Array Issues

I am trying ti fill arrays based on conditions, and it seems my sample code is sticking values in the wrong position. I included the position where it is being altered. I tried doing some basic +1/-1 fixes but so far all these efforts have misplaced things or returned various other errors.
to be more clear- the issue is that in the v loop, the value is being stuck in the (o,0) position instead of the (o,v) position (over writing the previous value).
I thought the way I had it structured would for each loop perform o's criteria and then check for v's criteria and in turn when v would loop would enter the value in the v position that is equal to the current value of o?
Im clearly missing something and any pointers in the correct direction would be gladly appreciated
Dim Cbox(9, 1)
For o = LBound(Cbox, 1) To UBound(Cbox, 1)
If LPDif2 = 0 Then
Cbox(o, 0) = 0
ElseIf LPDif2 < 3 And LPDif2 > 0 Then
CCount = CCount + LPDif2
Cbox(o, 0) = LPDif2
Else
CCount = CCount + 3
Cbox(o, 0) = 3
LPDif2 = CNLP - CCount
End If
LPDif2 = CNLP - CCount
If CCount = CNLP Then BCount = BCount + 1
WCount = WCount + CW
If CCount < CNLP And CCount >= Round(CLng(CNLP) / 2 + 0.000001, 0) Then DCount = DCount + 1
If CCount >= CNLP Then DCount = DCount + 2
For v = LBound(Cbox, 2) To UBound(Cbox, 2)
If CCount >= Round(CLng(CNLP) / 2 + 0.000001, 0) And DCount = 1 Then
Cbox(o, v) = "Green"
If CTCombo = "9 W" Then
AXWCount = WCount / 2
WH.Value = Ceiling(AXWCount, 9)
Else
WH.Value = WCount
End If
End If
Next v
Next o
Based on the comments, the code inside of the inner loop should only be running if the value of v is 1. It should be as simple as just eliminating the inner loop entirely and hard coding the value "1":
For o = LBound(Cbox, 1) To UBound(Cbox, 1)
'...
If CCount >= Round(CLng(CNLP) / 2 + 0.000001, 0) And DCount = 1 Then
Cbox(o, 1) = "Green"
If CTCombo = "9 W" Then
AXWCount = WCount / 2
WH.Value = Ceiling(AXWCount, 9)
Else
WH.Value = WCount
End If
End If
Next o

Parsing a Number

Here is my problem:
I am parsing a xml using excel VBA. I open the XML file as regular excel file, Then I go to column which is of my interest.
Cells in that column are 16 character.
So I am parsing each of those characters using Left and Right functions and saving them in a new file.
Mostly those 16 characters are Numeric and sometimes Alpha Character.
example 1111000011110000 or 1111YYYY00001111
when it parses 1111YYYY00001111 -- I get correct output,
like in new file Cell(1,1) =1 Cell(1,1) =1 Cell(1,2) =1 Cell(1,3) =1 Cell(1,4) =1 Cell(1,5) =Y and so on
But When I parse 1111000011110000 -- I dont get the correct output
I get Cell(1,1) =1 Cell(1,2) =. Cell(1,3) =1 Cell(1,4) =1 Cell(1,5) =1 Cell(1,6) =0
I tried to put NumberFormat="0" for column which I was parsing, but still i get the same output.
I also tried to put the debug variables and stored them as strings, Double Integer but I could not get pass it.
Here is my code:
Workbooks.Add
strip_concat = ActiveWorkbook.Name
Workbooks(strip_concat).Sheets(1).Cells(1, 1) = "Subid"
Workbooks(strip_concat).Sheets(1).Cells(1, 2) = "Strip#"
Workbooks(strip_concat).Sheets(1).Cells(1, 3) = "X"
Workbooks(strip_concat).Sheets(1).Cells(1, 4) = "Y"
Workbooks(strip_concat).Sheets(1).Cells(1, 5) = "Reject Code"
Workbooks(strip_concat).Sheets(1).Cells(1, 6) = "X-Y"
Workbooks.OpenXML Filename:=xml_file
stripfile = ActiveWorkbook.Name
For C = 3 To strip_col - 1
r = 1
Do
Y = r
x = C - 2
Workbooks(strip_concat).Sheets(1).Cells(A, 3) = x
Workbooks(strip_concat).Sheets(1).Cells(A, 4) = Y
Workbooks(strip_concat).Sheets(1).Cells(A, 6).NumberFormat = "#"
Workbooks(strip_concat).Sheets(1).Cells(A, 6) = x & "-" & Y
Workbooks(strip_concat).Sheets(1).Cells(A, 1) = Workbooks(stripfile).Sheets(1).Cells(C, Amkor_id_col)
Workbooks(strip_concat).Sheets(1).Cells(A, 2) = ExtractElement(Workbooks(stripfile).Sheets(1).Cells(C, Strip_num_col), 2, ".")
Workbooks(strip_concat).Sheets(1).Cells(A, 5) = Right(Left(Workbooks(stripfile).Sheets(1).Cells(C, Defect_data_col + i), r), 1)
r = r + 1
A = A + 1
Loop Until (r > 16)
If ((x Mod 13) = 0) Then i = i + 1
Next C
from xml file 1131111111111111
I got it working.. This is what I did
Dim x as variant
x = (Workbooks(stripfile).Sheets(1).Cells(C, Defect_data_col + i).Text
Workbooks(strip_concat).Sheets(1).Cells(A, 5) = Right(Left(x,r),1

Working with Excel ranges and arrays

In VBA, I can easily pull in an sheet\range into an array, manipulate, then pass back to the sheet\range. I'm having trouble doing this in VB.Net though.
Here's my code.
Rng = .Range("a4", .Cells(.UsedRange.Rows.Count, .UsedRange.Columns.Count))
Dim SheetArray(,) As Object = DirectCast(Rng.Value(Excel.XlRangeValueDataType.xlRangeValueDefault), Object(,))
For X As Integer = 0 To SheetArray.GetUpperBound(0)
If IsNothing(SheetArray(X, 0)) Then Exit For
SheetArray(X, 6) = SheetArray(X, 3)
SheetArray(X, 7) = CDbl(SheetArray(X, 3).ToString) - CDbl(SheetArray(X, 1).ToString) - _
CDbl(SheetArray(X, 7).ToString)
For Y As Integer = 0 To 3
SheetArray(X, Y * 2 + 1) = Math.Round(CDbl(SheetArray(X, Y * 2 + 1).ToString), 3)
Next
If Math.Abs(CDbl(SheetArray(X, 7).ToString)) > 0.1 Then _
.Range(.Cells(X + 1, 1), .Cells(X + 1, 8)).Font.Color = -16776961
Next
I'm getting an error on the first If IsNothing(SheetArray(X, 0)) Then Exit For
line. It is telling me index is out of bounds of the array. Any idea why? The SheetArray object contains the data, but I just am not sure how to get to it.
In the For you have to loop from 0 to Count - 1:
For X As Integer = 0 To SheetArray.GetUpperBound(0) - 1
'...
Next
That will fix your problem.