vba excel code for copying data from one workbook to another based on criteria

sapnadhankhar

New Member
Joined
Aug 8, 2015
Messages
2
Dear all,
Greeting!!!
i need vba code for to write the excel in desired format.

present excel file
name firstname
abc dfe a d g h
sdf the d g h t
dft tyr h a t d

desired format
name firstname a d g h t
abc dfe a d g h
sdf the d g h t
dft tyr a d h t

where there are coulmns named name,firtsname,a,d,g,h,t.data of each row is such that entry of data a is in the column a.and data of d dat is in column named d and so on.
please help.i am in great need of this format.i have so much hope from you my dear friends.
thanks
regards
sapna
 
Last edited:
Hello,

Sorry, was going to ask a question to which I figured out the answer.
 
Last edited:
Upvote 0
Hello,

does this work as expected?

Code:
Sub SORT_OUT()
    Application.ScreenUpdating = False
    For MY_ROWS = 2 To Range("A" & Rows.Count).End(xlUp).Row
        With Range("C" & MY_ROWS & ":G" & MY_ROWS)
            .Sort Key1:=Range("C" & MY_ROWS), Order1:=xlAscending, Header:=xlGuess, _
                OrderCustom:=1, MatchCase:=False, Orientation:=xlLeftToRight
        End With
        For MY_COLS = 3 To 7
            If MY_COLS = 3 And Cells(MY_ROWS, MY_COLS).Value <> "a" Then
                Cells(MY_ROWS, MY_COLS).Insert (xlToRight)
            End If
            If MY_COLS = 4 And Cells(MY_ROWS, MY_COLS).Value <> "d" Then
                Cells(MY_ROWS, MY_COLS).Insert (xlToRight)
            End If
            If MY_COLS = 5 And Cells(MY_ROWS, MY_COLS).Value <> "g" Then
                Cells(MY_ROWS, MY_COLS).Insert (xlToRight)
            End If
            If MY_COLS = 6 And Cells(MY_ROWS, MY_COLS).Value <> "h" Then
                Cells(MY_ROWS, MY_COLS).Insert (xlToRight)
            End If
            If MY_COLS = 7 And Cells(MY_ROWS, MY_COLS).Value <> "t" Then
                Cells(MY_ROWS, MY_COLS).Insert (xlToRight)
            End If
        Next MY_COLS
    Next MY_ROWS
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Hello,

does this work as expected?

Code:
Sub SORT_OUT()
    Application.ScreenUpdating = False
    For MY_ROWS = 2 To Range("A" & Rows.Count).End(xlUp).Row
        With Range("C" & MY_ROWS & ":G" & MY_ROWS)
            .Sort Key1:=Range("C" & MY_ROWS), Order1:=xlAscending, Header:=xlGuess, _
                OrderCustom:=1, MatchCase:=False, Orientation:=xlLeftToRight
        End With
        For MY_COLS = 3 To 7
            If MY_COLS = 3 And Cells(MY_ROWS, MY_COLS).Value <> "a" Then
                Cells(MY_ROWS, MY_COLS).Insert (xlToRight)
            End If
            If MY_COLS = 4 And Cells(MY_ROWS, MY_COLS).Value <> "d" Then
                Cells(MY_ROWS, MY_COLS).Insert (xlToRight)
            End If
            If MY_COLS = 5 And Cells(MY_ROWS, MY_COLS).Value <> "g" Then
                Cells(MY_ROWS, MY_COLS).Insert (xlToRight)
            End If
            If MY_COLS = 6 And Cells(MY_ROWS, MY_COLS).Value <> "h" Then
                Cells(MY_ROWS, MY_COLS).Insert (xlToRight)
            End If
            If MY_COLS = 7 And Cells(MY_ROWS, MY_COLS).Value <> "t" Then
                Cells(MY_ROWS, MY_COLS).Insert (xlToRight)
            End If
        Next MY_COLS
    Next MY_ROWS
    Application.ScreenUpdating = True
End Sub


Thanks for reply.But i want it to make generalized means it automatically count no. of columns,automatically see the value of columns and arrange it in alphabetically.we need not to compare it with "a" or "h".
thanks & regards
sapna
 
Upvote 0
Hello,

As I understand you just want to sort each row alphabeteically?

Code:
Sub SORT_OUT()
    Application.ScreenUpdating = False
    For MY_ROWS = 2 To Range("A" & Rows.Count).End(xlUp).Row
        With Range("C" & MY_ROWS & ":G" & MY_ROWS)
            .Sort Key1:=Range("C" & MY_ROWS), Order1:=xlAscending, Header:=xlGuess, _
                OrderCustom:=1, MatchCase:=False, Orientation:=xlLeftToRight
        End With
    Next MY_ROWS
    Application.ScreenUpdating = True
End Sub

This will do that, or did I mis-understand you?
 
Upvote 0

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