[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Storing values to an array

Posted on 2016-10-05
8
Medium Priority
?
99 Views
Last Modified: 2016-10-05
I have the following code which converts a cell string into a string of Ascii codes separated by comma:

Dim Str As String
Dim MyString As Variant

L = Len(Cell.Value)
c = Cell.Value

For P = 1 To L
    Sep = ","
    If P = L Then Sep = ""
    NextVal = Asc(Mid(c, P, 1)) & Sep
    Str = Str & NextVal
Next P

Open in new window


This works and if I put a Stop command in and hover over Str I see the comma separated list of Ascii codes.

I now want to refer to particular elements of the string so assume I need to convert it to an array so that I can use "Array(n)" to refer to the nth element of the array. How do I convert the string to an array or add the values to the array one at a time?

More specifically I want to sort the array into numerical order so am looking at using SMALL function to retrieve the Array elements in numerical order.

I have tried with adding:

MyString = Array(Str)
Ch = MyString(1)

Open in new window


Line 2 is trying to pull the first value from the Array but I get Subscript out of Range on that line.

Assistance much appreciated.

Thanks
Rob H
0
Comment
Question by:Rob Henson
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +2
8 Comments
 
LVL 43

Assisted Solution

by:Saqib Husain, Syed
Saqib Husain, Syed earned 500 total points
ID: 41829685
Try

MyString = Split(Str, ",")
0
 
LVL 33

Assisted Solution

by:Subodh Tiwari (Neeraj)
Subodh Tiwari (Neeraj) earned 500 total points
ID: 41829686
Have you tried MyString = Split(str, ",")
0
 
LVL 53

Accepted Solution

by:
Rgonzo1971 earned 1000 total points
ID: 41829691
pls try
Sub Macro()

Dim Str As String
Dim MyString As Variant

L = Len(Cell.Value)
c = Cell.Value

For P = 1 To L
    Sep = ","
    If P = L Then Sep = ""
    NextVal = Asc(Mid(c, P, 1)) & Sep
    Str = Str & NextVal
Next P
MyString = Split(Str, ",")
QuickSort MyString, LBound(MyString), UBound(MyString)
Ch = MyString(0)
End Sub

Sub QuickSort(arr, Lo As Long, Hi As Long)
  Dim varPivot As Variant
  Dim varTmp As Variant
  Dim tmpLow As Long
  Dim tmpHi As Long
  tmpLow = Lo
  tmpHi = Hi
  varPivot = arr((Lo + Hi) \ 2)
  Do While tmpLow <= tmpHi
    Do While arr(tmpLow) < varPivot And tmpLow < Hi
      tmpLow = tmpLow + 1
    Loop
    Do While varPivot < arr(tmpHi) And tmpHi > Lo
      tmpHi = tmpHi - 1
    Loop
    If tmpLow <= tmpHi Then
      varTmp = arr(tmpLow)
      arr(tmpLow) = arr(tmpHi)
      arr(tmpHi) = varTmp
      tmpLow = tmpLow + 1
      tmpHi = tmpHi - 1
    End If
  Loop
  If Lo < tmpHi Then QuickSort arr, Lo, tmpHi
  If tmpLow < Hi Then QuickSort arr, tmpLow, Hi
End Sub

Open in new window

0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 33

Expert Comment

by:Subodh Tiwari (Neeraj)
ID: 41829696
You can fill the array at the first place like below....
Dim MyString()
Dim i As Long
L = Len(Cell.Value)
c = Cell.Value

For P = 1 To L
    NextVal = Asc(Mid(c, P, 1))
    i = i + 1
    ReDim Preserve MyString(1 To i)
    MyString(i) = NextVal
Next P

Open in new window

And you can still incorporate the Str variable to have a comma separated values in the above code if required.
0
 
LVL 33

Author Comment

by:Rob Henson
ID: 41829811
@Neeraj and Saquib - Split(Str,",") did the business for making the String into an array; thank you.
@Rgonzo, I have tried your code in combo with a UDF but it doesn't give the correct result. The UDF and Sub are:

Function SortChar(c As Range)
Dim Str As String
Dim MyString As Variant

L = Len(c.Value)
T = c.Value

For P = 1 To L
    Sep = ","
    If P = L Then Sep = ""
    NextVal = Asc(Mid(T, P, 1)) & Sep
    Str = Str & NextVal
Next P

MyString = Split(Str, ",")
QuickSort MyString, LBound(MyString), UBound(MyString)
Ch = MyString(0)
SortChar = arr
End Function

Sub QuickSort(arr, Lo As Long, Hi As Long)
  Dim varPivot As Variant
  Dim varTmp As Variant
  Dim tmpLow As Long
  Dim tmpHi As Long
  tmpLow = Lo
  tmpHi = Hi
  varPivot = arr((Lo + Hi) \ 2)
  Do While tmpLow <= tmpHi
    Do While arr(tmpLow) < varPivot And tmpLow < Hi
      tmpLow = tmpLow + 1
    Loop
    Do While varPivot < arr(tmpHi) And tmpHi > Lo
      tmpHi = tmpHi - 1
    Loop
    If tmpLow <= tmpHi Then
      varTmp = arr(tmpLow)
      arr(tmpLow) = arr(tmpHi)
      arr(tmpHi) = varTmp
      tmpLow = tmpLow + 1
      tmpHi = tmpHi - 1
    End If
  Loop
  If Lo < tmpHi Then QuickSort arr, Lo, tmpHi
  If tmpLow < Hi Then QuickSort arr, tmpLow, Hi
End Sub

Open in new window

When I use the UDF and point at a cell containing "Robert Henson" I get 0

I was seeking help with this to try and get a different solution for the following question because the formula driven solution I gave would have involved too many helper columns.

https://www.experts-exchange.com/questions/28974215/Sorting-the-contents-of-a-text-string-in-Excel-in-Alphabetical-order.html?anchor=a41829745#a41829745

It looks like Aikimark has now solved that without resorting to Arrays with the following:
Public Function CollatedChars(ByVal parmString As String) As String
    Dim lngBuckets(0 To 255) As Long
    Dim lngLoop As Long
    Dim bChar As Byte
    For lngLoop = 1 To Len(parmString)
        bChar = Asc(Mid(parmString, lngLoop, 1))
        lngBuckets(bChar) = lngBuckets(bChar) + 1
    Next
    CollatedChars = vbNullString
    For lngLoop = LBound(lngBuckets) To UBound(lngBuckets)
        If lngBuckets(lngLoop) <> 0 Then
            CollatedChars = CollatedChars & String(lngBuckets(lngLoop), Chr(lngLoop))
        End If
    Next
End Function

Open in new window


For education purposes though, I would like to see why the UDF and Sub combo isn't working.

Thanks
Rob H
0
 
LVL 33

Author Comment

by:Rob Henson
ID: 41829844
Thanks RGonzo, now works. I guess it was just the Join(MyString) line that was missing which was pulling the string back from the QuickSort sub.

I will post the complete code in the other question as well for completeness; "Sharing the love!!" as they say.

Thanks
Rob H
0
 
LVL 53

Expert Comment

by:Rgonzo1971
ID: 41829856
corrected Code
Function SortChar(c As Range)
Dim Str As String, str1 As String
Dim MyString As Variant

L = Len(c.Value)
T = c.Value

For P = 1 To L
     Sep = ","
    If P = L Then Sep = ""
    NextVal = Asc(Mid(T, P, 1))
    Str = Str & Format(NextVal, "000") & Sep
Next P

MyString = Split(Str, ",")
QuickSort MyString, LBound(MyString), UBound(MyString)
Ch = MyString(0)
For Idx = 0 To UBound(MyString)
    NextVal = Chr(MyString(Idx))
    str1 = str1 & NextVal
Next
SortChar = str1
End Function

Sub QuickSort(arr, Lo As Long, Hi As Long)
  Dim varPivot As Variant
  Dim varTmp As Variant
  Dim tmpLow As Long
  Dim tmpHi As Long
  tmpLow = Lo
  tmpHi = Hi
  varPivot = arr((Lo + Hi) \ 2)
  Do While tmpLow <= tmpHi
    Do While arr(tmpLow) < varPivot And tmpLow < Hi
      tmpLow = tmpLow + 1
    Loop
    Do While varPivot < arr(tmpHi) And tmpHi > Lo
      tmpHi = tmpHi - 1
    Loop
    If tmpLow <= tmpHi Then
      varTmp = arr(tmpLow)
      arr(tmpLow) = arr(tmpHi)
      arr(tmpHi) = varTmp
      tmpLow = tmpLow + 1
      tmpHi = tmpHi - 1
    End If
  Loop
  If Lo < tmpHi Then QuickSort arr, Lo, tmpHi
  If tmpLow < Hi Then QuickSort arr, tmpLow, Hi
End Sub

Open in new window

0
 
LVL 46

Expert Comment

by:aikimark
ID: 41829886
Note: I'm posting this post-closure.

If you want a single statement conversion of the string, use the StrConv() function.
Example:
    Dim b(), lngLoop
    b = StrConv("Now is the time", vbFromUnicode)
    for lngLoop = 0 to UBound(b)
        debug.print lngLoop, b(lngLoop)
    next

Open in new window

 0             78 
 1             111 
 2             119 
 3             32 
 4             105 
 5             115 
 6             32 
 7             116 
 8             104 
 9             101 
 10            32 
 11            116 
 12            105 
 13            109 
 14            101 

Open in new window

0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

649 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question