Excel/Altering VBA

Advertisement


Question
Hi Tom

The following piece of code will search a range of data; look for the column headings in the code and rearrange them according to the order specified in the code.

For column “D” I have written “test” and for column “E” I have written “test2”.  However “test” and “test2” do not exist as column headings in the original dataset.  So, when I run the code, the new column heading for “D” and “E” will be blank instead of “test” and “test2”.

The dataset informs a pivot table; if there are any blank column headings in the source data,  I will get an error message.

Can the code be altered so that no column heading, between (A1:AZ1) is left blank after the code is run?

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
   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
       Exit For
     End If
   Next
 Else
    Set r1 = Cells(1, v(i, LBound(v, 1))).EntireColumn
    r1.Insert shift:=xlToRight
 End If
Next
End Sub

Thanks in advance



Chris Mitchell

Answer
Christopher Mitchell,

Try this and see if it does what you want:

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

--
Regards,
Tom Ogilvy

About Excel
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

All Answers


Answers by Expert:


Ask Experts

Volunteer


Tom Ogilvy

Expertise

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.]

Experience

Extensive experience.

Education/Credentials
Master of Science (MS) degree Operations Research (ORSA)

Awards and Honors
Microsoft MVP in Excel.

©2016 About.com. All rights reserved.