VBA Find - Move macro

Norbert

New Member
Joined
May 4, 2011
Messages
7
Hello,

I have an Excel file that has columns in random order. The order I would like to have them in is listed under 'MyArr.' The problem is that not all of the column names which are listed in 'MyArr' are in the file. They are also in random order.

The macro below seems to be a good solution. However, when I run it I always get a “Runtime Error ’91’: Object Variable or With block variable not set.” The error occurs on the line highlighted in red(TargetCol = Cells.Find(What:=MyArr(i)).Column).<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:eek:ffice:eek:ffice" /><o:p></o:p> I think that I might need some error checking in the case it doesn’t find the column. If it doesn't find the column, I want to insert a blank column.

Any help would be greatly appreciated. BTW, I am using Excel 2007.

Thank you,
Norbert




Sub Find_Move()<o:p></o:p>
'<o:p></o:p>
' Find_Move Macro<o:p></o:p>
'<o:p></o:p>
Range("A1").Select<o:p></o:p>
<o:p></o:p>
Dim MyArr As Variant<o:p></o:p>
Dim ColArr As Variant<o:p></o:p>
Dim TargetCol As Long<o:p></o:p>
<o:p></o:p>

MyArr = Array("StudentID[0]", "Term[0]", "Prefix[0]", "Last_Name[0]", "First_Name[0]", "Gender[0]", "Date_of_Birth[0]", "Home_address[0]", "City[0]", "State[0]", "ZIP[0]", "Country[0]", "Phone_1[0]", "Phone_2[0]", "E-Mail[0]", "Special_Needs[0]", "Medical_Conditions[0]", "Emergency_Contact_Name[0]", "Relationship[0]", "Address1[0]", "Address2[0]", "Phone_Number[0]", "Hall[0]", "GlobalLivingCommunity[0]", "HealthyLifestylesCommunity[0]", "QuietHours[0]", "Roommate_1ID[0]", "Roommate_1[0]", "Roommate_2ID[0]", "Roommate_2[0]", "Roommate_3ID[0]", "Roommate_3[0]", "Roommate_4ID[0]", "Roommate_4[0]", "WakeUpEarly[0]", "GoToBedLate[0]", "StudyMorning[0]", "StudyAfternoon[0]", "StudyNight[0]", "StudyWith[0]", "MyRoomIsMostly[0]", "Smoker[0]", "My_Academic_Program_Major[0]", "HobbiesAndComments[0]", "MealPlan[0]", "HealthConcern[0]", "SignatureDate[0]", "TermsAndConditions[0]", "Theft[0]", "Obligation[0]", "AgreeToTerms[0]", "LIU-Student[0]") <o:p></o:p>


ColArr = Array("A:A", "B:B", "C:C", "D:D", "E:E", "F:F", "G:G", "H:H", "I:I", "J:J", "K:K", "L:L", "M:M", "N:N", "O:O", "P:P", "Q:Q", "R:R", "S:S", "T:T", "U:U", "V:V", "W:W", "X:X", "Y:Y", "Z:Z", "AA:AA", "AB:AB", "AC:AC", "AD:AD", "AE:AE", "AF:AF", "AG:AG", "AH:AH", "AI:AI", "AJ:AJ", "AK:AK", "AL:AL", "AM:AM", "AN:AN", "AO:AO", "AP:AP", "AQ:AQ", "AR:AR", "AS:AS", "AT:AT", "AU:AU", "AV:AV", "AW:AW", "AX:AX", "AY:AY", "AZ:AZ", "BA:BA", "BB:BB", "BC:BC", "BD:BD", "BE:BE", "BF:BF", "BG:BG", "BH:BH") ' Destination column
<o:p></o:p>
<o:p></o:p>

For i = LBound(MyArr) To UBound(MyArr)<o:p></o:p>
TargetCol = Cells.Find(What:=MyArr(i)).Column
Columns(TargetCol).Cut Destination:=Columns(ColArr(i))<o:p></o:p>
Next<o:p></o:p>
<o:p></o:p>
End Sub<o:p></o:p>
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Perhaps like this

Code:
Sub Find_Move()
'
' Find_Move Macro
'
Range("A1").Select

Dim MyArr As Variant
Dim ColArr As Variant
Dim TargetCol As Long
Dim Found As Range

MyArr = Array("StudentID[0]", "Term[0]", "Prefix[0]", "Last_Name[0]", "First_Name[0]", "Gender[0]", "Date_of_Birth[0]", "Home_address[0]", "City[0]", "State[0]", "ZIP[0]", "Country[0]", "Phone_1[0]", "Phone_2[0]", "E-Mail[0]", "Special_Needs[0]", "Medical_Conditions[0]", "Emergency_Contact_Name[0]", "Relationship[0]", "Address1[0]", "Address2[0]", "Phone_Number[0]", "Hall[0]", "GlobalLivingCommunity[0]", "HealthyLifestylesCommunity[0]", "QuietHours[0]", "Roommate_1ID[0]", "Roommate_1[0]", "Roommate_2ID[0]", "Roommate_2[0]", "Roommate_3ID[0]", "Roommate_3[0]", "Roommate_4ID[0]", "Roommate_4[0]", "WakeUpEarly[0]", "GoToBedLate[0]", "StudyMorning[0]", "StudyAfternoon[0]", "StudyNight[0]", "StudyWith[0]", "MyRoomIsMostly[0]", "Smoker[0]", "My_Academic_Program_Major[0]", "HobbiesAndComments[0]", "MealPlan[0]", "HealthConcern[0]", "SignatureDate[0]", "TermsAndConditions[0]", "Theft[0]", "Obligation[0]", "AgreeToTerms[0]", "LIU-Student[0]")


ColArr = Array("A:A", "B:B", "C:C", "D:D", "E:E", "F:F", "G:G", "H:H", "I:I", "J:J", "K:K", "L:L", "M:M", "N:N", "O:O", "P:P", "Q:Q", "R:R", "S:S", "T:T", "U:U", "V:V", "W:W", "X:X", "Y:Y", "Z:Z", "AA:AA", "AB:AB", "AC:AC", "AD:AD", "AE:AE", "AF:AF", "AG:AG", "AH:AH", "AI:AI", "AJ:AJ", "AK:AK", "AL:AL", "AM:AM", "AN:AN", "AO:AO", "AP:AP", "AQ:AQ", "AR:AR", "AS:AS", "AT:AT", "AU:AU", "AV:AV", "AW:AW", "AX:AX", "AY:AY", "AZ:AZ", "BA:BA", "BB:BB", "BC:BC", "BD:BD", "BE:BE", "BF:BF", "BG:BG", "BH:BH") ' Destination column



For i = LBound(MyArr) To UBound(MyArr)

    Set Found = Cells.Find(What:=MyArr(i))
    If Not Found Is Nothing Then
        TargetCol = Found.Column
        Columns(TargetCol).Cut Destination:=Columns(ColArr(i))
    Else
        'action if header not found
    End If

Next

End Sub
 
Upvote 0
Try this.
Code:
Set rngFnd = Cells.Find(What:=MyArr(i))
 
If rngFnd Is Nothing
    ' header not found
   Columns(ColArr(i)).EntireRow.Insert
Else
   Columns(rngFnd.Column).Cut Destination:=Columns(ColArr(i))
End If
 
Upvote 0
Thanks Peter. I used a combination of Norie's and your reply and it worked like a charm. Thanks again for the quick reply.
 
Upvote 0
Thanks Norie. I used a combination of Peter's and your reply and it worked like a charm. Thanks again for the quick reply.
 
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,119
Members
451,399
Latest member
alchavar

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