Link to home
Start Free TrialLog in
Avatar of bsharath
bsharathFlag for India

asked on

Sort excel sheet as per the text file.

Hi,

I have some names in txt file need to sort the same order as the excel file.Data is in an excel sheet.If any name is not found in the excel sheet the leave a row blank as per the txt file.

Regards
Sharath
Avatar of William Elliott
William Elliott
Flag of United States of America image

can you provide more information?
a few example lines form the text file and a few example lines form the excel file,...

or uploade a sample text and excel file and say specifically what you want accomplished.

Avatar of bsharath

ASKER

In the text file i have

Sharath
Ramesh
Sureshr
      Blank space
Mahesh

Excel I have
Ramesh
Suresh
Mahesh
Sathish
Sharath
Output should be as in the excel
Sharath
Ramesh
Sureshr
      Blank space (As the text file has a blank so the data in the excel the whole row should be blank)
Mahesh
Sathish  (This name is not there in the txt file so all names which are not there in the txt file has to be made colored.)

Sorry if i have not mentioned.There would be data in the start and end of the names.For ex:
EMP ID  Name  Email ID                     Age   Sex
1536   Sharath   Sharat@plc.com   30   M
So when the excel sorts as per the txt file all the contents next to sharath has to be sorted as well.

THANKS
Sharath
'let me know if this works for you if not can you upload ur excel file at filepatio in '.xls format
Sub Macro1()
Dim FF As Integer, str1 As String, j As Integer, idfound As Boolean
s2row = 2
j = 1  'Change the column no here

  For i = 1 To 13 + Sheet1.UsedRange.Rows.Count
           FF = FreeFile
           Open "C:\filename.txt" For Input As #FF
           Do While Not EOF(FF)
                Line Input #FF, str1
                idfound = False
               If UCase(str1) = UCase(Sheet1.Cells(i, j)) And Sheet1.Cells(i, j) <> "" Then
                idfound = True
                Exit Do
              End If
            Loop
           Close FF
 
  If idfound = False And Sheet1.Cells(i, j) <> "" Then
             Sheet1.Rows(i).Cut Destination:=Sheet2.Rows(s2row)
             Sheet1.Rows(i).Insert (xlDown)
            s2row = s2row + 1
  End If


     
 Next
 FF = FreeFile
 Open "C:\filename.txt" For Input As #FF
s1row = 1
 While Not EOF(FF)
      Line Input #FF, str1
      if str1="" then
             Sheet1.Rows(s1row).Insert (xlDown)
      Else
         For i = 1 To Sheet1.UsedRange.Rows.Count

           If UCase(str1) = UCase(Sheet1.Cells(i, j)) And Sheet1.Cells(i, j) <> "" Then
                Sheet1.Rows(s1row).Copy Destination:=Sheet2.Rows(55555)
                Sheet1.Rows(i).Copy Destination:=Sheet1.Rows(s1row)
                Sheet2.Rows(55555).Cut Destination:=Sheet1.Rows(i)
                s1row = s1row + 1
            End If
        Next
      End if
Wend
 Close FF

End Sub
Hitesh is this the same macro as you gave me befor.This does not sort blanks according to the txt file.
'yes it is the same i guess because the questions are basically the same.
'let me know if this one works fine
Sub Macro1()
Dim FF As Integer, str1 As String, j As Integer, idfound As Boolean
s2row = 2

j = Val(InputBox("Enter Column No"))  'Take input from user

  For i = 1 To 13 + Sheet1.UsedRange.Rows.Count
           FF = FreeFile
           Open "C:\mc.txt" For Input As #FF
           Do While Not EOF(FF)
                Line Input #FF, str1
                idfound = False
               If UCase(str1) = UCase(Sheet1.Cells(i, j)) And Sheet1.Cells(i, j) <> "" Then
                idfound = True
                Exit Do
              End If
            Loop
           Close FF
 
  If idfound = False And Sheet1.Cells(i, j) <> "" Then
             Sheet1.Rows(i).Cut Destination:=Sheet2.Rows(s2row)
             Sheet1.Rows(i).Insert (xlDown)
            s2row = s2row + 1
  End If


     
 Next
 FF = FreeFile
 Open "C:\mc.txt" For Input As #FF
s1row = 13
 While Not EOF(FF)
      Line Input #FF, str1
      If Trim(str1) = "" Then
            Sheet1.Rows(s1row).Insert (xlDown)
      Else
         For i = 1 To Sheet1.UsedRange.Rows.Count

           If UCase(str1) = UCase(Sheet1.Cells(i, j)) And Sheet1.Cells(i, j) <> "" Then
                Sheet1.Rows(s1row).Copy Destination:=Sheet2.Rows(55555)
                Sheet1.Rows(i).Copy Destination:=Sheet1.Rows(s1row)
                Sheet2.Rows(55555).Cut Destination:=Sheet1.Rows(i)
                s1row = s1row + 1
            End If
        Next
     End If
Wend
 Close FF

End Sub


Avatar of dirknibleck
dirknibleck

I don't get this one. You want it sorted according to the Excel file, but somehow you want the blanks to remain where they are in the text file?
You Have given me a way previously to do this with a excel sheet.I want the same thing for txt file.

Sort the txt file as per the excel sheet.If the excel has a blank row then leave a blank line in the txt file.
ANy help on this excel Q...

Any of you experts can help.Very urgent please...
ASKER CERTIFIED SOLUTION
Avatar of dirknibleck
dirknibleck

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial