+ Reply to Thread
Results 1 to 4 of 4

Save substring wrapped around delimited notations

Hybrid View

  1. #1
    Registered User
    Join Date
    11-12-2018
    Location
    Norway
    MS-Off Ver
    Excel
    Posts
    2

    Save substring wrapped around delimited notations

    Let's say that I have a string:

    StringOriginal = "I want to go to %Paris% and have a %lunch%"

    I want to be able to save the substrings wrapped aroung '%' as unique variables so that I also have:

    StringB = "Paris"
    StringC = "lunch"

    Does anyone know how I can manage to fix this?

  2. #2
    Valued Forum Contributor dmcgov's Avatar
    Join Date
    11-11-2015
    Location
    Florida, USA
    MS-Off Ver
    Office 365 Business
    Posts
    1,518

    Re: Save substring wrapped around delimited notations

    does this help?

    Sub get_substrings()
    Dim x, y, z
    Dim a, b, c
    Dim stringOrginal As String
    stringOriginal = "I want to go to %Paris% and have a %lunch%"
    x = Split(stringOriginal, "%")
    y = UBound(x)
    
    For a = 1 To y Step 2
    b = b + 1
    z = Mid(Columns(b).Address, 2, 1)
        Debug.Print "string" & z & "=" & x(a)
    Next a
    
    End Sub
    Last edited by dmcgov; 02-14-2019 at 07:32 AM. Reason: changed the var from long to string

  3. #3
    Registered User
    Join Date
    11-12-2018
    Location
    Norway
    MS-Off Ver
    Excel
    Posts
    2

    Re: Save substring wrapped around delimited notations

    This one worked perfectly! I just had to add a dictionary in order to save the variables. What I need next is a way to combine the dynamic variables into a new variable where the length depends on the number of dynamic variables.

    Let's say that I have now have have i numbers of dynamic variables, and I want to create a string that consists of these variables:

    stringEnd = dynVariable(1) & dynVariable(2) & ... & dynVariable(i)

    If I have two dynamic variables like below I want to end up with:

    stringEnd = dynVariable(1) & dynVariable(2)

    However, if I have four dynamic variables, I want to end up with:

    stringEnd = dynVariable(1) & dynVariable(2) & dynVariable(3) & dynVariable(4)

    etc.. Anyone who knows how I can work this out?

    Private Sub get_substringss()
    
        Dim x, y, z, i, a, b, c
        Dim dynVariable As Object, stringOriginal As String, stringEnd as string
    
        Set dynVariable = CreateObject("Scripting.Dictionary")
        stringOriginal = "I want to go to %Paris% and have a %lunch%."
        x = Split(stringOriginal, "%")
        y = UBound(x)
    
        For a = 1 To y Step 2
            b = b + 1
            z = Mid(Columns(b).Address, 2, 1)
            dynVariable(b) = x(a)
        Next a
    
        stringEnd = dynVariable(1) & dynVariable(2) & ... & dynVariable(i)
    
    End sub
    Last edited by frkrfjrf; 02-26-2019 at 06:48 PM.

  4. #4
    Valued Forum Contributor dmcgov's Avatar
    Join Date
    11-11-2015
    Location
    Florida, USA
    MS-Off Ver
    Office 365 Business
    Posts
    1,518

    Re: Save substring wrapped around delimited notations

    so i don't think that there is a way to dynamically dimension variables but i did find a way by using a dictionary approach. see the following code:

    Sub get_substrings()
    Dim x, y As Long, z As String
    Dim a As Long, b As Long, c As String
    Dim stringOrginal As String
    Dim dyn_var As Object
    Dim key As Variant
    
    Set str_var = CreateObject("Scripting.Dictionary")
    
    stringOriginal = "I want to go to %Paris% and have a %lunch% on a %terrace% overlooking the %champseleyse%"
    
    x = Split(stringOriginal, "%")
    y = UBound(x)
    
    For a = 1 To y Step 2
        b = b + 1
        z = "String" & Mid(Columns(b).Address, 2, 1)
        str_var(z) = Chr(34) & x(a) & Chr(34)
        Debug.Print z & " = " & Chr(34) & x(a) & Chr(34)
    Next a
    
    Debug.Print "count is " & str_var.Count
    
    For Each key In str_var.keys
        Debug.Print key, str_var(key)
    Next key
    MsgBox str_var("StringD")
    End Sub
    then you cans say something like: if str_var("StringA") = "Paris" then msgbox "Found var"

    is this something that you can work with?

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Macro to search cells for substring and replace contents of cell if substring is found
    By robbyvegas in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-31-2015, 06:40 AM
  2. how to get the chess notations in columns?
    By sumesh56 in forum Excel General
    Replies: 38
    Last Post: 05-01-2014, 07:53 AM
  3. Search substring within range, report the substring found
    By Brooke1578 in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 12-20-2012, 03:56 PM
  4. Excel formula grammer in BNF or other notations
    By Ramesh561 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 12-07-2012, 02:15 PM
  5. [SOLVED] Extract Substring, then Ignore that Substring, while collecting data from Other substrings
    By Sameki121 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 04-17-2012, 05:21 PM
  6. Excel 2007 : Save as x Delimited
    By forJAT in forum Excel General
    Replies: 2
    Last Post: 09-30-2009, 06:09 PM
  7. Substring of a Chr(10) delimited cell
    By peacelittleone in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-05-2005, 03:53 PM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1