PDA

View Full Version : Solved: simplify



white_flag
06-09-2010, 07:59 AM
hello

question: it is possible to make't simpler this expression:

Controls("OptionButton1").Caption = Mid(sTemp, nEqualsSignPos)
Controls("OptionButton3").Caption = Mid(sTemp, nEqualsSignPos)
Controls("OptionButton8").Caption = Mid(sTemp, nEqualsSignPos)
Controls("OptionButton9").Caption = Mid(sTemp, nEqualsSignPos)
Controls("OptionButton12").Caption = Mid(sTemp, nEqualsSignPos)
Controls("OptionButton13").Caption = Mid(sTemp, nEqualsSignPos)
etc...
too become:

i = 1, 3, 8 ..etc
Controls("OptionButton" & i).Caption = Mid(sTemp, nEqualsSignPos)

thx :)

fumei
06-09-2010, 09:42 AM
Yes.

Assuming the variables sTemp and nEqualSignPos are validly declared and given valid values...
Dim arrCaption()
Dim var
' since you are using the same string value...
Dim strCaption As String

strCaption = Mid(sTemp, nEqualsSignPos)

arrCaption = Array("1", "8", "9", "12", "13")

For var = 0 To UBound(arrCaption())
Me.Controls("OptionButton" & arrCaption(var)).Caption = strCaption
Next
The array arrCaption is "1", "8", "9", "12", "13", thus when you run through the array:
For var = 0 To UBound(arrCaption())
Controls("OptionButton" & arrCaption(var)).Caption

becomes:

Controls("OptionButton" & "1").Caption
Controls("OptionButton" & "8").Caption
Controls("OptionButton" & "9").Caption
Controls("OptionButton" & "12").Caption
Controls("OptionButton" & "13").Caption

with all of them getting the same string variable (strCaption) - being equal to Mid(sTemp, nEqualsSignPos).

white_flag
06-09-2010, 12:43 PM
Gerry, thank you for the solution, it is working well.
I try to used in an diffrent "perspective" but it is not working (why?) my skills are not good enough.
so this is the second issue:
Call FillBM("insert2", strFindBetween(strImport, "insert2" & "=", "@"))
Call FillBM("insert7", strFindBetween(strImport, "insert7" & "=", "@"))
etc...
and from my point of view (with your solution in my head) become like this:
Dim arrCaption()
Dim var
arrCaption = Array("1", "8", "9", "12", "13")

For var = 0 To UBound(arrCaption())
Call FillBM("insert" & arrCaption(var), strFindBetween(strImport, "insert" & arrCaption(var)) & "=", "@")
Next
the FillBM it is an fuction that you provided some time ago and will fill some bookmarks
and the strFindBetween is an function that will cut an string between two markers.
but I recive this error:
Argument not optional. what it is wrong? (beside me)

fumei
06-09-2010, 01:06 PM
"Argument not optional. what it is wrong? (beside me)"

It has nothing to do with you personally.

It means literally that. Something is required as an argument, and you are not giving it.

I have NO idea what:

strFindBetween(strImport, "insert2" & "=", "@")

is supposed to be/mean. I can not read your mind, nor see what you have on screen.

What the heck is strFindBetween?????? Is it a Function? Strange name for a Function. Does that function have a required parameter/argument? Perhaps more importantly, if it IS a Function....WHAT do you have an an explicit data type.

Unless you have changed things, FillBM requires two parameters/arguments, both Strings.

Sub FillBM(yadda As String, whatever As String)

So.............

strFindBetween(strImport, "insert2" & "=", "@")

MUST MUST MUST return a string. One string.

white_flag
06-09-2010, 01:22 PM
correct
the mistake was here: I put the wrong ")"

Call FillBM("insert" & arrCaption(var), strFindBetween(strImport, "insert" & arrCaption(var) & "=", "@"))

it is working well.
Gerry, thank you very much

fumei
06-10-2010, 09:33 AM
Could please answer my question - as I am curious.

WHAT is strFindBetween?

white_flag
06-10-2010, 04:03 PM
Public Function strFindBetween(strToSearch As String, _
strStartPoint As String, strEndPoint As String, Optional _
skipInstance As Integer = 0) As String

'Quit the function if infomation provided doesn't fit requirements
If strToSearch = "" Or strStartPoint = "" Or strEndPoint = "" Then Exit Function
If Not InStrB(strToSearch, strStartPoint) > 0 _
And InStrB(strToSearch, strEndPoint) > 0 Then Exit Function

Dim trimStrLine As String
Dim strResult As String
Dim skipInst As Integer

trimStrLine = strToSearch

'Skip instances of first string; default doesn't skip
For skipInst = 0 To skipInstance
trimStrLine = Mid$(trimStrLine, InStr(trimStrLine, strStartPoint) + Len(strStartPoint))
Next skipInst

'Trim the line to the end of the second specified string
trimStrLine = Mid$(trimStrLine, 1, InStr(trimStrLine, strEndPoint) - 1)

'Assign the string between the two given string-points
strFindBetween = trimStrLine

End Function

it is an function an it is acting like this:
strFindBetween("This is an string.. etc @", "T", "@") the result will be: "his in an string.. etc " I need something like this, to insert in bookmarks some text from an txt file, it will insert also CR and some special characters that was not possible with "classic functions"or with my knowledge. The example is at my work here (home) I do not have the file. I will attached tomorrow.

fumei
06-11-2010, 09:26 AM
Function BetweenTokens(strIn As String, _
strStarting As String, _
strEnding As String)

If strToSearch = "" Or _
strStartPoint = "" Or _
strEndPoint = "" Then
Exit Function
End If

Dim j As Long, k As Long, l As Long

j = InStr(1, strIn, strStarting)
k = InStr(j, strIn, strEnding)
l = Len(strIn)

BetweenTokens = Mid$(strIn, j + 1, (k - j) - 1)
End Function

Now try both:
Sub TryBoth()
MsgBox strFindBetween("This is an string.. etc @", "T", "@")
MsgBox BetweenTokens("This is an string.. etc @", "T", "@")
End Sub
I do want to point out that both functions only deals with the first found instance of the parameters.

Both return: "his in an string.. etc "

white_flag
06-11-2010, 10:27 AM
correct.. but for the moment this are ok, for what I need.

fumei
06-11-2010, 12:52 PM
OK. I have been trying, in this as well as other threads, to point out that you are making more complicated code than you need to, but yes...it works. And actually, you do not even need the "l' variable.
Function BetweenTokens(strIn As String, _
strStarting As String, _
strEnding As String)

If strIn = "" Or _
strStarting = "" Or _
strEnding = "" Then
Exit Function
End If

Dim j As Long, k As Long

j = InStr(1, strIn, strStarting)
k = InStr(j, strIn, strEnding)
BetweenTokens = Mid$(strIn, j + 1, (k - j) - 1)
End Function
While yes, much code that I see here "works" (and not only yours), but as a general best-practice, it is better to have shorter, cleaner, code.

It makes for easier reading and understanding, by oneself and others. I am NOT trying to be critical, I am actually trying to help. Nonetheless, if you are happy with dealing with more complicated, convoluted coding...or would rather have more convoluted code...who am I to say otherwise?

I would like to point out that your original request here is for "simpler", and that is good. I would think that making things simpler would be applicable in every case.

white_flag
06-13-2010, 03:00 PM
you are correct..I would like to make some VBA code shorter and functional (it is easier to understand and to find an solution), in my opinion more skilled you become more simple and functional code you can write (logic). on the other side (like in my case), less skilled, more chances to complicate things were it is not necessary. But for the moment I do not know to know (and this is coming in time..if will come). so thx for the shorter code, I will used :)

have an nice day

Antwan