Results 1 to 7 of 7

Thread: Varriable array size

  1. #1

    Thread Starter
    Lively Member Nigorr's Avatar
    Join Date
    Apr 2002
    Location
    Brisbane, Australia
    Posts
    106

    Varriable array size

    I have the following code, and what it is doing is going through a spreadsheet in excel and splitting up the string that is dumped out of Novell. What it does is for example it splits "username.context.tree" into three parts. This works fine except when it comes to things like "username.subcontext.context.tree" or "username.tree". So how would i go about making choices and giving me the right bits in the places??

    VB Code:
    1. Option Explicit
    2. Function mySplit(strToSplt, strSplitOn) 'Split Function
    3.     Dim ip1, ip2
    4.     Dim strArray()
    5.     Dim iCount
    6.    
    7.    
    8.     ip1 = 1: ip2 = 1
    9.     Do
    10.       ip2 = InStr(ip1, strToSplt, strSplitOn)
    11.       If ip2 = 0 Then
    12.         ip2 = Len(strToSplt) + 1
    13.       End If
    14.      
    15.       If iCount Mod 100 = 0 Then
    16.         ReDim Preserve strArray(iCount + 100)
    17.       End If
    18.       strArray(iCount) = Mid$(strToSplt, ip1, ip2 - ip1)
    19.      
    20.       ip1 = ip2 + Len(strSplitOn)
    21.       iCount = iCount + 1
    22.      
    23.     Loop Until ip2 >= Len(strToSplt)
    24.    
    25.     ReDim Preserve strArray(iCount - 1)
    26.     mySplit = strArray
    27.    
    28. End Function
    29.  
    30. Sub test()
    31. Dim rw
    32. Dim intRow As Long
    33. Dim Mystr, strParams, strUserName, strContext, strSubContext, strTree As String
    34.  
    35. intRow = 1
    36. For Each rw In Worksheets("allusers").Cells(1, 1).CurrentRegion.Rows 'Do this for every row in excel
    37.     intRow = intRow 'get the row number (Im not really sure how this works but it does further down
    38.     Mystr = rw.Cells(1, 1).Value 'Get the string to split up
    39.     strParams = mySplit(Mystr, ".") 'Send string tho be split
    40.     If isArray(strParams) Then 'assign the split string to the right variables
    41.         strUserName = Trim(strParams(0))
    42.         strSubContext = Trim(strParams(1))
    43.         strContext = Trim(strParams(2))
    44.         strTree = Trim(strParams(3))
    45.     End If
    46.     rw.Range("C" & intRow).Value = strUserName 'put the username in cell D and the row
    47.     rw.Range("D" & intRow).Value = strSubContext 'put the context1 if it exsits in cell D and the row
    48.     rw.Range("E" & intRow).Value = strContext 'put the context 2 in cell D and the row
    49.     rw.Range("F" & intRow).Value = strTree 'put the tree in cell E and the row
    50.  
    51. Next rw
    52.  
    53. End Sub
    Hope you see what I am getting at..

  2. #2
    Frenzied Member Microbasic's Avatar
    Join Date
    Mar 2001
    Posts
    1,402
    Let's change your Sub a little bit:

    VB Code:
    1. Sub test()
    2.     Dim rw
    3.     Dim intRow As Long
    4.     Dim Mystr, strParams(), strUserName, strContext, strSubContext, strTree As String
    5.  
    6.     intRow = 1
    7.     For Each rw In Worksheets("allusers").Cells(1, 1).CurrentRegion.Rows 'Do this for every row in excel
    8.         intRow = intRow 'get the row number (Im not really sure how this works but it does further down
    9.         Mystr = rw.Cells(1, 1).Value 'Get the string to split up
    10.         strParams = mySplit(Mystr, ".") 'Send string tho be split
    11.         If isArray(strParams) Then 'assign the split string to the right variables
    12.             strUserName = Trim(strParams(0))
    13.         End If
    14.         If UBound(strParams) = 1 Then
    15.             strTree = Trim(strParams(1))
    16.         ElseIf UBound(strParams) = 2 Then
    17.             strContext = Trim(strParams(1))
    18.             strTree = Trim(strParams(2))
    19.         ElseIf UBound(strParams) = 3 Then
    20.             strSubContext = Trim(strParams(1))
    21.             strContext = Trim(strParams(2))
    22.             strTree = Trim(strParams(3))
    23.         End If
    24.         rw.Range("C" & intRow).Value = strUserName 'put the username in cell D and the row
    25.         rw.Range("D" & intRow).Value = strSubContext 'put the context1 if it exsits in cell D and the row
    26.         rw.Range("E" & intRow).Value = strContext 'put the context 2 in cell D and the row
    27.         rw.Range("F" & intRow).Value = strTree 'put the tree in cell E and the row
    28.     Next rw
    29. End Sub

    Does this work?


    MicroBasic
    Dragon Shadow Trainer

    There is no good or evil in the world...only programmers and fools .

  3. #3

    Thread Starter
    Lively Member Nigorr's Avatar
    Join Date
    Apr 2002
    Location
    Brisbane, Australia
    Posts
    106

    Figured it out

    I have come up with the following all i had to do was use the ubound thing. But I still don't know what is going on with the following line in the following code.
    VB Code:
    1. 'what is this intRow needed for is there something else
    2. 'I can stick in this line??
    3. rw.Range("C" & intRow).Value = strUserName
    VB Code:
    1. Option Explicit
    2. Function mySplit(strToSplt, strSplitOn)
    3.     Dim ip1, ip2
    4.     Dim strArray()
    5.     Dim iCount
    6.    
    7.     ip1 = 1: ip2 = 1
    8.     Do
    9.       ip2 = InStr(ip1, strToSplt, strSplitOn)
    10.       If ip2 = 0 Then
    11.         ip2 = Len(strToSplt) + 1
    12.       End If
    13.      
    14.       If iCount Mod 100 = 0 Then
    15.         ReDim Preserve strArray(iCount + 100)
    16.       End If
    17.       strArray(iCount) = Mid$(strToSplt, ip1, ip2 - ip1)
    18.      
    19.       ip1 = ip2 + Len(strSplitOn)
    20.       iCount = iCount + 1
    21.      
    22.     Loop Until ip2 >= Len(strToSplt)
    23.    
    24.     ReDim Preserve strArray(iCount - 1)
    25.     mySplit = strArray
    26.    
    27. End Function
    28.  
    29. Sub test()
    30. Dim intRow, intThisOne As Integer
    31. Dim rw As Variant
    32. Dim MyStr, strParams, strUserName, strSubContext, strContext, strTree As String
    33. intRow = 1
    34.  
    35. 'Do this for every row in worksheet
    36. For Each rw In Worksheets("allusers").Cells(1, 1).CurrentRegion.Rows
    37.     MyStr = rw.Cells(1, 1).Value    'Get the string to split up
    38.     strParams = mySplit(MyStr, ".") 'Send string tho be split
    39.     If UBound(strParams) = 2 Then intThisOne = 1 'username.context.tree
    40.     If UBound(strParams) = 1 Then intThisOne = 2 'username.tree
    41.     If UBound(strParams) = 3 Then intThisOne = 3 'username.subcontext.context.tree
    42.    
    43.     Select Case intThisOne
    44.         Case 1 'username.context.tree
    45.             strUserName = Trim(strParams(0))
    46.             strSubContext = ""
    47.             strContext = Trim(strParams(1))
    48.             strTree = Trim(strParams(2))
    49.        
    50.         Case 2 'username.tree
    51.             strUserName = Trim(strParams(0))
    52.             strSubContext = ""
    53.             strContext = ""
    54.             strTree = Trim(strParams(1))
    55.                    
    56.         Case 3 'username.subcontext.context.tree
    57.             strUserName = Trim(strParams(0))
    58.             strSubContext = Trim(strParams(1))
    59.             strContext = Trim(strParams(2))
    60.             strTree = Trim(strParams(3))
    61.                
    62.     End Select
    63.    
    64.     If strUserName <> "" Then
    65.         rw.Range("C" & intRow).Value = strUserName      'put the username in column D and the row
    66.         rw.Range("D" & intRow).Value = strSubContext    'put the subcontext  in column D and the row
    67.         rw.Range("E" & intRow).Value = strContext       'put the context in column D and the row
    68.         rw.Range("F" & intRow).Value = strTree          'put the tree in column E and the row
    69.     End If
    70.    
    71. Next rw
    72.  
    73. End Sub

  4. #4

    Thread Starter
    Lively Member Nigorr's Avatar
    Join Date
    Apr 2002
    Location
    Brisbane, Australia
    Posts
    106

    It does

    It does work, thank you. I did the same thing just a bit more long winded though. I am fairly new to programing vb/vba/vbscript so I guess I will learn to do things more quickly as I go along.

    I want to get a hold of vb.net so i can make my asp pages into asp.net pages more easily.

    EDIT: But I still don't know how to go about the following line because obviously it can be shortened.
    VB Code:
    1. rw.Range("C" & intRow).Value = strUserName

  5. #5
    Frenzied Member Microbasic's Avatar
    Join Date
    Mar 2001
    Posts
    1,402
    First of all, that "intRow" is a part of YOUR code. I didn't add it.

    Second, it seems to always point to a alue of 1. Therefore, I'm assuming that you can replace intRow with "1":

    "C1"


    MicroBasic
    Dragon Shadow Trainer

    There is no good or evil in the world...only programmers and fools .

  6. #6
    Frenzied Member Microbasic's Avatar
    Join Date
    Mar 2001
    Posts
    1,402
    Never mind, then.


    MicroBasic
    Dragon Shadow Trainer

    There is no good or evil in the world...only programmers and fools .

  7. #7

    Thread Starter
    Lively Member Nigorr's Avatar
    Join Date
    Apr 2002
    Location
    Brisbane, Australia
    Posts
    106
    That works, and i know i put intRow in I thought I had to obviosly not.

    I think it also relates to the right cell because of the for each statement. so it is only looking at the the one row at a time so even though it really is D155 it sees that row as A1, B1 C1 etc.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width