VBA Code transform numbers in specific columns into other numbers

LNG2013

Active Member
Joined
May 23, 2011
Messages
466
I need some VBA code that will look at only specific columns, eg: ValueA11, ValueB11, ValueC11, etc... and transform its data from a single number into a number sequence.

I have about 50 columns I need to do this for, and there are other columns that have similar data that I don't want affected by this.

Eg. the first value in the column ValueA11 = 7, I want the value to be changed into 1 2 3 4 5 6 7. The second value is 4, so it should translate to 1 2 3 4. This sequencing would not go higher then 7. More examples below:


<STYLE type=text/css>
table.tableizer-table {border: 1px solid #CCC; font-family: Arial, Helvetica, sans-serif; font-size: 12px;} .tableizer-table td {padding: 4px; margin: 3px; border: 1px solid #ccc;}
.tableizer-table th {background-color: #104E8B; color: #FFF; font-weight: bold;}
</STYLE>
<TABLE style="WIDTH: 278px; HEIGHT: 234px" class=tableizer-table>
<TBODY><TR class=tableizer-firstrow><TH>ValueA11</TH><TH>ValueA11</TH></TR><TR><TD>7</TD><TD>1 2 3 4 5 6 7</TD></TR><TR><TD>4</TD><TD>1 2 3 4</TD></TR><TR><TD>2</TD><TD>1 2</TD></TR><TR><TD>1</TD><TD>1</TD></TR><TR><TD>3</TD><TD>1 2 3</TD></TR><TR><TD>6</TD><TD>1 2 3 4 5 6</TD></TR><TR><TD>5</TD><TD>1 2 3 4 5</TD></TR>


</TABLE>
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
Here's a function to convert a number greater than 0 to the string you want. Not that the result will be text, so its value will be zero if you try to do any math on it. I'm not sure how you're designating columns and which ones to convert this way.

Code:
Public Function RavelNumber$(Number&)
    Dim I&
 
    RavelNumber$ = ""
    If Number& < 1 Then Exit Sub
 
    For I& = 1 To Number&
        If Len(RavelNumber$) > 0 Then RavelNumber$ = RavelNumber$ & Space$(1)
        RavelNumber$ = RavelNumber$ & I&
    Next
 
End Function
 
Upvote 0
I'm assuming that your column headers begin at A1 and are in consecutive cells in row 1 -- no intervening blanks. I'm also assuming that the data begin at A2, B2, etc., and have no blanks until the bottom of the data. Here's the whole package, which will operate on columns with headers ValueA11 and ValueC11:

Code:
Public Sub DoIt()
    Call RavelColumns("ValueA11,ValueC11")
End Sub
 
Public Sub RavelColumns(HdrsToRavel$)
    Dim I&, ColHdr As Range, ColCell As Range, ColsToRavel As Variant
 
    ColsToRavel = Split(HdrsToRavel$, ",")
 
    ' Assume headers start in A1 and are contiguous
    For Each ColHdr In Range(Range("A1"), Range("A1").End(xlToRight))
 
        ' See if this header is among the desired ones
        For I& = 0 To UBound(ColsToRavel)
        If ColHdr.Text = ColsToRavel(I&) Then
 
            ' Assume data begin right below the header and are contiguous
            For Each ColCell In Range(ColHdr.Offset(1, 0), ColHdr.End(xlDown))
                If IsNumeric(ColCell.Value) Then ColCell.Value = "'" & RavelNumber$(ColCell.Value)
            Next
 
        End If
        Next
    Next
End Sub
 
Public Function RavelNumber$(Number&)
    Dim I&
 
    RavelNumber$ = ""
    If Number& < 1 Then Exit Function
 
    For I& = 1 To Number&
        If Len(RavelNumber$) > 0 Then RavelNumber$ = RavelNumber$ & Space$(1)
        RavelNumber$ = RavelNumber$ & I&
    Next
 
End Function
 
Upvote 0
Hey Jasmith!

That worked, it takes a little while to complete, about 2-3 minutes, but it does the trick!

Any ideas on how I might speed it up a bit?

Update: there is a bad side effect, where by this code adds a bunch of commas to my csv ( it uses fields that are empty)... this makes the csv into about an 8-9mb file, it is usually 2-3k. Any idea on how to add limiters to only fields that have data in them.



I'm assuming that your column headers begin at A1 and are in consecutive cells in row 1 -- no intervening blanks. I'm also assuming that the data begin at A2, B2, etc., and have no blanks until the bottom of the data. Here's the whole package, which will operate on columns with headers ValueA11 and ValueC11:

Code:
Public Sub DoIt()
    Call RavelColumns("ValueA11,ValueC11")
End Sub
 
Public Sub RavelColumns(HdrsToRavel$)
    Dim I&, ColHdr As Range, ColCell As Range, ColsToRavel As Variant
 
    ColsToRavel = Split(HdrsToRavel$, ",")
 
    ' Assume headers start in A1 and are contiguous
    For Each ColHdr In Range(Range("A1"), Range("A1").End(xlToRight))
 
        ' See if this header is among the desired ones
        For I& = 0 To UBound(ColsToRavel)
        If ColHdr.Text = ColsToRavel(I&) Then
 
            ' Assume data begin right below the header and are contiguous
            For Each ColCell In Range(ColHdr.Offset(1, 0), ColHdr.End(xlDown))
                If IsNumeric(ColCell.Value) Then ColCell.Value = "'" & RavelNumber$(ColCell.Value)
            Next
 
        End If
        Next
    Next
End Sub
 
Public Function RavelNumber$(Number&)
    Dim I&
 
    RavelNumber$ = ""
    If Number& < 1 Then Exit Function
 
    For I& = 1 To Number&
        If Len(RavelNumber$) > 0 Then RavelNumber$ = RavelNumber$ & Space$(1)
        RavelNumber$ = RavelNumber$ & I&
    Next
 
End Function
 
Last edited:
Upvote 0
See if this works any better. I removed the initial apostrophe from the converted values among other improvements:

Code:
Option Explicit
Public Sub DoIt()
    Call RavelColumns("ValueA11,ValueC11")
End Sub
 
Public Sub RavelColumns(HdrsToRavel$)
    Dim I&, ColHdrs As Range, Hdr As Range, Hdr1 As Range, ColCell As Range, ColsToRavel As Variant
 
    ColsToRavel = Split(HdrsToRavel$, ",")
 
    For I& = 0 To UBound(ColsToRavel)
 
        Set Hdr1 = Nothing
        Set Hdr = Rows(1).Find(ColsToRavel(I&), LookIn:=xlValues, LookAt:=xlWhole, MatchCase:=False)
        Do While Not Hdr Is Nothing
 
            ' Mark the first find
            If Hdr1 Is Nothing Then Set Hdr1 = Hdr
 
            ' Assume data begin right below the header and are contiguous
            For Each ColCell In Range(Hdr.Offset(1, 0), Hdr.End(xlDown))
                If IsNumeric(ColCell.Value) Then ColCell.Value = RavelNumber$(ColCell.Value)
            Next
 
            ' Continue until wrapped back to first find
            Set Hdr = Rows(1).FindNext(Hdr)
            If Hdr.Address = Hdr1.Address Then Exit Do
 
        Loop
 
    Next
End Sub
 
Public Function RavelNumber$(Number&)
    If Number& >= 1 And Number& <= 7 Then RavelNumber$ = Left("1 2 3 4 5 6 7", 2 * Number& - 1)
End Function
 
Upvote 0
Hey Jamith!
Thanks for your help, unfortunately still got the same result, I can send you the file if you PM me your email address.

See if this works any better. I removed the initial apostrophe from the converted values among other improvements:

Code:
Option Explicit
Public Sub DoIt()
    Call RavelColumns("ValueA11,ValueC11")
End Sub
 
Public Sub RavelColumns(HdrsToRavel$)
    Dim I&, ColHdrs As Range, Hdr As Range, Hdr1 As Range, ColCell As Range, ColsToRavel As Variant
 
    ColsToRavel = Split(HdrsToRavel$, ",")
 
    For I& = 0 To UBound(ColsToRavel)
 
        Set Hdr1 = Nothing
        Set Hdr = Rows(1).Find(ColsToRavel(I&), LookIn:=xlValues, LookAt:=xlWhole, MatchCase:=False)
        Do While Not Hdr Is Nothing
 
            ' Mark the first find
            If Hdr1 Is Nothing Then Set Hdr1 = Hdr
 
            ' Assume data begin right below the header and are contiguous
            For Each ColCell In Range(Hdr.Offset(1, 0), Hdr.End(xlDown))
                If IsNumeric(ColCell.Value) Then ColCell.Value = RavelNumber$(ColCell.Value)
            Next
 
            ' Continue until wrapped back to first find
            Set Hdr = Rows(1).FindNext(Hdr)
            If Hdr.Address = Hdr1.Address Then Exit Do
 
        Loop
 
    Next
End Sub
 
Public Function RavelNumber$(Number&)
    If Number& >= 1 And Number& <= 7 Then RavelNumber$ = Left("1 2 3 4 5 6 7", 2 * Number& - 1)
End Function
 
Upvote 0
OK, I sent it. Can you give me an idea of the CSV's dimensions? That is, when you open it manually and do Ctrl-End, where do you end up?
 
Upvote 0

Forum statistics

Threads
1,224,597
Messages
6,179,813
Members
452,945
Latest member
Bib195

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top