You are here:

- Home
- Computing/Technology
- Business Software
- Excel
- Altering VBA

Advertisement

Hi Tom

I have made a slight alteration to a piece of code that you kindly wrote for me. When I now run the code it fails at the the following line:

"For i = LBound(v, 1) To UBound(v, 1)"

Your original code was:

Sub rearrangecolumns()

Dim s As String, v As Variant, r As Range, cell As Range

Dim i As Long, r1 As Range

s = "{""A"",""Name"";""B"",""Year"";""C"",""Gender"";" & _

"""D"",""test"";""E"",""test2"";""F"",""FSM""}"

v = Evaluate(s)

'For i = LBound(v, 1) To UBound(v, 1)

' Debug.Print v(i, 1), v(i, 2)

'Next

For i = LBound(v, 1) To UBound(v, 1)

Set r = Range("A1", Cells(1, Columns.Count).End(xlToLeft))

If Len(Trim(v(i, UBound(v, 2)))) <> 0 Then

bFound = False

For Each cell In r

If LCase(cell.Value) = LCase(v(i, UBound(v, 2))) Then

Set r1 = Cells(1, v(i, LBound(v, 1))).EntireColumn

cell.EntireColumn.Copy

r1.Insert shift:=xlToRight

cell.EntireColumn.Delete

bFound = True

Exit For

End If

Next

End If

If Not bFound Then

Set r1 = Cells(1, v(i, LBound(v, 1))).EntireColumn

r1.Insert shift:=xlToRight

Cells(1, r1.Column - 1) = v(i, UBound(v, 2))

End If

Next

j = 1

For Each cell In Range("A1:AZ1")

If Len(Trim(cell)) = 0 Then

cell.Value = "DUM" & j

j = j + 1

End If

Next

End Sub

The altered code is:

Sub rearrangecolumns()

Dim s As String, v As Variant, r As Range, cell As Range

Dim i As Long, r1 As Range

s = "{""A"",""Name"";""B"",""Year"";""C"",""Gender"";" & _

"""D"",""test"";""E"",""test2"";""F"",""FSM"";""G"",""Ethnicity"";""H"",""DOB"";""I"",""Age"";""J"",""Ranked Need"";""K"",""EAL"";""L"",""GandT"";""M"",""Tutor Group"";""N"","""";""O"",""Base"";""P"",""Attendance"";""Q"",""PPI"";""R"","""";""S"",""Group"";""T"",""KS2 ENG SAT"";""U"",""KS2 MAT SAT"";""V"",""KS2 Average pts"";""W"",""Target"";""X"",""Best Performance"";""Y"",""Reading age"";""Z"",""Spelling age"";""AA"",""PPI"";""AB"",""AUT 1"";""AC"",""AUT 2"";""AD"",""SPR 1"";""AE"",""SPR 2"";""AF"",""SUM 1"";""AG"",""SUM 2"";""AH"","""";""AI"","""";""AJ"","""";""AK"","""";""AL"","""";""AM"","""";""AN"","""";""AO"","""";""AP"","""";""AQ"","""";""AR"","""";""AS"","""";""AT"","""";""AU"","""";""AV"","""";""AW"","""";""AX"","""";""AY"","""";""AZ"",""""}"

v = Evaluate(s)

'For i = LBound(v, 1) To UBound(v, 1)

' Debug.Print v(i, 1), v(i, 2)

'Next

For i = LBound(v, 1) To UBound(v, 1)

Set r = Range("A1", Cells(1, Columns.Count).End(xlToLeft))

If Len(Trim(v(i, UBound(v, 2)))) <> 0 Then

bFound = False

For Each cell In r

If LCase(cell.Value) = LCase(v(i, UBound(v, 2))) Then

Set r1 = Cells(1, v(i, LBound(v, 1))).EntireColumn

cell.EntireColumn.Copy

r1.Insert shift:=xlToRight

cell.EntireColumn.Delete

bFound = True

Exit For

End If

Next

End If

If Not bFound Then

Set r1 = Cells(1, v(i, LBound(v, 1))).EntireColumn

r1.Insert shift:=xlToRight

Cells(1, r1.Column - 1) = v(i, UBound(v, 2))

End If

Next

j = 1

For Each cell In Range("A1:AZ1")

If Len(Trim(cell)) = 0 Then

cell.Value = "DUM" & j

j = j + 1

End If

Next

End Sub

The section that I altered is:

"{""A"",""Name"";""B"",""Year"";""C"",""Gender"";" & _

"""D"",""test"";""E"",""test2"";""F"",""FSM"";""G"",""Ethnicity"";""H"",""DOB"";""I"",""Age"";""J"",""Ranked Need"";""K"",""EAL"";""L"",""GandT"";""M"",""Tutor Group"";""N"","""";""O"",""Base"";""P"",""Attendance"";""Q"",""PPI"";""R"","""";""S"",""Group"";""T"",""KS2 ENG SAT"";""U"",""KS2 MAT SAT"";""V"",""KS2 Average pts"";""W"",""Target"";""X"",""Best Performance"";""Y"",""Reading age"";""Z"",""Spelling age"";""AA"",""PPI"";""AB"",""AUT 1"";""AC"",""AUT 2"";""AD"",""SPR 1"";""AE"",""SPR 2"";""AF"",""SUM 1"";""AG"",""SUM 2"";""AH"","""";""AI"","""";""AJ"","""";""AK"","""";""AL"","""";""AM"","""";""AN"","""";""AO"","""";""AP"","""";""AQ"","""";""AR"","""";""AS"","""";""AT"","""";""AU"","""";""AV"","""";""AW"","""";""AX"","""";""AY"","""";""AZ"",""""}"

Could you tell me what I have done wrong?

Thanks in advance

Chris Mitchell

Christopher Mitchell,

the method I used to build the two dimensional array was a bit of a trick. It is only good for a string length of 255 characters and you have exceeded that.

Here is a revision to the code to do what you want. I have made a function called buildarray where you would set up you column organization.

I have set it up as I read you string in your posting.

Sub rearrangecolumns()

Dim s As String, v As Variant, r As Range, cell As Range

Dim i As Long, r1 As Range

v = BuildArray()

'For i = LBound(v, 1) To UBound(v, 1)

' Debug.Print v(i, 1), v(i, 2)

'Next

For i = LBound(v, 1) To UBound(v, 1)

Set r = Range("A1", Cells(1, Columns.Count).End(xlToLeft))

If Len(Trim(v(i, UBound(v, 2)))) <> 0 Then

bFound = False

For Each cell In r

If LCase(cell.Value) = LCase(v(i, UBound(v, 2))) Then

Set r1 = Cells(1, v(i, LBound(v, 1))).EntireColumn

cell.EntireColumn.Copy

r1.Insert shift:=xlToRight

cell.EntireColumn.Delete

bFound = True

Exit For

End If

Next

End If

If Not bFound Then

Set r1 = Cells(1, v(i, LBound(v, 1))).EntireColumn

r1.Insert shift:=xlToRight

Cells(1, r1.Column - 1) = v(i, UBound(v, 2))

End If

Next

j = 1

For Each cell In Range("A1:AZ1")

If Len(Trim(cell)) = 0 Then

cell.Value = "DUM" & j

j = j + 1

End If

Next

End Sub

Function BuildArray()

Dim v(1 To 52, 1 To 2)

v(1, 1) = "A": v(1, 2) = "Name"

v(2, 1) = "B": v(2, 2) = "Year"

v(3, 1) = "C": v(3, 2) = "Gender"

v(4, 1) = "D": v(4, 2) = "test"

v(5, 1) = "E": v(5, 2) = "test2"

v(6, 1) = "F": v(6, 2) = "FSM"

v(7, 1) = "G": v(7, 2) = "Ethnicity"

v(8, 1) = "H": v(8, 2) = "DOB"

v(9, 1) = "I": v(9, 2) = "Age"

v(10, 1) = "J": v(10, 2) = "Ranked Need"

v(11, 1) = "K": v(11, 2) = "EAL"

v(12, 1) = "L": v(12, 2) = "GandT"

v(13, 1) = "M": v(13, 2) = "Tutor Group"

v(14, 1) = "N": v(14, 2) = ""

v(15, 1) = "O": v(15, 2) = "Base"

v(16, 1) = "P": v(16, 2) = "Attendance"

v(17, 1) = "Q": v(17, 2) = "PPI"

v(18, 1) = "R": v(18, 2) = ""

v(19, 1) = "S": v(19, 2) = "Group"

v(20, 1) = "T": v(20, 2) = "KS2 ENG SAT"

v(21, 1) = "U": v(21, 2) = "KS2 MAT SAT"

v(22, 1) = "V": v(22, 2) = "KS2 Average pts"

v(23, 1) = "W": v(23, 2) = "Target"

v(24, 1) = "X": v(24, 2) = "Best Peformance"

v(25, 1) = "Y": v(25, 2) = "Reading Age"

v(26, 1) = "Z": v(26, 2) = "Spelling Age"

v(27, 1) = "AA": v(27, 2) = "PPI"

v(28, 1) = "AB": v(28, 2) = "AUT 1"

v(29, 1) = "AC": v(29, 2) = "AUT 2"

v(30, 1) = "AD": v(30, 2) = "SPR 1"

v(31, 1) = "AE": v(31, 2) = "SPR 2"

v(32, 1) = "AF": v(32, 2) = "SUM 1"

v(33, 1) = "AG": v(33, 2) = "SUM 2"

v(34, 1) = "AH": v(34, 2) = ""

v(35, 1) = "AI": v(35, 2) = ""

v(36, 1) = "AJ": v(36, 2) = ""

v(37, 1) = "AK": v(37, 2) = ""

v(38, 1) = "AL": v(38, 2) = ""

v(39, 1) = "AM": v(39, 2) = ""

v(40, 1) = "AN": v(40, 2) = ""

v(41, 1) = "AO": v(41, 2) = ""

v(42, 1) = "AP": v(42, 2) = ""

v(43, 1) = "AQ": v(43, 2) = ""

v(44, 1) = "AR": v(44, 2) = ""

v(45, 1) = "AS": v(45, 2) = ""

v(46, 1) = "AT": v(46, 2) = ""

v(47, 1) = "AU": v(47, 2) = ""

v(48, 1) = "AV": v(48, 2) = ""

v(49, 1) = "AW": v(49, 2) = ""

v(50, 1) = "AX": v(50, 2) = ""

v(51, 1) = "AY": v(51, 2) = ""

v(52, 1) = "AZ": v(52, 2) = ""

BuildArray = v

End Function

--

Regards,

Tom Ogilvy

- Add to this Answer
- Ask a Question

Rating(1-10) | Knowledgeability = 10 | Clarity of Response = 10 | Politeness = 10 |

Comment | It seems everything is possible with code. Thank you very very much for your time patience and undoubted expertise Chris |

This topic answers questions related to Microsoft Excel spreadsheet (or workbook) stand-alone or Mircrosoft Office Excel including Excel 2003, Excel 2007, Office 2000, and Office XP. You can get Excel help on Excel formulas(or functions), Excell macros, charting in Excel, advanced features, and the general use of Excel. This does not provide a general Excel tutorial nor the basics of using a spreadsheet. It provides specific answers to using Microsoft Excel only. If you do not see your Excel question answered in this area then please ask an Excel question here

Excel

Answers by Expert:

Selected as an Excel MVP by Microsoft since 1999. Answering Excel questions in Allexperts since its inception in 2001. Able to answer questions on almost all aspects of Excel's internal capabilities. If seeking a VBA solution, please specify that in your question itself so I give you the answer you want. [Excel has weak protection - if you are distributing an application, I don't answer questions on how to protect your project from your users.]

Extensive experience. **Education/Credentials**

Master of Science (MS) degree Operations Research (ORSA)**Awards and Honors**

Microsoft MVP in Excel.