Explanation of code

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Can someone please explain to me what this line of code is doing. I have some idea, but would like to know exactly what it is doing. If you could break it down that would be great. Thanks. Mat

nChkNum = CInt(Mid$(ole.Name, Len("Checkbox") + 1))
 
Matt,

CInt converts a number held as a string into an integer.

Len returns the number of characters in a string or value.

Mid$ returns a subset of another string as a string. In
this case, the string in question is the Name property
of "ole" (which I assume is an ole Object variable.) Thus,
Mid("Peter",2,3) would return "ete".

The second argument in the Mid function determines the
point in the string at which the substring starts and this
is where the problem arises(if you are having a problem,
that is...)

Len("Checkbox") will always return 8, as you are
calculating the length of the string contained between the
inverted commas. If it is supposed to be testing a
variable, the correct form would be:
Len(Checkbox)
or Len(Checkbox.value) if it's an object.

If you're happy with it the way it is then:
CInt(Mid$(ole.Name, 9))
would be simpler!

Cheers, Pete
-----Original Message-----
Can someone please explain to me what this line of code
is doing. I have some idea, but would like to know
exactly what it is doing. If you could break it down that
would be great. Thanks. Matt
 
Thanks Pete. So what your last comment is saying is that I can just say to start the substring at the 9th position and it will pick up everything from that point on or do I have to tell it how many characters to go from the starting point. The reason is I have control checkboxes, and I needed to take the numbers from the names of the checkboxes to do a loop that will only make a range of checkboxes visible. My range is from checkbox2-checkbox30. The main reason I wanted to understand what this does, is that I want to rename the cells that these checkboxes are in so that the cell w/ checkbox 2 will be renamed to "Box 2". Then write a loop that will check if checkbox2 value it true. If it is true, then I want to change the locked on "Box 2/cell" to true. Also, if there is text in "Box 2/cell", then I want to make checkbox2 visible to be false. This way they can either select the checkbox or type text in the cell, but not both. Any help would be great. Thanks. Matt
 
Matt,

the mid statement has three parts. Mid("Matt",2,1)

The first is the source string, the second the start
character for the result and the third how many characters
to return. If you omit the third argument, it will go up
to the end of te source string. For example:
Mid("Matt",2,1) returns "a", but

Mid("Matt",2) returns "att".

With regards to you checkboxes, are they in a userform and
how many are there in total? Has checkbox1 been deleted,
or is it just not part of the group you want to manipulate?

Pete
-----Original Message-----
Thanks Pete. So what your last comment is saying is that
I can just say to start the substring at the 9th position
and it will pick up everything from that point on or do I
have to tell it how many characters to go from the
starting point. The reason is I have control checkboxes,
and I needed to take the numbers from the names of the
checkboxes to do a loop that will only make a range of
checkboxes visible. My range is from checkbox2-
checkbox30. The main reason I wanted to understand what
this does, is that I want to rename the cells that these
checkboxes are in so that the cell w/ checkbox 2 will be
renamed to "Box 2". Then write a loop that will check if
checkbox2 value it true. If it is true, then I want to
change the locked on "Box 2/cell" to true. Also, if there
is text in "Box 2/cell", then I want to make checkbox2
visible to be false. This way they can either select the
checkbox or type text in the cell, but not both. Any help
would be great. Thanks. Matt
 
Back
Top