Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

excel script to convert range to string

Posted on 2011-05-06
8
Medium Priority
?
309 Views
Last Modified: 2012-05-11
so if I have

   column A  |   column B
---------------------------
1-5                   a
8-10                 b
23-27               c
29-29               d
32-33               e

and i want this:

  column A    |  column B
-------------------------------------
1                        a
2                        a
3                        a
4                        a
5                        a
8                        b
9                        b
10                      b
23                      c
24                      c
25                      c
26                      c
27                      c
etc....

how do I do that?
0
Comment
Question by:k1ng87
7 Comments
 
LVL 37

Expert Comment

by:TommySzalapski
ID: 35706977
Try this:
Private Sub CommandButton1_Click()
Dim lastRow As Integer, i As Integer, j As Integer, num As Integer, last As Integer, middle As Integer
Dim s As String

lastRow = Range("A" & Rows.Count).End(xlUp).Row

j = 1
i = 1
Range("C:D").Insert 0, 0

For i = 1 To lastRow
  s = Range("A" & i).Value
  middle = InStr(1, s, "-")
  num = Left(s, middle - 1)
  last = Mid(s, middle + 1)
  While num <= last
    Range("C" & j).Value = num
    Range("D" & j).Value = Range("B" & i).Value
    j = j + 1
    num = num + 1
  Wend
Next
Range("A:B").Delete
End Sub

Open in new window

0
 
LVL 1

Author Comment

by:k1ng87
ID: 35707054
almost works...but i forgot to mention something....sometimes the table looks like this:


   column A  |   column B
---------------------------
1-5                   a
8-10                 b
23-27               c
28                    d
29-29               e
32-33               f
36                    g
40-44               h
0
 
LVL 37

Accepted Solution

by:
TommySzalapski earned 1000 total points
ID: 35707090
No worries. Simple fix. Just add an if.
Private Sub CommandButton1_Click()
Dim lastRow As Integer, i As Integer, j As Integer, num As Integer, last As Integer, middle As Integer
Dim s As String

lastRow = Range("A" & Rows.Count).End(xlUp).Row

j = 1
i = 1
Range("C:D").Insert 0, 0

For i = 1 To lastRow
  s = Range("A" & i).Value
  middle = InStr(1, s, "-")
  If middle > 0 Then
    num = Left(s, middle - 1)
    last = Mid(s, middle + 1)
  Else
    num = Val(s)
    last = Val(s)
  End If
  While num <= last
    Range("C" & j).Value = num
    Range("D" & j).Value = Range("B" & i).Value
    j = j + 1
    num = num + 1
  Wend
Next
Range("A:B").Delete
End Sub

Open in new window

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 18

Assisted Solution

by:krishnakrkc
krishnakrkc earned 1000 total points
ID: 35707534
Hi,

Here is my take on this


Kris
Sub kTest()
    Dim ka, k(), x, i As Long, c As Long, n As Long, j As Long
    
    ka = [a1].CurrentRegion
    
    ReDim k(1 To UBound(ka, 1) * 5, 1 To 2)
    
    For i = 1 To UBound(ka, 1)
        x = Split(ka(i, 1), "-")
        If UBound(x) Then
            c = x(1) - x(0) + 1
            For j = 1 To c
                n = n + 1
                k(n, 1) = x(0) + j - 1: k(n, 2) = ka(i, 2)
            Next
        Else
            n = n + 1
            k(n, 1) = ka(i, 1): k(n, 2) = ka(i, 2)
        End If
    Next
    If n Then
        [d1].Resize(n, 2).Value = k
    End If
    
End Sub

Open in new window

0
 
LVL 1

Author Comment

by:k1ng87
ID: 35725068
whats the difference?
0
 
LVL 37

Expert Comment

by:TommySzalapski
ID: 35730187
They do the same thing. I would go with the one you can understand the most. If one is easier to read, then it's much easier to debug or update later.

One may run a few nanoseconds faster, but you probably wouldn't see the difference unless you had 1000s of lines.

I think mine is much easier to read and definitely has much better variable names.
0
 
LVL 50
ID: 35913502
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.
How can you see what you are working on when you want to see it while you to save a copy? Add a "Save As" icon to the Quick Access Toolbar, or QAT. That way, when you save a copy of a query, form, report, or other object you are modifying, you…

564 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