How to avoid first instance of data while string concatenation - vbscript

I am concatenation hlinks obtained from word do
data = data & "," & Vbcr & hlnk.Address & ":" & hlnk.TextToDisplay
Here ',' is separator.
Now I get every time data starting with ',' (obviously)
I then use
data = Right(data,Len(data)-1)
But I doubted my method of string concatenation.
Am I using Right method of string concatenation in first place?
I have seen ASP classic - how do I join an array of strings / join / implode do not work but I don't think that is my case. I am not joining array but creating one.

The canonical way to avoid leading (or trailing) list separators is to collect the items you want to concatenate in an array, then join that array. That's probably why #Filburt considered your question a (borderline) duplicate. If you don't know the number of items beforehand you'd dynamically resize the array:
ReDim a(-1)
For Each hlnk In ...
ReDim Preserve a(UBound(a)+1)
a(UBound(a)) = hlnk.Address & ":" & hlnk.TextToDisplay
Next
Once the array is filled you simply join the elements:
data = Join(a, "," & vbCr)
Otherwise you need to either handle the first (or last) element differently from the rest:
If IsEmpty(data) Then
data = hlnk.Address & ":" & hlnk.TextToDisplay
Else
data = data & "," & vbCr & hlnk.Address & ":" & hlnk.TextToDisplay
End If
or remove the leading (trailing) separator after you finished constructing the string:
data = Mid(data, 3)

Related

Select Case search options

I have an ASP query form that among other things includes a text box which allows the user to enter some text which can be searched for in a database. Unfortunately, the search only returns a result when there is a match for the exact string that was entered. Is there a way to change this so as to return a result for partial matches as well, or if what's in the DB includes all or part of the search string?
The code is below and the Case in question is the one titled "Nam". Note that I have sort of gotten around this issue by adding an asterix wild card feature, but I'd really just prefer to avoid using the asterixes altogether. I chose to leave the extra code for the wildcards out so as not to complicate things.
Select Case strOption
Case "Nam"
strSelect = strSelect & " Nam='" & UCase(strNam) & "'"
Case "Location"
strSelect = strSelect & " Location='" & UCase(strLocation) & "'"
Case "Typ"
strSelect = strSelect & " Typ='" & UCase(strTyp) & "'"
Case "Season"
strSelect = strSelect & " Season='" & UCase(strSeason) & "'"
Case "Duration"
strSelect = strSelect & " Duration='" & UCase(strDuration) & "'"
Case "Yr"
strSelect = strSelect & " Yr='" & UCase(strYr) & "'"
End Select
It's more about SQL than VBScript. According the Operators docs, any of comparison and logical operators return a Boolean data type with a value of TRUE, FALSE, or UNKNOWN. More important could be reading about building a search condition (a combination of one or more predicates) using operators in question; in brief:
= (equal to) is the operator used to test the equality between two expressions;
LIKE indicates that the subsequent character string is to be used with pattern matching and returns TRUE if the operand matches a pattern (more on valid syntax, escaping etc.); a pattern can include the following valid wildcard characters:
% Any string of zero or more characters;
_ (underscore) Any single character;
[] Any single character within the specified range ([a-f]) or set ([abcdef]);
[^] Any single character not within the specified range ([^a-f]) or set ([^abcdef]).
Thus, you could formulate a predicate on Nam in your search condition (in terms of VBScript) as follows:
strSelect = strSelect & " Nam LIKE '%" & UCase(strNam) & "%'"

Is there a way to make special characters work when using InStr in VBScript?

A VBScript is in use to shorten the system path by replacing entries with the 8.3 versions since it gets cluttered with how much software is installed on our builds. I'm currently adding the ability to remove duplicates, but it's not working correctly.
Here is the relevant portion of code:
original = "apple;orange;apple;lemon\banana;lemon\banana"
shortArray=Split(original, ";")
shortened = shortArray(1) & ";"
For n=2 to Ubound(shortArray)
'If the shortArray element is not in in the shortened string, add it
If NOT (InStr(1, shortened, shortArray(n), 1)) THEN
shortened = shortened & ";" & shortArray(n)
ELSE
'If it already exists in the string, ignore the element
shortened=shortened
End If
Next
(Normally "original" is the system path, I'm just using fruit names to test...)
The output should be something like
apple;orange;lemon\banana
The issue is entries with punctuation, such as lemon\banana, seem to be skipped(?). I've tested it with other punctuation marks, still skips over it. Which is an issue, seeing how the system path has punctuation in every entry.
I know the basic structure works, since there are only one of each entry without punctuation. However, the real output is something like
apple;orange;lemon\banana;lemon\banana
I thought maybe it was just a character escape issue. But no. It still will not do anything with entries containing punctuation.
Is there something I am doing wrong, or is this just a "feature" of VBScript?
Thanks in advance.
This code:
original = "apple;orange;apple;lemon\banana;lemon\banana"
shortArray = Split(original, ";")
shortened = shortArray(0) ' array indices start with 0; & ";" not here
For n=1 to Ubound(shortArray)
'If the shortArray element is not in in the shortened string, add it
'i.e. If InStr() returns *number* 0; Not applied to a number will negate bitwise
' If 0 = InStr(1, shortened, shortArray(n), 1) THEN
If Not CBool(InStr(1, shortened, shortArray(n), 1)) THEN ' if you insist on Not
WScript.Echo "A", shortArray(n), shortened, InStr(1, shortened, shortArray(n), vbTextCompare)
shortened = shortened & ";" & shortArray(n)
End If
Next
WScript.Echo 0, original
WScript.Echo 1, shortened
WScript.Echo 2, Join(unique(shortArray), ";")
Function unique(a)
Dim d : Set d = CreateObject("Scripting.Dictionary")
Dim e
For Each e In a
d(e) = Empty
Next
unique = d.Keys()
End Function
output:
0 apple;orange;apple;lemon\banana;lemon\banana
1 apple;orange;lemon\banana
2 apple;orange;lemon\banana
demonstrates/explains your errors (indices, Not) and shows how to use the proper tool for uniqueness (dictionary).

Concatenate rows per group in RDLC report

Is it possible to concatenate rows into single one? Attached image shows what is required. First table is what I get and second table is what I need. It is the same as SUM function but just need to concatenate strings instead of sum calculation.
I am unable to change datasource.
You can use the & operator.
=Exp1 & ", " & Exp2 & ", " & Exp3
This concatenates the three values with dots between them.

Vbs Multiplication Table

i was making resource for my programming class well its actually very basic scripting and i found this site and look through it there was realy many useful stuff about scripting but the thing i was searching for wasnt on the list or i wasnt using right keyword
anyway my question is
My teacher ask me to write a Vbs to Print Multiplication Table and i made researches and this is where i am right now;
dim sum, arraynum(), arrayline1, count, arraynum2(), arrayline2, arraynum3(), arrayline3, arraynum4(), arrayline4, arraynum5(), arrayline5
count=1
sum=1
arrayline1=1
for count=1 to 5
redim preserve arraynum(arrayline1)
redim preserve arraynum2(arrayline2)
redim preserve arraynum3(arrayline3)
redim preserve arraynum4(arrayline4)
redim preserve arraynum5(arrayline5)
arraynum(arrayline1)=sum
arraynum2(arrayline2)=sum*2
arrayline2=arrayline2+1
arraynum3(arrayline3)=sum*3
arrayline3=arrayline3+1
arraynum4(arrayline4)=sum*4
arrayline4=arrayline3+1
arraynum5(arrayline5)=sum*5
arrayline5=arrayline5+1
sum=sum+1
arrayline1=arrayline1+1
next
wscript.echo join(arraynum) & vbcrlf & join(arraynum2) & vbcrlf & join(arraynum3) & vbcrlf & join
(arraynum4) & vbcrlf & join(arraynum5)
' Its printing like;
' 1 2 3 4 5
' 2 4 6 8 10
' 3 6 8 12 15
' 4 8 12 16 20
' 5 10 15 20 25
as you can see they are not in a straight line and i wasnt able to do this with an input i mean take an input and show multiplication table for that i hope i made myself clear enough and if its not too much to ask how can i put a border between them or is it possible.
The trick is to apply leftpadding to your values that you are printing: Count the number of characters that a value contains, substract them from a fixed amount and add the same amount of spaces to the value.
This is an example that will replace and leftpad the vbTab character. If you join your arrays with a vbTab instead of the default space, you can use such a function.
Because this is a homework assignment, I added also some code that recursively get the multiples for a number, starting with 0. Just to trigger some curiosity. I would not recommend to just copy paste it, it does not comply to your requirement: "start from 1".
dim multiple
' Get the numbers 0 to 5
for each multiple in split(getMultiples(1,5), vbTab)
' print the multiplication table for each of this numbers
wscript.echo trim(TabToLpad(getMultiples(multiple, 5), 10))
next
' Does the calculation and returns a Tab delimited string of all multiples
function getMultiples(nr, amount)
getMultiples = 0
' As long as the amount is larger then 0, get the next multiple
if amount > getMultiples then getMultiples = getMultiples(nr, amount-1) & vbTab & (nr * amount)
End function
' Pads each value in a tab delimited string with the nrPadChars spaces. Returns a string.
function tabToLpad(str, nrPadChars)
dim part
for each part in split(str, vbTab)
tabToLpad = tabToLpad & string(nrPadChars - len(part), " ") & part
next
End Function

Sorting range in ascending numerical order containing strings by vba excel

i have a range containing the following strings:
step_1, step_10, step_3, step_2
using the following code
input_sh.Activate
With ActiveSheet
.Range("H2:H20").Select
.Sort.SortFields.Clear
.Sort.SortFields.Add Key:=Range("H2"), _
SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortTextAsNumbers 'xlSortNormal
With .Sort
.SetRange Range("H2:H20")
.Header = xlNo
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
step_10, step_1, step_2, step_3
but i would like to get
step_1, step_2,step_3,step_10
I would separate the number into another column, your last + 1, using mid function and sort on that.
Edit: I'm not at my pc but I think your only way to do this is to setup a macro that:
Filter your sheet by the first 9.
Cut and insert them before row 2.
Sort these on their own.
Then remove the filter and sort the rest as you have above.
Your strings have underscore followed by numbers. if that is going to be format of your string you can simply split your string using convert text to columns using "_" as your delimiter. Later you can sort and concatenate to get your sorted list of strings.
Sub Sample()
Columns(1).Copy Columns(3)
Columns("C:C").Select
Selection.TextToColumns Destination:=Range("C1"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, Other:=True, OtherChar:="_", FieldInfo:=Array(Array(1, 1), Array(2, 1))
Columns("D:D").Sort Range("D1")
i = 1
Do While Not IsEmpty(Range("C" & i))
Range("B" & i) = Range("C" & i) & "_" & Range("D" & i)
i = i + 1
Loop
End Sub
thanks every one for you contribution.
to user I found my solution before reading your suggestion. thanks anyway for your effort
my solution:
split str for "_"
write 2nd index next to filenames order 2nd cols by then col with only number
clean col with numbers

Resources