Search string - illegal chars in path - vb.net

I have this search string.
Dim files As String() = IO.Directory.GetFiles("\\192.168.0.2\shares\be\" & functiicomune.numeclient & "\" & r & " " & codnumeric & "*" & "\" & "PROD\", "*" & codnumeric & "*" & "DECOMPOSITION" & "*" & ".pdf")
I get illegal characters in path and i dont know why. Can someone provide a hint?
The path on the network is:
\\192.168.0.2\shares\be\BERTHOUD\BA 390683 L\PROD\BA390683 L - PP. DECOMPOSITION 160630.pdf
The numeclient function code is:
Public Shared Function numeclient()
Dim codclient As String = Form1.TextBox4.Text.Substring(0, 2)
Dim r As String
Select Case codclient
Case "BA"
r = "BERTHOUD"
Case "CN"
r = "CARUELLE"
Case "TT"
r = "TECNOMA"
Case "PR"
r = "PRECICULTURE"
Case "KR"
r = "KREMLIN"
End Select
Return r
End Function
The r and codnumeric code is:
Dim rgx As New Regex("[^0-9]")
Dim codnumeric As String = rgx.Replace(TextBox4.Text, "")
Dim r As String = TextBox4.Text.Substring(0, 2)
The textbox4 contains string this form: BA390683 L

You could simplify first your code, and add parenthesis when calling the function :
Dim files As String() = IO.Directory.GetFiles("\\192.168.0.2\shares\be\" & functiicomune.numeclient() & "\" & r & " " & codnumeric & "*PROD\","*" & codnumeric & "*DECOMPOSITION*.pdf"
and second, check the values which compose the path or extension through your debugger :
functiicomune.numeclient
r
codnumeric

Related

VBA, 2nd last "/" using InstrRev

I have code that parses out the last word on a string.
ie. Stack/Over/Flow will give me "Flow".
But I want to get "Over/Flow".
This is what I got, but only able to get "Flow"
arr(counter - 2) = "'" & mid(Text, InStrRev(Text, "/") + 1) & "'"
I would use Split()
Sub lastTwo()
Dim str As String
str = "Stack/Over/Flow"
Dim splt() As String
splt = Split(str, "/")
If UBound(splt) > 0 Then
Debug.Print splt(UBound(splt) - 1) & "/" & splt(UBound(splt))
End If
End Sub
Here is a function that does it:
Function lastParts(str As String, delim As String, x As Long) As String
Dim splt() As String
splt = Split(str, "/")
If UBound(splt) + 1 >= x Then
Dim t As String
t = "=INDEX(INDEX({""" & Join(splt, """;""") & """},N(IF({1},ROW(" & UBound(splt) - x + 2 & ":" & UBound(splt) + 1 & "))),),)"
lastParts = Join(Application.Transpose(Application.Evaluate(t)), delim)
Else
lastParts = str
End If
End Function
It has three parts, the string, the delimiter and the number of returns.
It can be called using your code:
arr(counter-2) = lastParts(Text,"/",2)
or from the worksheet
=lastParts(A1,"/",2)
Initially misread the question. You can nest InStrRev() calls
arr(counter - 2) = "'" & mid(Text, InStrRev(Text, "/",InStrRev(Text, "/")-1)+1) & "'"

Excel VBA CountIF To Search For String Array

How can I use the COUNTIF() function to count only certain text strings that exist in the range?
I tried to use the below, but I get an error of
Syntax error
This is the syntax I attempted
Dim worksheetmaster As String = "Master"
Dim worksheettocheck As String = "New"
Dim softcount As Int, i As Long, hardcount As Int
softcount = Evaluate("=COUNTIF(Range('" & worksheettocheck & "'!A:A'" & worksheetmaster & "'!A" & i & ")"Soft")")
hardcount = Evaluate("=COUNTIF(Range('" & worksheettocheck & "'!A:A'" & worksheetmaster & "'!A" & i & ")"Hard")")
EDIT
I tried to use this syntax without the Range and am still getting the error
hardcount = Evaluate("=COUNTIF('" & worksheettocheck & "'!A:A'" & worksheetmaster & "'!A" & i & ")"Hard"")
To match in column A with Hard in column B, this is how it should be:
hardcount = Application.Evaluate("COUNTIFS('" & worksheettocheck & "'!A:A,'" & worksheetmaster & "'!A" & i & ",'" & worksheettocheck & "'!B:B, ""Hard"")")
softcount = Application.Evaluate("COUNTIFS('" & worksheettocheck & "'!A:A,'" & worksheetmaster & "'!A" & i & ",'" & worksheettocheck & "'!B:B, ""Soft"")")
Your first syntax errors are here:
Dim worksheetmaster As String = "Master"
Dim worksheettocheck As String = "New"
You can't do that. Instead, you would need to use:
Dim worksheetmaster As String
Dim worksheettocheck As String
worksheetmaster = "Master"
worksheettocheck = "New"
Even better would be to assign them to point diectly at the worksheets, but let's work with your code as much as possible instead of totally rewriting it.
For the countif, you cannot join ranges that way. You haven't even assigned a value to i, but assuming i = 1, your code:
softcount = Evaluate("=COUNTIF(Range('" & worksheettocheck & "'!A:A'" & worksheetmaster & "'!A" & i & ")"Soft")")
evaluates to total nonsense in VBA:
softcount = Evaluate(=COUNTIF(Range('New'!A:A'Master'!A1)Soft))
Now, from what I can tell, what you are trying to do is to count how many times the value in cell Master!A & i appears in the range New!A:A, depending whether Master!A & i="Soft" or Master!A & i="Hard". So let's see if we can find code that will do that.
For data we enter "Soft" into Master!A1 and "Hard" into Master!A2. Then we enter random "Soft" or "Hard" into various cells in the column New!A:A.
Now your code looks like this:
Dim worksheetmaster As String
Dim worksheettocheck As String
Dim softcount As Long, i As Long, hardcount As Long
worksheetmaster = "Master"
worksheettocheck = "New"
i = 1
softcount = Application.CountIf(Sheets(worksheettocheck).Range("A:A"), Sheets(worksheetmaster).Range("A" & i).Value)
i = 2
hardcount = Application.CountIf(Sheets(worksheettocheck).Range("A:A"), Sheets(worksheetmaster).Range("A" & i).Value)
This is inefficient and limited, but it retains as much of your original code as possible, and it works.
Edited to add: if "Hard" is in Master!B & i instead of A & i then the code becomes:
i = 1
softcount = Application.CountIf(Sheets(worksheettocheck).Range("A:A"), Sheets(worksheetmaster).Range("A" & i).Value)
hardcount = Application.CountIf(Sheets(worksheettocheck).Range("A:A"), Sheets(worksheetmaster).Range("B" & i).Value)

Illegal chars in path - Directory.GetFiles error [duplicate]

I have this search string.
Dim files As String() = IO.Directory.GetFiles("\\192.168.0.2\shares\be\" & functiicomune.numeclient & "\" & r & " " & codnumeric & "*" & "\" & "PROD\", "*" & codnumeric & "*" & "DECOMPOSITION" & "*" & ".pdf")
I get illegal characters in path and i dont know why. Can someone provide a hint?
The path on the network is:
\\192.168.0.2\shares\be\BERTHOUD\BA 390683 L\PROD\BA390683 L - PP. DECOMPOSITION 160630.pdf
The numeclient function code is:
Public Shared Function numeclient()
Dim codclient As String = Form1.TextBox4.Text.Substring(0, 2)
Dim r As String
Select Case codclient
Case "BA"
r = "BERTHOUD"
Case "CN"
r = "CARUELLE"
Case "TT"
r = "TECNOMA"
Case "PR"
r = "PRECICULTURE"
Case "KR"
r = "KREMLIN"
End Select
Return r
End Function
The r and codnumeric code is:
Dim rgx As New Regex("[^0-9]")
Dim codnumeric As String = rgx.Replace(TextBox4.Text, "")
Dim r As String = TextBox4.Text.Substring(0, 2)
The textbox4 contains string this form: BA390683 L
You could simplify first your code, and add parenthesis when calling the function :
Dim files As String() = IO.Directory.GetFiles("\\192.168.0.2\shares\be\" & functiicomune.numeclient() & "\" & r & " " & codnumeric & "*PROD\","*" & codnumeric & "*DECOMPOSITION*.pdf"
and second, check the values which compose the path or extension through your debugger :
functiicomune.numeclient
r
codnumeric

ObjFile.Name not comapring with String in VBA Excel

i have a variable String named fileName and another variable named finalDoc
i am trying to compare both but it is not working. the program does not go in the if condition
Dim fileName As String
fileName = objFile.Name
finalDoc = cellValue & "-" & Range(sheetNo).Value & ".pdf"
//The fileName and finalDoc are getting values as expected
If finalDoc = fileName Then
'MsgBox finalDoc & " " & fileName
End If
the if condition is not working. it only works if i hardcode the value in fileName like
fileName ="abc.pdf"
any suggestions, i think this is a type issue String etc. Any help would be appreciated.
Using VBA of Excel 2013
You can't compare a string using "=". You must use the StrComp command that will return an integer depending on the outcome.
More information can be found Here
But here is an example that returns a match when comparing ABC.pdf and abc.pdf:
Sub test1()
Dim fileName1 As String
Dim fileName2 As String
Dim TestComp As Integer
fileName1 = "abc.pdf"
fileName2 = "ABC.pdf"
TestComp = StrComp(fileName1, fileName2, vbTextCompare)
If TestComp = 0 Then
MsgBox ("Match!")
Else
MsgBox (fileName1 & " and " & fileName2 & " are No Match. Outcome is " & TestComp)
End If
End Sub
All you now need to do is adjust this code to suit your needs.
Basically:
Dim fileName As String
Dim TestComp As Integer
fileName = objFile.Name
finalDoc = cellValue & "-" & Range(sheetNo).Value & ".pdf"
TestComp = StrComp(fileName,finalDoc,vbTextCompare)
If TestComp = 0 Then
//Insert code to run for a correct match
Else
//Insert code to run for incorrect match
End If

Function to return info

I changed the above to use string builder but some reason its not comming through on the loop its returning ok through the OrdersLine variable but not to the stream . Below is the loop im declaring it in
Dim OrdersLine As String
For Each item As String In split
For Each thisEntry As DataRow In orderHeaderInformation.Rows
orderLineInformation = connection.SqlSelectToDataTable(scriptBuilder.GetOrderLineInformation(item, thisEntry.Item("location")))
Dim orderNumber = From row In newEntries.AsEnumerable()
Select row.Field(Of String)("ordernumber") Distinct
For Each c In IO.Path.GetInvalidFileNameChars
filename = thisEntry.Item("orderNumber").ToString().Replace(c, "")
Next
ediExportPath = configuration.EditExport
filename = ediExportPath & "\" & filename & "_" & thisEntry.Item("location") & ".edi"
Dim streamWriter As New IO.StreamWriter(filename)
OrdersLine = ExportOrdersLine(orderLineInformation).ToString()
streamWriter.WriteLine(OrdersLine)
streamWriter.Close()
streamWriter.Dispose()
Next
Next
Public Function ExportOrdersLine(editProductLine As DataTable) As String
Dim retVal As String
Dim newRecord As infoEDILine
Dim filenameWithoutExtensions As String
Dim i As Integer = 1
Dim edilIneOrder As New StringBuilder
For Each thisentry In editProductLine.Rows
edilIneOrder.AppendLine("LIN+" & i & thisentry.Item("TagBcode") & ":EN'")
edilIneOrder.AppendLine("PIA+1" & thisentry.Item("PLU") & ":SA'")
edilIneOrder.AppendLine("IMD+C++CU'")
edilIneOrder.AppendLine("IMD+F++:::" & thisentry.Item("Style.Description") & "'")
edilIneOrder.AppendLine("QTY+" & thisentry.Item("PLU") & ":1'")
edilIneOrder.AppendLine("QTY+" & thisentry.Item("OnOrder") & ":1'")
edilIneOrder.AppendLine("TAX+7+VAT+++:::00" & thisentry.item("VatRate") & "'")
' if the vat rate is zero add three zeros to above line
' if the vat rate is not zero add only two 00 lke above line
' if no decimal places add one decimal place of zero
edilIneOrder.AppendLine("MOA+203:" & thisentry.item("LineNetCost") & "'")
edilIneOrder.AppendLine("PRI++AAA:" & thisentry.Item("GrossCost") & "'")
edilIneOrder.AppendLine("PRI++AAB:" & thisentry.Item("WholeSaleCost") & "'")
edilIneOrder.AppendLine("UNS+S'")
i = i + 1
Next
Return edilIneOrder.ToString()
End Function
Turns Out I was missing
streamWriter.AutoFlush = True