Run time error '9' VBA Subcript out of range - vba

I interited a sheet at work and there is no one who actually supports anything Excel related. My VBA is rather rusty and hence I hope that someone can help me out here.
I have the following code: It goes in error at line
If mesi(mese) = "JAN" Then anno = Int(Right(oggi, 2)) + 1 Else anno = Int(Right(oggi, 2)) and i get Run-time error '9': Subscript out of range
I have not changed anything and it used to work for a long time. I really appreciate any input
Many thanks
Public Function Pulsante1_Click()
Dim oggi As Date
Dim mesi(1 To 12) As String
Dim prossima_data As String
Dim squarto, sstagione As String
Dim sqa As Range
Dim valore As Double
Dim r As Integer
Dim c As Integer
Dim quarto As Integer
Dim mesi_spalm() As String
Dim valori_spalm() As Double
Dim valor() As Double
Dim anno, mese As Integer
ActiveSheet.Range("J2:K1000000").ClearContents
ActiveSheet.Range("M2:N1000000").ClearContents
ActiveSheet.Range("P2:Q1000000").ClearContents
ActiveSheet.Range("J2:K1000000").Interior.ColorIndex = xlNone
ActiveSheet.Range("M2:N1000000").Interior.ColorIndex = xlNone
ActiveSheet.Range("P1:Q1000000").Interior.ColorIndex = xlThemeColorLight2
mesi(1) = "JAN"
mesi(2) = "FEB"
mesi(3) = "MAR"
mesi(4) = "APR"
mesi(5) = "MAY"
mesi(6) = "JUN"
mesi(7) = "JUL"
mesi(8) = "AUG"
mesi(9) = "SEP"
mesi(10) = "OCT"
mesi(11) = "NOV"
mesi(12) = "DEC"
oggi = Date
mese = (Int(Mid(oggi, 4, 2)) + 1) Mod 12
If mesi(mese) = "JAN" Then anno = Int(Right(oggi, 2)) + 1 Else anno = Int(Right(oggi, 2))
prossima_data = mesi(mese) & Right(anno, 1)
'MsgBox (prossima_data)
If ActiveSheet.Cells(29, 5) = oggi Then
ActiveSheet.Cells(2, 10) = oggi + 1
ActiveSheet.Cells(2, 11) = ActiveSheet.Cells(29, 3)
i = 3
Else
i = 2
End If
If (ActiveSheet.Cells(3, 2) = prossima_data) And (ActiveSheet.Cells(3, 5) = Date) Then
ActiveSheet.Cells(i, 10) = mese & "/20" & anno
ActiveSheet.Cells(i, 11) = ActiveSheet.Cells(3, 3)
i = i + 1
mese = mese + 1
If mese = 13 Then
mese = 1
anno = anno + 1
End If
If InStr(ActiveSheet.Cells(4, 2), "#N/A") = 0 And (ActiveSheet.Cells(4, 5) = Date) Then
ActiveSheet.Cells(i, 10) = mese & "/20" & anno
ActiveSheet.Cells(i, 11) = ActiveSheet.Cells(4, 3)
i = i + 1
mese = mese + 1
If mese = 13 Then
mese = 1
anno = anno + 1
End If
End If
If InStr(ActiveSheet.Cells(5, 2), "#N/A") = 0 And (ActiveSheet.Cells(5, 5) = Date) Then
ActiveSheet.Cells(i, 10) = mese & "/20" & anno
ActiveSheet.Cells(i, 11) = ActiveSheet.Cells(5, 3)
i = i + 1
mese = mese + 1
If mese = 13 Then
mese = 1
anno = anno + 1
End If
End If
If InStr(ActiveSheet.Cells(6, 2), "#N/A") = 0 And (ActiveSheet.Cells(6, 5) = Date) Then
ActiveSheet.Cells(i, 10) = mese & "/20" & anno
ActiveSheet.Cells(i, 11) = ActiveSheet.Cells(6, 3)
i = i + 1
mese = mese + 1
If mese = 13 Then
mese = 1
anno = anno + 1
End If
End If
If InStr(ActiveSheet.Cells(7, 2), "#N/A") = 0 And (ActiveSheet.Cells(7, 5) = Date) Then
ActiveSheet.Cells(i, 10) = mese & "/20" & anno
ActiveSheet.Cells(i, 11) = ActiveSheet.Cells(7, 3)
i = i + 1
mese = mese + 1
If mese = 13 Then
mese = 1
anno = anno + 1
End If
End If
ElseIf ActiveSheet.Cells(4, 2) = prossima_data And (ActiveSheet.Cells(4, 5) = Date) Then
ActiveSheet.Cells(i, 10) = mese & "/20" & anno
ActiveSheet.Cells(i, 11) = ActiveSheet.Cells(4, 3)
i = i + 1
mese = mese + 1
If mese = 13 Then
mese = 1
anno = anno + 1
End If
If InStr(ActiveSheet.Cells(5, 2), "#N/A") = 0 And (ActiveSheet.Cells(5, 5) = Date) Then
ActiveSheet.Cells(i, 10) = mese & "/20" & anno
ActiveSheet.Cells(i, 11) = ActiveSheet.Cells(5, 3)
i = i + 1
mese = mese + 1
If mese = 13 Then
mese = 1
anno = anno + 1
End If
End If
If InStr(ActiveSheet.Cells(6, 2), "#N/A") = 0 And (ActiveSheet.Cells(6, 5) = Date) Then
ActiveSheet.Cells(i, 10) = mese & "/20" & anno
ActiveSheet.Cells(i, 11) = ActiveSheet.Cells(6, 3)
i = i + 1
mese = mese + 1
If mese = 13 Then
mese = 1
anno = anno + 1
End If
End If
If InStr(ActiveSheet.Cells(7, 2), "#N/A") = 0 And (ActiveSheet.Cells(7, 5) = Date) Then
ActiveSheet.Cells(i, 10) = mese & "/20" & anno
ActiveSheet.Cells(i, 11) = ActiveSheet.Cells(7, 3)
i = i + 1
mese = mese + 1
If mese = 13 Then
mese = 1
anno = anno + 1
End If
End If
End If
'MsgBox (mese & " " & anno)
'cercare in foglio reuters il quarter e se la data è di oggi allora moltiplicare il suo valore per i pesi in valori_spalm per ottenere i singoli valori mese
quarto = WorksheetFunction.Ceiling(mese / 3, 1)
squarto = quarto & "Q" & anno
r = 1
c = 1
Set sqa = ActiveSheet.Range("B10:B16").Find(squarto, LookIn:=xlValues)
If Not sqa Is Nothing Then
r = sqa.Row
c = sqa.Column
End If
While Not sqa Is Nothing And ActiveSheet.Cells(r, c + 3) = Date
If Not sqa Is Nothing And ActiveSheet.Cells(sqa.Row, sqa.Column + 3) = Date Then
valore = ActiveSheet.Cells(sqa.Row, sqa.Column + 1)
ReDim mesi_spalm(1 To 3)
Select Case quarto
Case 1
mesi_spalm(1) = mesi(1) & anno
mesi_spalm(2) = mesi(2) & anno
mesi_spalm(3) = mesi(3) & anno
Case 2
mesi_spalm(1) = mesi(4) & anno
mesi_spalm(2) = mesi(5) & anno
mesi_spalm(3) = mesi(6) & anno
Case 3
mesi_spalm(1) = mesi(7) & anno
mesi_spalm(2) = mesi(8) & anno
mesi_spalm(3) = mesi(9) & anno
Case 4
mesi_spalm(1) = mesi(10) & anno
mesi_spalm(2) = mesi(11) & anno
mesi_spalm(3) = mesi(12) & anno
End Select
For j = 1 To 3
If mesi(mese) & anno = mesi_spalm(j) Then Exit For
Next j
If j > 1 Then ReDim valor(1 To (j - 1)) Else ReDim valor(0)
For pp = 1 To (j - 1)
valor(pp) = ActiveSheet.Cells(i - pp, 11)
Next pp
valori_spalm = spalma_mesi(mesi_spalm, valor, valore)
For k = j To 3
ActiveSheet.Cells(i, 10) = mese & "/20" & anno
ActiveSheet.Cells(i, 11) = valori_spalm(k)
i = i + 1
mese = mese + 1
If mese = 13 Then
mese = 1
anno = anno + 1
End If
Next k
End If
quarto = WorksheetFunction.Ceiling(mese / 3, 1)
squarto = quarto & "Q" & anno
r = 1
c = 1
Set sqa = ActiveSheet.Range("B10:B16").Find(squarto, LookIn:=xlValues)
If Not sqa Is Nothing Then
r = sqa.Row
c = sqa.Column
End If
Wend
'MsgBox (mese & " " & anno)
'cercare in foglio reuters il season e se la data è di oggi allora moltiplicare il suo valore per i pesi in valori_spalm per ottenere i singoli valori mese
If mese < 10 And mese >= 4 Then sstagione = "S-" & anno Else sstagione = "W-" & anno
r = 1
c = 1
Set sqa = ActiveSheet.Range("B19:B20").Find(sstagione, LookIn:=xlValues)
If Not sqa Is Nothing Then
r = sqa.Row
c = sqa.Column
End If
While Not sqa Is Nothing And ActiveSheet.Cells(r, c + 3) = Date
If Not sqa Is Nothing And ActiveSheet.Cells(sqa.Row, sqa.Column + 3) = Date Then
valore = ActiveSheet.Cells(sqa.Row, sqa.Column + 1)
ReDim mesi_spalm(1 To 6)
Select Case InStr(sstagione, "S-")
Case Is > 0
mesi_spalm(1) = mesi(4) & anno
mesi_spalm(2) = mesi(5) & anno
mesi_spalm(3) = mesi(6) & anno
mesi_spalm(4) = mesi(7) & anno
mesi_spalm(5) = mesi(8) & anno
mesi_spalm(6) = mesi(9) & anno
Case Is = 0
mesi_spalm(1) = mesi(10) & anno
mesi_spalm(2) = mesi(11) & anno
mesi_spalm(3) = mesi(12) & anno
mesi_spalm(4) = mesi(1) & (anno + 1)
mesi_spalm(5) = mesi(2) & (anno + 1)
mesi_spalm(6) = mesi(3) & (anno + 1)
End Select
For j = 1 To 6
If mesi(mese) & anno = mesi_spalm(j) Then Exit For
Next j
If j > 1 Then ReDim valor(1 To (j - 1)) Else ReDim valor(0)
For pp = 1 To (j - 1)
valor(pp) = ActiveSheet.Cells(i - pp, 11)
Next pp
valori_spalm = spalma_mesi(mesi_spalm, valor, valore)
For k = j To 6
ActiveSheet.Cells(i, 10) = mese & "/20" & anno
ActiveSheet.Cells(i, 11) = valori_spalm(k)
i = i + 1
mese = mese + 1
If mese = 13 Then
mese = 1
anno = anno + 1
End If
Next k
End If
If mese < 10 And mese >= 4 Then sstagione = "S-" & anno Else sstagione = "W-" & anno
r = 1
c = 1
Set sqa = ActiveSheet.Range("B19:B20").Find(sstagione, LookIn:=xlValues)
If Not sqa Is Nothing Then
r = sqa.Row
c = sqa.Column
End If
Wend
'MsgBox (mese & " " & anno)
'cercare in foglio reuters il year e se la data è di oggi allora moltiplicare il suo valore per i pesi in valori_spalm per ottenere i singoli valori mese
r = 1
c = 1
Set sqa = ActiveSheet.Range("B23:B26").Find("20" & anno, LookIn:=xlValues)
If Not sqa Is Nothing Then
r = sqa.Row
c = sqa.Column
End If
While Not sqa Is Nothing And ActiveSheet.Cells(r, c + 3) = Date
If Not sqa Is Nothing And ActiveSheet.Cells(sqa.Row, sqa.Column + 3) = Date Then
valore = ActiveSheet.Cells(sqa.Row, sqa.Column + 1)
ReDim mesi_spalm(1 To 12)
mesi_spalm(1) = mesi(1) & anno
mesi_spalm(2) = mesi(2) & anno
mesi_spalm(3) = mesi(3) & anno
mesi_spalm(4) = mesi(4) & anno
mesi_spalm(5) = mesi(5) & anno
mesi_spalm(6) = mesi(6) & anno
mesi_spalm(7) = mesi(7) & anno
mesi_spalm(8) = mesi(8) & anno
mesi_spalm(9) = mesi(9) & anno
mesi_spalm(10) = mesi(10) & anno
mesi_spalm(11) = mesi(11) & anno
mesi_spalm(12) = mesi(12) & anno
For j = 1 To 12
If mesi(mese) & anno = mesi_spalm(j) Then Exit For
Next j
If j > 1 Then ReDim valor(1 To (j - 1)) Else ReDim valor(0)
For pp = 1 To (j - 1)
valor(pp) = ActiveSheet.Cells(i - pp, 11)
Next pp
valori_spalm = spalma_mesi(mesi_spalm, valor, valore)
For k = j To 12
ActiveSheet.Cells(i, 10) = mese & "/20" & anno
ActiveSheet.Cells(i, 11) = valori_spalm(k)
i = i + 1
mese = mese + 1
If mese = 13 Then
mese = 1
anno = anno + 1
End If
Next k
End If
r = 1
c = 1
Set sqa = ActiveSheet.Range("B23:B26").Find("20" & anno, LookIn:=xlValues)
If Not sqa Is Nothing Then
r = sqa.Row
c = sqa.Column
End If
Wend
'MsgBox (mese & " " & anno)
tro = mesi(mese) & anno
Set sqa = ThisWorkbook.Sheets("ICE").Range("A:A").Find(tro, LookIn:=xlValues)
While Not sqa Is Nothing
ActiveSheet.Cells(i, 10) = mese & "/20" & anno
ActiveSheet.Cells(i, 11) = ThisWorkbook.Sheets("ICE").Cells(sqa.Row, 5) / 1000
ActiveSheet.Cells(i, 10).Interior.Color = RGB(0, 255, 255)
ActiveSheet.Cells(i, 11).Interior.Color = RGB(0, 255, 255)
i = i + 1
mese = mese + 1
If mese = 13 Then
mese = 1
anno = anno + 1
End If
tro = mesi(mese) & anno
Set sqa = ThisWorkbook.Sheets("ICE").Range("A:A").Find(tro, LookIn:=xlValues)
Wend
Pulsante3_Click
End Function
Public Function spalma_mesi(mesi() As String, valo() As Double, media_imp As Double) As Variant
Dim sm() As Double
Dim variazione() As Double
Dim media As Double
Dim nummes As Integer
Dim trov As Range
ReDim sm(1 To UBound(mesi))
ReDim variazione(1 To UBound(mesi))
media_imp = media_imp * 1000
media = 0
nummes = 0
For i = LBound(mesi) To UBound(mesi)
Set trov = ThisWorkbook.Sheets("ICE").Range("A:A").Find(mesi(i), LookIn:=xlValues)
If Not trov Is Nothing Then
If Not IsEmpty(valo) And i <= UBound(valo) Then sm(i) = valo(i) * 1000 Else sm(i) = ThisWorkbook.Sheets("ICE").Cells(trov.Row, trov.Column + 4)
variazione(i) = ThisWorkbook.Sheets("ICE").Cells(trov.Row, trov.Column + 4)
media = media + variazione(i)
nummes = nummes + 1
End If
Next i
media = media / nummes
For ll = LBound(mesi) To UBound(mesi)
variazione(ll) = 1 - (variazione(ll) - media) / media
Next ll
For i = UBound(valo) + 1 To UBound(sm)
sm(i) = (1 - (media - sm(i)) / media) * media_imp
Next i
nummes = 0
media = 0
For i = LBound(sm) To UBound(sm)
nummes = nummes + 1
media = media + sm(i)
Next i
media = media / nummes
While Abs(media - media_imp) > 0.1
va = media_imp - media
For i = UBound(valo) + 1 To UBound(sm)
If va > 0 Then sm(i) = sm(i) + 0.1 Else sm(i) = sm(i) - 0.1
Next i
nummes = 0
media = 0
For i = LBound(sm) To UBound(sm)
nummes = nummes + 1
media = media + sm(i)
Next i
media = media / nummes
Wend
For i = LBound(sm) To UBound(sm)
sm(i) = sm(i) / 1000
Next i
spalma_mesi = sm
End Function

Because, as #Skaterhaz stated, LBOUND(mesi) equals 1 and (Int(Mid(12, 4, 2)) + 1) will return 0 you will need to add one to your formula.
Dim mesi(1 To 12) As String
mese = (Int(Mid(oggi, 4, 2)) + 1) Mod 12 + 1

Related

VBA - How to optimize VBA code to run faster?

I'm running the code below and it takes around 8 hours to complete. It chooses 20 projects (out of 38) by maximizing the utility of the projects while keeping the budget constraint. There is also a constraint on the min/max number of projects of ClassA, ClassB and ClassA and ClassB combined.
The code works fine, however it takes a long time to complete. Do you know how to optimize this code to make it run faster (if possible)?
'''
Sub CalculateOptions()
Dim InputProjects(1 To 38, 4) As Single
Dim TopChoices(1 To 5, 1 To 22) As Single
Dim CurrentChoice(1 To 22) As Single
TotalHorInArray = 22
TotalVertInArray = 5
MinimalProjectsClassA = 9
MaximalProjectsClassA = 14
MinimalProjectsClassB = 5
MaximalProjectsClassB = 9
MinimalProjectsClassAandClassB = 15
MaximalProjectsClassAandClassB = 19
Budget = 49
NumberOfProjects = 38
'Read input
For x = 1 To NumberOfProjects
For y = 1 To 4
InputProjects(x, y) = Cells(2 + x, 4 + y)
Next
Next
For a = 1 To NumberOfProjects
For b = a + 1 To NumberOfProjects
For c = b + 1 To NumberOfProjects
For d = c + 1 To NumberOfProjects
For e = d + 1 To NumberOfProjects
For f = e + 1 To NumberOfProjects
For g = f + 1 To NumberOfProjects
For h = g + 1 To NumberOfProjects
For i = h + 1 To NumberOfProjects
For j = i + 1 To NumberOfProjects
For k = j + 1 To NumberOfProjects
For l = k + 1 To NumberOfProjects
For m = l + 1 To NumberOfProjects
For n = m + 1 To NumberOfProjects
For o = n + 1 To NumberOfProjects
For p = o + 1 To NumberOfProjects
For q = p + 1 To NumberOfProjects
For r = q + 1 To NumberOfProjects
For s = r + 1 To NumberOfProjects
For t = s + 1 To NumberOfProjects
' Utility
UtilityChoice = InputProjects(a, 1) + _
InputProjects(b, 1) + _
InputProjects(c, 1) + _
InputProjects(d, 1) + _
InputProjects(e, 1) + _
InputProjects(f, 1) + _
InputProjects(g, 1) + _
InputProjects(h, 1) + _
InputProjects(i, 1) + _
InputProjects(j, 1) + _
InputProjects(k, 1) + _
InputProjects(l, 1) + _
InputProjects(m, 1) + _
InputProjects(n, 1) + _
InputProjects(o, 1) + _
InputProjects(p, 1) + _
InputProjects(q, 1) + _
InputProjects(r, 1) + _
InputProjects(s, 1) + _
InputProjects(t, 1)
' Check if utility constraint is met (only want to show the top 5 combinations in the output)
If UtilityChoice > max5 Then
' Budget
budgetChoice = InputProjects(a, 3) _
+ InputProjects(b, 3) _
+ InputProjects(c, 3) _
+ InputProjects(d, 3) _
+ InputProjects(e, 3) _
+ InputProjects(f, 3) _
+ InputProjects(g, 3) _
+ InputProjects(h, 3) _
+ InputProjects(i, 3) _
+ InputProjects(j, 3) _
+ InputProjects(k, 3) _
+ InputProjects(l, 3) _
+ InputProjects(m, 3) _
+ InputProjects(n, 3) _
+ InputProjects(o, 3) _
+ InputProjects(p, 3) _
+ InputProjects(q, 3) _
+ InputProjects(r, 3) _
+ InputProjects(s, 3) _
+ InputProjects(t, 3)
' Check if budget constraint is met
If budgetChoice <= Budget Then
' Check number of projects per Class
ClassOfProject = InputProjects(a, 4) + _
InputProjects(b, 4) + _
InputProjects(c, 4) + _
InputProjects(d, 4) + _
InputProjects(e, 4) + _
InputProjects(f, 4) + _
InputProjects(g, 4) + _
InputProjects(h, 4) + _
InputProjects(i, 4) + _
InputProjects(j, 4) + _
InputProjects(k, 4) + _
InputProjects(l, 4) + _
InputProjects(m, 4) + _
InputProjects(n, 4) + _
InputProjects(o, 4) + _
InputProjects(p, 4) + _
InputProjects(q, 4) + _
InputProjects(r, 4) + _
InputProjects(s, 4) + _
InputProjects(t, 4) + 10
ProjectsClassA = Right(ClassOfProject, 2) - 10
ProjectsClassB = Round(ClassOfProject / 100, 0)
ProjectsClassAorB = ProjectsClassA + ProjectsClassB
' Check if minumum and maximum constraint is satisfied for total projects of class A and B
If ProjectsClassA >= MinimalProjectsClassA And ProjectsClassA <= MaximalProjectsClassA _
And ProjectsClassB >= MinimalProjectsClassB And ProjectsClassB <= MaximalProjectsClassB And _
ProjectsClassAorB >= MinimalProjectsClassAandClassB And ProjectsClassAorB <= MaximalProjectsClassAandClassB Then
' Project specifics
CurrentChoice(1) = InputProjects(a, 2)
CurrentChoice(2) = InputProjects(b, 2)
CurrentChoice(3) = InputProjects(c, 2)
CurrentChoice(4) = InputProjects(d, 2)
CurrentChoice(5) = InputProjects(e, 2)
CurrentChoice(6) = InputProjects(f, 2)
CurrentChoice(7) = InputProjects(g, 2)
CurrentChoice(8) = InputProjects(h, 2)
CurrentChoice(9) = InputProjects(i, 2)
CurrentChoice(10) = InputProjects(j, 2)
CurrentChoice(11) = InputProjects(k, 2)
CurrentChoice(12) = InputProjects(l, 2)
CurrentChoice(13) = InputProjects(m, 2)
CurrentChoice(14) = InputProjects(n, 2)
CurrentChoice(15) = InputProjects(o, 2)
CurrentChoice(16) = InputProjects(p, 2)
CurrentChoice(17) = InputProjects(q, 2)
CurrentChoice(18) = InputProjects(r, 2)
CurrentChoice(19) = InputProjects(s, 2)
CurrentChoice(20) = InputProjects(t, 2)
CurrentChoice(21) = UtilityChoice
CurrentChoice(22) = budgetChoice
' Read in project specifics if utility is in the top 5 (and constraints above are met)
If UtilityChoice > max1 Then
max5 = max4
max4 = max3
max3 = max2
max2 = max1
max1 = UtilityChoice
For Z = 1 To TotalHorInArray
TopChoices(5, Z) = TopChoices(4, Z)
TopChoices(4, Z) = TopChoices(3, Z)
TopChoices(3, Z) = TopChoices(2, Z)
TopChoices(2, Z) = TopChoices(1, Z)
TopChoices(1, Z) = CurrentChoice(Z)
Next
ElseIf UtilityChoice > max2 Then
max5 = max4
max4 = max3
max3 = max2
max2 = UtilityChoice
For Z = 1 To TotalHorInArray
TopChoices(5, Z) = TopChoices(4, Z)
TopChoices(4, Z) = TopChoices(3, Z)
TopChoices(3, Z) = TopChoices(2, Z)
TopChoices(2, Z) = CurrentChoice(Z)
Next
ElseIf UtilityChoice > max3 Then
max5 = max4
max4 = max3
max3 = UtilityChoice
For Z = 1 To TotalHorInArray
TopChoices(5, Z) = TopChoices(4, Z)
TopChoices(4, Z) = TopChoices(3, Z)
TopChoices(3, Z) = CurrentChoice(Z)
Next
ElseIf UtilityChoice > max4 Then
max5 = max4
max4 = UtilityChoice
For Z = 1 To TotalHorInArray
TopChoices(5, Z) = TopChoices(4, Z)
TopChoices(4, Z) = CurrentChoice(Z)
Next
ElseIf UtilityChoice > max5 Then
max5 = UtilityChoice
For Z = 1 To TotalHorInArray
TopChoices(5, Z) = CurrentChoice(Z)
Next
End If
End If
End If
End If
Next
Next
Next
Next
Next
Next
Next
Next
Next
Next
Next
Next
Next
Next
Next
Next
Next
Next
Next
Next
' Write output to excel
For x = 1 To TotalVertInArray
For y = 1 To TotalHorInArray
Range("OutputCalculation").Offset(x, y) = TopChoices(x, y)
Next
Next
Range("A1").Select
End Sub
'''
Thanks,
Walther

EXCEL VBA , Allow only if condition verifies

I need a condition that sees if an item has stock in X wharehouse(CC) , if there's any stock than the user can move if there's no stock than the item can't be moved.
Right now i have a condition than only allows only to move from 1 to max stock.
last = Application.ThisWorkbook.Worksheets("Registos").Range("A65536").End(xlUp).Row
For i = 1 To last
REFERENCIA = Application.ThisWorkbook.Worksheets("Registos").Cells(i, 8)
ENTRADAeSAIDA = Application.ThisWorkbook.Worksheets("Registos").Cells(i, 12)
CC = Application.ThisWorkbook.Worksheets("Registos").Cells(i, 6)
Down bellow is my condition that's suppose to check for the check in and if there's no check in can't be any checkout my problema is that the first time i run CommandButton it doesn't works so there will be a checkout without having any check in , and after the first time i run CommandButton it works but not properly as it also doesn't let me move nothing even if i have it on stock
If registos.ComboBox1 = "SAÍDA" Then
If REFERENCIA <> registos.TextBox1 And CC = registos.Label11.Caption Then
MsgBox "Não existe em stock!"
GoTo fim
Else: GoTo salto_1
End If
End If
End here the condition
salto_1:
If REFERENCIA = registos.TextBox1 And registos.ComboBox1 = "SAÍDA" Then
Worksheets("registos").Select
ActiveSheet.ListObjects("Tabela1").Range.AutoFilter Field:=8, Criteria1:= _
registos.TextBox1.Text 'Filtrar referência
ActiveSheet.ListObjects("Tabela1").Range.AutoFilter Field:=6, Criteria1:= _
registos.Label11.Caption 'Filtrar CC
ActiveSheet.ListObjects("Tabela1").Range.AutoFilter Field:=12, Criteria1:= _
"ENTRADA" 'Filtrar Entrada
'Somar quantidades de peças de Entrada
xty100 = ThisWorkbook.Worksheets("calculos").Range("A1")
ActiveSheet.ListObjects("Tabela1").Range.AutoFilter Field:=12, Criteria1:= _
"SAÍDA" 'Filtrar Saída
'Somar quantidade de peças de saída
xty101 = ThisWorkbook.Worksheets("calculos").Range("A1")
sumfinal = xty100 - xty101 'Calculo do Stock
ThisWorkbook.Worksheets("calculos").Range("A20") = Format(registos.TextBox4, "#")
xtybx4 = ThisWorkbook.Worksheets("calculos").Range("A20")
If xtybx4 <= sumfinal And xtybx4 > 0 Then
GoTo salto_2
Else
MsgBox "Não foi possível concluir o movimento!Stock " & sumfinal & ""
GoTo fim
End If
End If
Next i
salto_2:
If registos.TextBox3 = "" And registos.TextBox1 <> "" And registos.TextBox2 <> "" And registos.TextBox4 <> "" And registos.ComboBox5 <> "" Then
Application.ThisWorkbook.Worksheets("Registos").Cells(last + 1, 1) = Now() 'data
Application.ThisWorkbook.Worksheets("Registos").Cells(last + 1, 5) = registos.Label20.Caption 'ano fiscal
Application.ThisWorkbook.Worksheets("Registos").Cells(last + 1, 6) = registos.Label11.Caption 'cc
Application.ThisWorkbook.Worksheets("Registos").Cells(last + 1, 7) = a1logiin.TextBox1 'operario
Application.ThisWorkbook.Worksheets("Registos").Cells(last + 1, 8) = registos.TextBox1 'referencia formata
Application.ThisWorkbook.Worksheets("Registos").Cells(last + 1, 9) = registos.TextBox2 'ordem
Application.ThisWorkbook.Worksheets("Registos").Cells(last + 1, 11) = Format(registos.TextBox4, "#") 'quantidade
Application.ThisWorkbook.Worksheets("Registos").Cells(last + 1, 12) = registos.ComboBox1 'ENTRADA/SAIDA
Application.ThisWorkbook.Worksheets("Registos").Cells(last + 1, 13) = registos.ComboBox5 ' ESTADO
Application.ThisWorkbook.Worksheets("Registos").Cells(last + 1, 14) = Format(registos.ComboBox3, "#") 'CODIGO DEFEITO
Application.ThisWorkbook.Worksheets("Registos").Cells(last + 1, 16) = registos.ComboBox6 'ORIGEM DEFEITO
Application.ThisWorkbook.Worksheets("Registos").Cells(last + 1, 17) = registos.TextBox5 'OBSERVAÇÕES
MsgBox "Dados introduzidos com sucesso!"
'GoTo fim
GoTo fim2
End If
MsgBox "Insira todos os dados"
GoTo fim
fim2: If registos.ComboBox1 <> "SAÍDA" Then
GoTo fim
End If
If registos.ComboBox1 = "SAÍDA" And registos.TextBox3 = "" And registos.TextBox1 <> "" And registos.TextBox2 <> "" And registos.TextBox4 <> "" And registos.ComboBox5 <> "" And registos.ComboBox2 <> "" Then
Application.ThisWorkbook.Worksheets("Registos").Cells(last + 2, 1) = Now() 'data
Application.ThisWorkbook.Worksheets("Registos").Cells(last + 2, 5) = registos.Label20.Caption 'ThisWorkbook.Worksheets("anofiscal").Range("A1") 'ano fiscal
Application.ThisWorkbook.Worksheets("Registos").Cells(last + 2, 6) = registos.ComboBox2 'cc
Application.ThisWorkbook.Worksheets("Registos").Cells(last + 2, 7) = a1logiin.TextBox1 'operario
Application.ThisWorkbook.Worksheets("Registos").Cells(last + 2, 8) = registos.TextBox1 'referencia formata
Application.ThisWorkbook.Worksheets("Registos").Cells(last + 2, 9) = registos.TextBox2 'ordem
Application.ThisWorkbook.Worksheets("Registos").Cells(last + 2, 11) = Format(registos.TextBox4, "#") 'quantidade
Application.ThisWorkbook.Worksheets("Registos").Cells(last + 2, 12) = "ENTRADA" 'Define a saída de um como a entrada de outro
Application.ThisWorkbook.Worksheets("Registos").Cells(last + 2, 13) = registos.ComboBox5 ' ESTADO
Application.ThisWorkbook.Worksheets("Registos").Cells(last + 2, 14) = Format(registos.ComboBox3, "#") 'CODIGO DEFEITO
Application.ThisWorkbook.Worksheets("Registos").Cells(last + 2, 16) = registos.ComboBox6 'ORIGEM DEFEITO
Application.ThisWorkbook.Worksheets("Registos").Cells(last + 2, 17) = registos.TextBox5 'OBSERVAÇÕES
Application.ThisWorkbook.Worksheets("Registos").Cells(last + 2, 18) = registos.ComboBox7 'detalhe produção
GoTo fim
End If
If contador = 1 Then
MsgBox "Foi efetuado o registo de " & contador & " referência!", vbInformation
' Else
' MsgBox "Foi efetuado o registo de " & contador & " referências!", vbInformation
End If
contador = 0
GoTo fim:
fim:
End Sub

Excel VBA Screenupdating False Infinite Loop

I have a function that maps data from one sheet (where it has been copied) to another which is then used for further analysis. When I run the code with screen updating on it always works fine. When I turn screen updating off the code gets stuck in an infinite loop in the last part of the sub (highlighted in bold - it is the inner most loop of the final section of code). If you then debug the code and re-start it continues normally and finished the code. If left it will never end, but next time will work fine:
Sub simsMap()
Dim simsCol As String
Dim mapCol As String
range("A5:OP253").ClearContents
range("S1:OP1").ClearContents
range("S4:OP4").ClearContents
simsCol = range("A1")
For x = 2 To 250
If Worksheets("simsData").range(simsCol & x) <> "" Then range("A" & x + 3).Value = Worksheets("simsData").range(simsCol & x)
Next x
simsCol = range("B1")
For x = 2 To 250
If range("A" & x + 3) <> "" Then
If Worksheets("simsData").range(simsCol & x) = range("B2") Or Worksheets("simsData").range(simsCol & x) = range("B3") Then
range(simsCol & x + 3) = "Y"
Else
range(simsCol & x + 3) = "N"
End If
End If
Next x
Dim simsArray As Variant
Dim mapArray As Variant
simsArray = Array("C1", "D1", "G1")
mapArray = Array("C", "D", "G")
For y = 0 To UBound(simsArray)
simsCol = range(simsArray(y))
mapCol = mapArray(y)
For x = 2 To 250
If range("A" & x + 3) <> "" Then
If Worksheets("simsData").range(simsCol & x) = "Y" Then
range(mapCol & x + 3) = "Y"
Else
range(mapCol & x + 3) = "N"
End If
End If
Next x
Next y
simsArray = Array("E1", "F1", "H1", "I1", "J1", "K1", "L1", "M1", "N1", "O1", "P1", "Q1")
mapArray = Array("E", "F", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q")
For y = 0 To UBound(simsArray)
simsCol = range(simsArray(y))
mapCol = mapArray(y)
For x = 2 To 250
If range("A" & x + 3) <> "" Then
range(mapCol & x + 3) = Worksheets("simsData").range(simsCol & x)
End If
Next x
Next y
Dim realColumn As String
Dim valueColumn As String
Dim columnNumber As Long
Dim realCell As String
Dim valueCell As String
Dim subjectJump As Integer
realColumn = "S"
subjectJump = 8 - Worksheets("menu").range("F17")
For y = 1 To 48
If Worksheets("menu").range("F19") = "Y" Then
valueColumn = range(realColumn & 1)
realCell = realColumn & 1
realColumn = Split(Cells(1, range(realCell).Column + 1).Address, "$")(1)
range(realColumn & 1) = valueColumn
For x = 1 To Worksheets("menu").range("F17")
valueCell = range(realColumn & 1) & 1
If range(realColumn & 1) <> "" Then valueColumn = Split(Cells(1, range(valueCell).Column + 1).Address, "$")(1)
realCell = realColumn & 1
realColumn = Split(Cells(1, range(realCell).Column + 1).Address, "$")(1)
range(realColumn & 1) = valueColumn
Next x
Else
If range(realColumn & 1) = "" Then
realCell = realColumn & 1
realColumn = Split(Cells(1, range(realCell).Column + 8).Address, "$")(1)
Else
For x = 1 To Worksheets("menu").range("F17")
valueCell = range(realColumn & 1) & 1
valueColumn = Split(Cells(1, range(valueCell).Column + 1).Address, "$")(1)
realCell = realColumn & 1
realColumn = Split(Cells(1, range(realCell).Column + 1).Address, "$")(1)
range(realColumn & 1) = valueColumn
Next x
realCell = realColumn & 1
realColumn = Split(Cells(1, range(realCell).Column + subjectJump).Address, "$")(1)
End If
End If
Next y
realColumn = "S"
For y = 1 To 384
simsCol = range(realColumn & 1)
mapCol = realColumn
If range(mapCol & 1) <> "" Then
If range("A" & 4) <> "" Then
range(mapCol & 4) = Worksheets("simsData").range(simsCol & 1)
End If
End If
realColumn = Split(Cells(1, range(realColumn & 1).Column + 1).Address, "$")(1)
Next y
realColumn = "S"
For y = 1 To 384
simsCol = range(realColumn & 1)
mapCol = realColumn
If range(mapCol & 1) <> "" Then
For x = 2 To 250
**If range("A" & x + 3) <> "" Then
range(mapCol & x + 3) = Left(Worksheets("simsData").range(simsCol & x), 1)
End If**
Next x
End If
realColumn = Split(Cells(1, range(realColumn & 1).Column + 1).Address, "$")(1)
If y = 384 Then loopCheck = False
Next y
For x = 5 To 253
If range("A" & x) <> "" Then studentNumber = x
Next x
End Sub

Read Each line not reading through entire file

I am using Vb to take a .txt file, parse it, and check for errors. My code works just fine, however, the code does not go through the entire file. It stops, on average, 20 lines shy of the EOF.
I am using the following
For Each lines As String In System.IO.File.ReadLines(myFile)
from here I parse the line and see if it needs any fixes.
Is there something that I'm missing or something that just cant be avoided.
The files that I'm reading in are about 150,000 KB to 230,000 KB and over 2 million lines.
As requested, the following is my code. Warning, I just started using Vb...
Module Module1
Sub Main()
Dim root As String = "C:\Users\mschramm\Documents\Agco\WindSensor\Data\filestobecleaned\"
Dim datafile As String = root & "ES.txt"
Dim outfile As String = root & "temptry.txt"
Dim output As System.IO.StreamWriter = My.Computer.FileSystem.OpenTextFileWriter(outfile, False)
Dim k As UInteger = 0
Dim fixes As UInteger = 0
Dim time As ULong = 0
Dim count As UInteger = 0
Dim n As UInteger = 0
Dim LineCount As UInteger = 0
Dim TimeStep As ULong = 100
Dim Solar As UInteger = 0
For Each lines As String In System.IO.File.ReadLines(datafile)
LineCount = LineCount + 1
'Console.WriteLine(LineCount)
Dim parsedline As String() = Split(lines, ",")
If IsNumeric(parsedline(0)) = True And UBound(parsedline) = 8 Then
'TimeStep = parsedline(0) - time
Solar = parsedline(1)
time = parsedline(0)
output.WriteLine(lines & " Good Line")
count = count + 1
Else
Dim j As UInteger = 0
Dim ETX As Integer = 0
Dim STX As Integer = 0
Dim datacheck As Boolean = False
Dim fixedline As String = ""
Dim newtime As ULong = 0
For j = 0 To UBound(parsedline)
Dim a As Char = parsedline(j)
If a = (Chr(3)) Then ETX = j
If a = (Chr(2)) Then STX = j
Next
j = 0
If (STX < ETX) And (ETX - STX) = 6 And STX >= 2 Then
If Len(parsedline(STX + 1)) = 8 And Len(parsedline(STX + 2)) = 8 And Len(parsedline(STX + 3)) = 8 Then
Dim g = Len(parsedline(STX - 2))
While (j < g) And datacheck = False
If IsNumeric(parsedline(STX - 2)) Then
If parsedline(STX - 2) - time < 10000 And parsedline(STX - 2) - time > 0 Then
newtime = Right(parsedline(STX - 2), Len(parsedline(STX - 2)))
Solar = parsedline(STX - 1)
'TimeStep = newtime - time
fixedline = newtime & "," & parsedline(STX - 1) & "," & parsedline(STX) & "," & parsedline(STX + 1) & "," & parsedline(STX + 2) & "," & parsedline(STX + 3) & "," & parsedline(STX + 4) & "," & parsedline(STX + 5) & "," & parsedline(STX + 6) & " Fixed Line"
datacheck = True
Else
j = j + 1
parsedline(STX - 2) = Right(parsedline(STX - 2), Len(parsedline(STX - 2)) - 1).ToString
End If
Else
j = j + 1
parsedline(STX - 2) = Right(parsedline(STX - 2), Len(parsedline(STX - 2)) - 1).ToString
End If
End While
End If
End If
If (STX < ETX) And (ETX - STX) = 6 And STX = 0 Then
If Len(parsedline(1)) = 8 And Len(parsedline(2)) = 8 And Len(parsedline(3)) = 8 And Len(parsedline(4)) = 1 And Len(parsedline(5)) = 2 And Len(parsedline(6)) = 3 Then
newtime = time + TimeStep
fixedline = newtime & "," & Solar & "," & parsedline(STX) & "," & parsedline(STX + 1) & "," & parsedline(STX + 2) & "," & parsedline(STX + 3) & "," & parsedline(STX + 4) & "," & parsedline(STX + 5) & "," & parsedline(STX + 6) & " Fixed Line Gave Time and Solar"
datacheck = True
End If
End If
If newtime < time And newtime > 1000 Then
Dim badtime As ULong = newtime
Dim firstdig As ULong = time
Dim loopcount As UInteger = 0
While firstdig > 9
firstdig = firstdig / 10
loopcount = loopcount + 1
End While
firstdig = firstdig * (10 ^ loopcount)
If (firstdig + badtime) > time Then
newtime = firstdig + badtime
If (newtime - (10 ^ loopcount)) > time Then
newtime = newtime - (10 ^ loopcount)
End If
End If
End If
If datacheck = True Then
k = k + 1
If (newtime > 500) Then
output.WriteLine(fixedline)
'count = count + 1
time = newtime
End If
End If
If datacheck = False Then
n = n + 1
If STX >= 0 And ETX > 0 And ETX - STX < 9 Then
Console.WriteLine(LineCount)
'n = n + 1
End If
End If
End If
Next
Console.WriteLine(count & " Good lines")
Console.WriteLine(k & " Lines Corrected")
Console.WriteLine(LineCount & " Total Lines")
Console.WriteLine(n & " Lines were thrown out")
Console.WriteLine(n / LineCount * 100 & "% thrown out")
End Sub
End Module
and here is a sample of the data
Time: 16:52:18.0
Date: 11/6/2014
Time,Sensor1,U,V,W
544161,219,Q,-001.341,+000.947,+000.140,M,00,17
544284,218,Q,-001.207,+001.074,+000.225,M,00,1C
544361,220,Q,-000.935,+000.898,+000.187,M,00,17
544460,220,Q,-001.299,+001.151,-000.009,M,00,17
This is what the last 10 lines look like
Q,+001.681,-003.510,-0356154697,236,Q,+000.826,-002.744,-000.559,M,00,19
Q,+000.474,-002.789,-0356155062,234,Q,+000.400,-002.975,+000.438,M,00,1D
Q,+000.813,-002.934,-0356155297,236,Q,+000.146,-002.129,-000.235,M,00,16
Q,+000.494,-002.234,+0356155497,236,Q,+000.681,-001.996,-000.248,M,00,1F
Q,+000.800,-001.999,-0356155697,236,Q,+001.181,-002.883,-000.795,M,00,1A
356156060,233,Q,+000.400,-002.106,+000.251,M,00,18
356156296,235,Q,+000.888,-001.026,+000.442,M,00,10
356156495,236,Q,+000.570,-001.694,+000.589,M,00,13
356156695,236,Q,+001.495,-002.177,-000.035,M,00,15
356157060,234,Q,+000.770,-003.484,-000.161,M,00,14
for this file, the code makes it to the 6th to last line.
Thanks to mafafu for pointing out the solution.
I never closed the file, so the addition of output.Close() fixed everything.
Once again, thank you mafafu.

Edit a CSV in vb

i have a CSV export from a earlier version of a software and would like to import it into the new version but however I would only like a couple of columns from the CSV and for it to display when I click button1 in a windows form in col order. Can this be done and how.
please message if don't understand and will go into more details.
i have this so far but this just displays the CSV
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim dx As Long = 0
Dim dRow As Long = 0
Dim dColumn As Long = 0
Dim dTotalRows As Long = 0
Dim dTotalColumns As Long = 0
Dim dFileName As String = ""
Dim dReadLine As String = ""
Dim dChar As String = ""
Dim dArray(1, 1) As String
Dim dStart As Long = 1
Dim dEnd As Long = 1
Dim dLen As Long = 0
Dim dLineLength As Long = 0
Dim dQuoteCounter As Long = 0
Dim dAdd2ItemList As String = ""
dFileName = "E:\test docs/test.csv"
FileOpen(1, dFileName, OpenMode.Input, OpenAccess.Default, OpenShare.Default, -1)
Do While Not EOF(1)
dReadLine = LineInput(1)
dRow = dRow + 1
dTotalColumns = dColumn
dColumn = 0
dLineLength = Len(dReadLine)
For dx = 1 To dLineLength
dChar = Mid(dReadLine, dx, 1)
If dChar = Chr(34) Then
dQuoteCounter = dQuoteCounter + 1
If dQuoteCounter = 2 * (Int(dQuoteCounter / 2)) Then
dChar = Mid(dReadLine, dx, 2)
If dChar = Chr(34) + "," Then
dColumn = dColumn + 1
dQuoteCounter = -1
If Chr(34) = dChar + "," Then
dQuoteCounter = -1
dColumn = dColumn + 1
End If
End If
End If
End If
If dQuoteCounter = 0 And dChar = "," Then
dColumn = dColumn + 1
End If
If dx = dLineLength Then
dColumn = dColumn + 1
End If
If dQuoteCounter = -1 And dChar = "," Then dQuoteCounter = 0
Next (dx)
Loop
dTotalRows = dRow
ReDim dArray(dTotalRows, dTotalColumns)
FileClose(1)
dRow = 0
FileOpen(1, dFileName, OpenMode.Input, OpenAccess.Default, OpenShare.Default, -1)
Do While Not EOF(1)
dAdd2ItemList = ""
dRow = dRow + 1
dStart = 1
dEnd = 1
dLen = 0
dColumn = 0
dReadLine = LineInput(1)
dLineLength = Len(dReadLine)
For dx = 1 To dLineLength
dChar = Mid(dReadLine, dx, 1)
If dChar = Chr(34) Then
dQuoteCounter = dQuoteCounter + 1
If dQuoteCounter = 1 Then dStart = dx + 1
If dQuoteCounter = 2 * (Int(dQuoteCounter / 2)) Then
dChar = Mid(dReadLine, dx, 2)
If dChar = Chr(34) + "," Then
dEnd = dx
dLen = dEnd - dStart
dColumn = dColumn + 1
dArray(dRow, dColumn) = Mid(dReadLine, dStart, dLen)
dAdd2ItemList = dAdd2ItemList + dArray(dRow, dColumn) + "|"
dQuoteCounter = -1
dStart = dx + 2
End If
End If
End If
If dQuoteCounter = 0 And dChar = "," Then
dEnd = dx
dLen = (dEnd - dStart)
dColumn = dColumn + 1
If dLen < 1 Then
dArray(dRow, dColumn) = ""
dAdd2ItemList = dAdd2ItemList + dArray(dRow, dColumn) + "|"
Else
dArray(dRow, dColumn) = Mid(dReadLine, dStart, dLen)
dAdd2ItemList = dAdd2ItemList + dArray(dRow, dColumn) + "|"
End If
dStart = dx + 1
End If
If dx = dLineLength Then
dEnd = dx
dLen = (dEnd - dStart) + 1
dColumn = dColumn + 1
If dLen < 1 Then
dArray(dRow, dColumn) = ""
dAdd2ItemList = dAdd2ItemList + dArray(dRow, dColumn)
Else
dArray(dRow, dColumn) = Mid(dReadLine, dStart, dLen)
dAdd2ItemList = dAdd2ItemList + dArray(dRow, dColumn)
End If
dStart = dx + 1
End If
If dQuoteCounter = -1 And dChar = "," Then dQuoteCounter = 0
Next (dx)
ListBox1.Items.Add(dAdd2ItemList)
REM dRow = dRow + 1
Loop
End Sub
End Class