Using ASPPDF, I am creating a pdf form from user input.
When the user selects a radio option, I am able to set where the data will write within the PDF like this.
If Request("type") = 1 Then x=57
If Request("type") = 1 Then y=506 else
If Request("type") = 2 Then x=57
If Request("type") = 2 Then y=400 else
Page1.Canvas.SetParams "color=black, linewidth=2"
Page1.Canvas.DrawLine x, y, x + 7, y - 7
Page1.Canvas.DrawLine x, y - 7, x + 7, y
This generates an X mark in an appropriate box in my PDF.
My problem is that the value of those fields needs to be a string, not a number. When I try this, I do not receive any errors but it also doesn't write anything.
If Request("type") = AP Then x=57
If Request("type") = AP Then y=506 else
If Request("type") = AR Then x=57
If Request("type") = AR Then y=400 else
Page1.Canvas.SetParams "color=black, linewidth=2"
Page1.Canvas.DrawLine x, y, x + 7, y - 7
Page1.Canvas.DrawLine x, y - 7, x + 7, y
I am not able to simply change the values within the form to numbers as those same values are used in multiple places throughout the script and I need it as that value, not a number.
I've also tried adding " " (quotes) around the value, but that doesn't work either.
...
If Request("type") = "AP" Then x=57
...
Any help out there?
Wrong structured if .. then .. else statement. Right syntax is as follows:
' Single-Line syntax:
If condition Then statements [Else elsestatements ]
' Or, you can use the block form syntax:
If condition Then
[statements]
[ElseIf condition-n Then
[elseifstatements]] . . .
[Else
[elsestatements]]
End If
Hence, your code snipped could be as follows:
If UCase(Request("type")) = "AP" Then
x=57
y=506
ElseIf UCase(Request("type")) = "AR" Then
x=57
y=400
Else
'
End If
Or
Select Case UCase(Request("type"))
Case "AP"
x=57
y=506
Case "AR"
x=57
y=400
Case Else
'
End Select
Note: UCase function returns a string that has been converted to uppercase, as we could not know which letter case the Request("type") is (e.g. ap, aP, Ap or AP?).
Resource: VBScript Language Reference
Related
I have an example of a program that shows how to set up a counter for how many times each letter of the alphabet was used. I don't understand the syntax of the middle portion of the program.
LET letter$ = MID$(sentence$, LETTERNUMBER, 1)
I have tried searching on youtube and tutorials online
CLS
REM Make Counters for each Letter!!!
DIM Count(ASC("A") TO ASC("Z"))
REM Get the Sentence
INPUT "Enter Sentence:", sentence$
LET sentence$ = UCASE$(sentence$)
FOR I = ASC("A") TO ASC("Z")
LET Count(I) = 0
NEXT I
FOR LETTERNUMBER = 1 TO LEN(sentence$)
LET letter$ = MID$(sentence$, LETTERNUMBER, 1)
IF (letter$ >= "A") AND (letter$ <= "Z") THEN
LET k = ASC(letter$)
LET Count(k) = Count(k) + 1
END IF
NEXT LETTERNUMBER
PRINT
REM Display These Counts Now
LET letterShown = 0
FOR letternum = ASC("A") TO ASC("Z")
LET letter$ = CHR$(letternum)
IF Count(letternum) > 0 THEN
PRINT USING "\\## "; letter$; Count(letternum);
END IF
LET letterShown = letterShown + 1
IF letterShown = 7 THEN
PRINT
LET letterShown = 0
END IF
NEXT letternum
END
A through Z appears with the count of how many times they appeared.
The MID$ function returns a portion of a STRING's value from any position inside a string.
Syntax:
MID$(stringvalue$, startposition%[, bytes%])
Parameters:
stringvalue$
can be any literal or variable STRING value having a length. See LEN.
startposition%
designates the non-zero position of the first character to be returned by the function.
bytes%
(optional) tells the function how many characters to return including the first character when it is used.
Another method to calculate characters in a string:
REM counts and displays characters in a string
DIM count(255) AS INTEGER
PRINT "Enter string";: INPUT s$
' parse string
FOR s = 1 TO LEN(s$)
x = ASC(MID$(s$, s, 1))
count(x) = count(x) + 1
NEXT
' display string values
FOR s = 1 TO 255
PRINT s; "="; count(s); " ";
IF (s MOD 8) = 0 THEN
PRINT
IF (s MOD 20) = 0 THEN
PRINT "Press key:";
WHILE INKEY$ = "": WEND: PRINT
END IF
END IF
NEXT
END
Trying to write a macro to insert a hyphen at specific points in a text string depending on how long the string is or delete all text after said point.
i.e
- if 6 characters, insert a hyphen between char 4+5 or delete all text after char 4
- if 7 characters, insert a hyphen between char 5+6 or delete all text after char 5
Ideally i would love to be able to truncate the string at that point rather than hyphenate the text but i couldn't get my head around how to make it work so i decided to hyphen and then just run a find and replace '-*' to remove the unwanted characters. Can get this working on small sample sets 100-300 cells but i need the code to be able to go through workbooks with 70,000+ cells. I've tried tweaking the code to stop the memory issue but now i can't seem to get it to work.
Sub Postcodesplitter()
Dim b As Range, w As Long, c As Range, x As Long, d As Range, y As Long
For Each b In Selection
w = Len(b)
If w = 8 And InStr(b, "-") = 0 Then b = Application.WorksheetFunction.Replace(b, 15 - w, 0, "-")
For Each c In Selection
x = Len(c)
If x = 7 And InStr(c, "-") = 0 Then c = Application.WorksheetFunction.Replace(c, 13 - x, 0, "-")
For Each d In Selection
y = Len(d)
If y = 6 And InStr(d, "-") = 0 Then d = Application.WorksheetFunction.Replace(d, 11 - y, 0, "-")
Next
Next
Next
End Sub
That's the original code i put together, but it caused memory issues over 300 target cells. I'm a pretty bad coder even at the best of times but with some advice from a friend i tried this instead.
Sub Postcodesplitter()
Dim b As Range, x As Long
If (Len(x) = 6) Then
b = Application.WorksheetFunction.Replace(b, 11 - x, 0, "-")
Else
If (Len(x) = 7) Then
b = Application.WorksheetFunction.Replace(b, 13 - x, 0, "-")
Else
If (Len(x) = 8) Then b = Application.WorksheetFunction.Replace(b, 15 - x, 0, "-")
End Sub
But this just throws out errors when compiling. I feel like im missing something really simple.
Any tips?
It looks as though you want to truncate to two less than the existing number of characters, if that number is 6-8? If so, something like this:
Sub Postcodesplitter()
Dim data
Dim x as Long
Dim y as Long
data = Selection.Value
For x = 1 to ubound(data,1)
for y = 1 to ubound(data, 2)
Select Case Len(data(x, y))
Case 6 to 8
data(x, y) = left(data(x, y), len(data(x, y)) - 2)
end select
next y
next x
selection.value = data
End Sub
I have a INI file that looks like this !
[columnNumber]
Number1=2,4
this is my code
the ini file is read using function defined here
the columberNumber is taken as string "2,4" i want to split this and pass it into my select case object mytmp looping for all values in Number1 pass it to mytmp in select
columnNum = ReadIni(file, "columnNumber", "Number1")
mytmp = columnNum
x = Split(mytmp, ",")
For k = 0 To UBound(x)
'mytmp1 = Split(array_colnum, ",")
'mytmp2 = Search(array_col)
'mytmp1 = x(k)
'mytmp2 = x(k)
Next k
this is my select case
select case i...<does something>
select case mytmp
Could somebody guide me in doing this!
Updated : I want to put in select case i the values got from [columnNumber] my ReadIni function reads from [columnNumber section] the number1={2,4} i want to split this and store in a variable, the variable is read from select case variable
It's not completly clear what exactly you trying to do...
what is "I" variable? what it should contain?
mytmp = "2,4"
x = Split(mytmp, ",")
For k = LBound (x) To UBound (x)
'## Get value and store in variable "I".
i = x (k)
Select Case i
Case "2"
Response.Write "yep, it's 2"
Case "4"
Response.Write "yep, it's 4"
End Select
Next
I am trying to achieve the following:
The user is shown an excel spread sheet with a list of assumption which they can change.
Title | Value |
Input01 | 10 | =
Input02 | 2 | >=
Input03 | 800 | >=
Input04 | 4 | >=
Input05 | 2 | <=
There is an If .. Then Statement that pulls in data if the assumption are met. However if an assumption is blanc, it should not be included in the If .. Then Statement.
If x = Input01Value And y >= Input02Value _
And z >= Input03Value And a >= Input04Value _
And b <= Input05Value Then
User ommits Input03
If x = Input01Value And y >= Input02Value _
And a >= Input04Value And b <= Input05Value Then
Now I could check to see if each value exist, and then follow it by another If statement with the appropriate variables. But this seems a bit redundant.
I was wondering if something like the following is possible:
Input 01 = ""
If Input01Value != "" Then Input01 = "x = " & Input01Value
'Then use join or something similar to join all of them ..
And Then use this Input01 directly in the If .. Then statement. This way when a variable is empty the And .. are not included and the If statement will not fail.
Eg. (I know this doesn't work, just illustrating the scenario)
VBA: If Input01 Then
Result while compiling: If x = Input01Value Then
Please Note, I know I could do something like the following:
If Boolean And Variable2 > 4 Then and then have Boolean and Variable2 populate with a value in the cell, however the issue with this is that if the user, for example, decides to omit the Variable2 (which is reasonable) it will fail. eg. If (Boolean = True) And > 4 Then.
Hope my question is clear, thanks for the help.
What about using a function which operates on a select case depending on a string operator and two values?
Function conditionalString(condition As String, x As Variant, y As Variant) As Boolean
Select Case condition
Case "="
If (x = y) Then
conditionalString = True
Else
conditionalString = False
End If
Exit Function
Case ">="
conditionalString = (x >= y)
Exit Function
Case "<="
conditionalString = (x <= y)
Exit Function
Case ">"
conditionalString = (x > y)
Exit Function
Case "<"
conditionalString = (x < y)
Exit Function
Case Else
conditionalString = False
End Select
End Function
You could then just have another function, say "check if value isn't blank" before calling all assumptions.
Expanding on my comment, you can use something like this to test each row of input.
Function TestIt(testValue,inputOperator,inputValue) As Boolean
If Len(inputValue)=0 Then
TestIt=True 'ignore this test: no value supplied
Else
TestIt=Application.Evaluate(testValue & inputOperator & inputValue)
End If
End function
I may be asking a silly question but I am self teaching myself VBA and I am just stumped and I am not even sure what terms I can use to look up a solution.
I am writing a code that will compare three variables to three other variables then I want to display which variables have changed.
So if x = a but y <> b and z <> c then the output should be b/c
I have worked out a code that works fine
Dim Str As String
If X <> A Then
If Y <> B Then
If Z <> C Then
Str = "a/b/c"
Else
Str = "a/b"
End If
ElseIf Z <> C Then
Str = "a/c"
Else
Str = "a"
End If
ElseIf Y <> B Then
If Z <> C Then
Str = "b/c"
Else
Str = "b"
End If
Else
Str = "c"
End If
But as I increase the number of variables this becomes extremely complex very quickly.
If anyone can help direct me to a simpler method without the exponential complexity I would be very grateful.
Thank you all so much!
You need to test each variable pair independently from each other -- not link them together in one giant If construct tree.
Example:
str = "" 'Start with blank string. Append as required.
If x <> a Then str = str & "a/"
If y <> b Then str = str & "b/"
If z <> c Then str = str & "c/"
'Remove the extra / at the end
If Right(str, 1) = "/" Then str = Left(str, Len(str - 1))
You could put the 2 strings in 2 arrays, and then use a FOR...NEXT construct to loop both arrays. You can use UBound(arValues) to dynamically find out the number of items in the array.
Good luck