Edit a CSV in vb - vb.net

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

Related

How to split a string every fourth delimiter?

Is it possible to split a string every fourth delimiter?
We receive a text file that has one string containing several days data.
I would like to split once using the '|' as the delimiter, but different days data is joined by a comma.
Option Explicit
Sub Split4thdelim()
Dim strOriginal as string
Dim originalArry() as string
Dim X as long
strOriginal = "01/01/2020|user1,89|user2,90|user3,99,02/01/2020|user1,80|user2,85|user3,97,03/01/2020|user1,88|user2,96|user3,99"
originalArry = split(strOriginal,"|")
For X = originalArry(originalArry(Lbound)) to originalArry(originalArry(Ubound))
Debug.Print originalArry(X)
Next
End Sub
I would like to split it like this:
01/01/2020
user1,89
user2,90
user3,99
02/01/2020
user1,80
user2,85
user3,97
03/01/2020
user1,88
user2,96
user3,99
Sub Split4thdelim()
Dim strOriginal As String
Dim originalArry() As String
Dim X As Long, n As Long
strOriginal = "01/01/2020|user1,89|user2,90|user3,99,02/01/2020|user1," & _
"80|user2,85|user3,97,03/01/2020|user1,88|user2,96|user3,99"
'replace every fourth "," with chr(0)
For X = 1 To Len(strOriginal)
If Mid(strOriginal, X, 1) = "," Then
n = n + 1
If n Mod 4 = 0 Then Mid(strOriginal, X, 1) = Chr(0)
End If
Next X
originalArry = Split(strOriginal, Chr(0))
For X = LBound(originalArry) To UBound(originalArry)
Debug.Print Join(Split(originalArry(X), "|"), vbLf)
Next
End Sub
Do it manually:
Sub Split4thdelim()
Dim strOriginal As String
Dim Token As New Collection
Dim strToken As String
Dim X As Long
strOriginal = "01/01/2020|user1,89|user2,90|user3,99,02/01/2020|user1,80|user2,85|user3,97,03/01/2020|user1,88|user2,96|user3,99"
Dim Pos As Long
Dim strTemp As String
Pos = InStr(1, strOriginal, "user")
Token.Add Mid(strOriginal, 1, Pos - 2)
strOriginal = Right(strOriginal, Len(strOriginal) - Pos + 1)
While Pos > 0
Pos = InStr(2, strOriginal, "user")
Token.Add Mid(strOriginal, 1, Pos - 2)
strOriginal = Right(strOriginal, Len(strOriginal) - Pos + 1)
Pos = InStr(2, strOriginal, "user")
Token.Add Mid(strOriginal, 1, Pos - 2)
strOriginal = Right(strOriginal, Len(strOriginal) - Pos + 1)
Pos = InStr(2, strOriginal, "user")
If Pos = 0 Then 'no more user, so this is the last set
Token.Add Right(strOriginal, Len(strOriginal) - Pos)
Else
strTemp = Left(strOriginal, Pos - 2)
strOriginal = Right(strOriginal, Len(strOriginal) - Pos + 1) 'save new strOriginal
Pos = InStr(1, strOriginal, ",")
strToken = Mid(strTemp, 1, Pos)
strTemp = Right(strTemp, Len(strTemp) - Pos)
Pos = InStr(1, strTemp, ",")
If Pos = 0 Then
Pos = InStr(1, strTemp, "|")
End If
strToken = strToken + Mid(strTemp, 1, Pos - 1)
Token.Add strToken
Token.Add Right(strTemp, Len(strTemp) - Pos)
End If
Wend
Dim strItem As Variant
For Each strItem In Token
Debug.Print strItem
Next strItem
End Sub
Try,
Sub Split4thdelim()
Dim strOriginal As String
Dim originalArry() As String
Dim X As Long
Dim vSplit, s As String, s2 As String
Dim vR()
Dim n As Long
strOriginal = "01/01/2020|user1,89|user2,90|user3,99,02/01/2020|user1,80|user2,85|user3,97,03/01/2020|user1,88|user2,96|user3,99"
originalArry = Split(strOriginal, "|")
For X = LBound(originalArry) To UBound(originalArry)
'Debug.Print originalArry(X)
s = originalArry(X)
If InStr(s, "/") And InStr(s, ",") Then
n = n + 2
vSplit = Split(s, ",")
s2 = vSplit(UBound(vSplit))
ReDim Preserve vR(1 To n)
vR(n - 1) = Replace(s, "," & s2, "")
vR(n) = s2
Else
n = n + 1
ReDim Preserve vR(1 To n)
vR(n) = s
End If
Next
Range("a1").Resize(n) = WorksheetFunction.Transpose(vR)
End Sub
or ...
Option Explicit
Sub Split4thdelim()
Dim strOriginal As String
Dim vCounter As Long
Dim vNewElement As String
Dim vCommaCount As Long
strOriginal = "01/01/2020|user1,89|user2,90|user3,99,02/01/2020|user1,80|user2,85|user3,97,03/01/2020|user1,88|user2,96|user3,99"
vNewElement = ""
vCommaCount = 0
For vCounter = 1 To Len(strOriginal)
If Mid$(strOriginal, vCounter, 1) <> "|" And Mid$(strOriginal, vCounter, 1) <> "," Then
vNewElement = vNewElement & Mid$(strOriginal, vCounter, 1)
Else
If Mid$(strOriginal, vCounter, 1) = "|" Then
Debug.Print vNewElement
vNewElement = ""
vCommaCount = 0
ElseIf Mid$(strOriginal, vCounter, 1) = "," Then
vCommaCount = vCommaCount + 1
If vCommaCount = 2 Then
Debug.Print vNewElement
vNewElement = ""
vCommaCount = 0
Else
vNewElement = vNewElement & Mid$(strOriginal, vCounter, 1)
End If
End If
End If
Next
Debug.Print vNewElement
End Sub

Make a loop asynchronous in vb.net [duplicate]

This question already has answers here:
How can I run code in a background thread and still access the UI?
(2 answers)
Use of Application.DoEvents()
(10 answers)
Closed 3 years ago.
I'm trying to create an app to copy massive amounts of files in vb.net and I'm trying to make it asynchronous, so I can pause, stop and create a counter over the progress bars, and I have researched a little bit on how to do it, but I don't quite understand how to modify my code.
This is most of the code I'm working on:
Private Sub btnMoverImagenes_Click(sender As Object, e As EventArgs) Handles btnMoverImagenes.Click
Dim carpetaProducto As String
Dim agno As String
Dim mes As String
Dim dia As String
Dim secuencia As String
Dim rutaOrigen As String
Dim RutaDestinoOp As String
Dim RutaDestinoHis As String
Dim archivo As String
Dim DestinoArchivoOrigen As String
Dim DestinoArchivoHistorico As String
Dim idregistro As Integer
Dim dsregistros As Integer
Dim imagenMulti As String
Dim nuevaRutaImagenMulti As String
Dim nombreImagenOrigen As String
Dim contadorIMGCopiadas As Integer = 0
Dim contadorRegistros As Integer = 0
Dim contadorArchivosSinImagen As Integer = 0
Dim h1 As Date
Dim h2 As Date
Dim tiempoCarga As String
Dim fechaCarga As String
h1 = System.DateTime.Now
ProgressBar1.Minimum = 0
ProgressBar1.Maximum = 1000
ProgressBar1.Value = 0
ProgressBar2.Minimum = 0
ProgressBar2.Maximum = 1000
ProgressBar2.Value = 0
fechaDesde = dtpDesde.ToString
fechaHasta = dtpHasta.ToString
dtpDesde.Format = DateTimePickerFormat.Custom
dtpDesde.CustomFormat = "yyyyMMdd"
fechaDesde = dtpDesde.Text
dtpHasta.Format = DateTimePickerFormat.Custom
dtpHasta.CustomFormat = "yyyyMMdd"
fechaHasta = dtpHasta.Text
dsImagenes = EjecutarXML(conexionDat, fechaDesde, fechaHasta)
Try
If dsImagenes.Tables(dsImagenes.Tables.Count - 1).Rows(0).Item(3) > 0 Then
dsregistros = dsImagenes.Tables(0).Rows.Count
ProgressBar1.Minimum = 0
ProgressBar1.Maximum = dsregistros
ProgressBar1.Value = 0
ProgressBar2.Minimum = 0
ProgressBar2.Maximum = dsregistros
ProgressBar2.Value = 0
Label5.Text = ProgressBar1.Value.ToString + " de " + ProgressBar1.Maximum.ToString
Label6.Text = ProgressBar2.Value.ToString + " de " + ProgressBar2.Maximum.ToString
Log("******* SE INICIA CARGA *******")
If dsImagenes.Tables(0).Rows.Count > 0 Then
For Each row As DataRow In dsImagenes.Tables(0).Rows
archivo = row.Item(0).ToString()
carpetaProducto = row.Item(3).ToString()
rutaOrigen = row.Item(1).ToString()
idregistro = row.Item(2).ToString()
agno = row.Item(4).ToString()
mes = row.Item(5).ToString()
dia = row.Item(6).ToString()
secuencia = row.Item(7).ToString()
fechaCarga = row.Item(9).ToString()
imagenMulti = row.Item(8).ToString()
nombreImagenOrigen = row.Item(10).ToString()
nuevaRutaImagenMulti = carpetaProducto + "\" + agno + "\" + mes + "\" + dia + "\" + secuencia + "\" + nombreImagenOrigen
RutaDestinoOp = RutaDestinoNasOp + "\" + carpetaProducto + "\" + agno + "\" + mes + "\" + dia + "\" + secuencia
RutaDestinoHis = RutaDestinoNasHis + "\" + carpetaProducto + "\" + agno + "\" + mes + "\" + dia + "\" + secuenciavo
DestinoArchivoOrigen = RutaDestinoNasOp + "\" + carpetaProducto + "\" + agno + "\" + mes + "\" + dia + "\" + secuencia + "\" + nombreImagenOrigen
DestinoArchivoHistorico = RutaDestinoNasHis + "\" + carpetaProducto + "\" + agno + "\" + mes + "\" + dia + "\" + secuencia + "\" + archivo
Log("------------------------------------------------------------------------------")
Log("******* Inicio copia archivo IDREGISTRO: " + idregistro.ToString + " *******")
If ValidaExisteImagenOrigen(rutaOrigen) = True Then
If ValidarRutaOrigen(rutaOrigen) = True Then
If CargaNasHistorico(rutaOrigen, RutaDestinoHis, DestinoArchivoHistorico, conexionDat, idregistro) = True Then
If CargaNasOperativo(rutaOrigen, RutaDestinoOp, DestinoArchivoOrigen, conexionDat, idregistro, nuevaRutaImagenMulti) = True Then
contadorIMGCopiadas = contadorIMGCopiadas + 1
If ProgressBar2.Value < ProgressBar2.Maximum Then
ProgressBar2.Value += 1
End If
End If
End If
Else
If ValidaImagenHistorico(DestinoArchivoHistorico) = True Then
If CargaNasHistorico(rutaOrigen, RutaDestinoHis, DestinoArchivoHistorico, conexionDat, idregistro) = True Then
contadorIMGCopiadas = contadorIMGCopiadas + 1
If ProgressBar2.Value < ProgressBar2.Maximum Then
ProgressBar2.Value += 1
End If
Else
Log("******* TODO OK, Registro id: " + idregistro.ToString + ". no se realiza copia de imagen *******")
contadorIMGCopiadas = contadorIMGCopiadas + 1
If ProgressBar2.Value < ProgressBar2.Maximum Then
ProgressBar2.Value += 1
End If
End If
End If
End If
Else
Log("******* ALERTA: Registro id: " + idregistro.ToString + " NO EXISTE *******")
contadorArchivosSinImagen = contadorArchivosSinImagen + 1
End If
contadorRegistros = contadorRegistros + 1
Log("******* Finaliza copia archivo IDREGISTRO: " + idregistro.ToString + " *******")
archivo = ""
carpetaProducto = ""
rutaOrigen = ""
idregistro = 0
agno = ""
mes = ""
dia = ""
secuencia = ""
If ProgressBar1.Value < ProgressBar1.Maximum Then
ProgressBar1.Value += 1
End If
Label5.Text = ProgressBar1.Value.ToString
Label6.Text = ProgressBar2.Value.ToString + " de " + ProgressBar2.Maximum.ToString
Next
h2 = System.DateTime.Now
tiempoCarga = (h2 - h1).ToString
MsgBox("Proceso Terminado, revisar log para ver el detalle del proceso completo")
Log("------------------------------------------------------------------------------")
Log("******* Copia Finalizada, IMAGENES COPIADAS: " + contadorIMGCopiadas.ToString + " DE " + dsregistros.ToString + " *******")
Log("******* REGISTROS SIN IMAGENES: " + contadorArchivosSinImagen.ToString + " *******")
Log("******* TIEMPO DE EJECUCION: " + tiempoCarga + " *******")
End If
Else
MsgBox("sin datos para la fecha seleccionada")
End If
Catch ex As Exception
MsgBox("ocurrio un error al buscar los datos")
End Try
End Sub
I'd really apreciate if someone could help me.

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

Run time error '9' VBA Subcript out of range

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

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.