如何使用VB脚本对excel工作表中的行进行排序并打印到文件中?

如何使用VB脚本对excel工作表中的行进行排序并打印到文件中?,excel,vba,Excel,Vba,我有一张名为student的桌子。我正试图打印出与他的分数相对应的学生姓名。我想使用Excel宏打印到文件 我的表格包含标题,如学生姓名1、2、3等。。相应的标记为1、2、3、4和“-” 我想在VB中编写函数,将对应的行按每个主题(0,1,2,3,…7)排序,并将值打印到文件中 输出(file.txt) 行中的列应按升序排序,如果任何列中出现“-”,则应将其打印为空值作为剩余值 我写过 Sub test() 'create and write into file txt Dim fso As

我有一张名为student的桌子。我正试图打印出与他的分数相对应的学生姓名。我想使用Excel宏打印到文件

我的表格包含标题,如学生姓名1、2、3等。。相应的标记为1、2、3、4和“-”

我想在VB中编写函数,将对应的行按每个主题(0,1,2,3,…7)排序,并将值打印到文件中

输出(file.txt)

行中的列应按升序排序,如果任何列中出现“-”,则应将其打印为空值作为剩余值

我写过

Sub test()
'create and write into file txt
Dim fso As Object

Set fso = CreateObject("Scripting.FileSystemObject")

    Dim Fileout As Object
    Set Fileout = fso.CreateTextFile("MyFile.txt", True, True)

    'Write logic for sorting 

    Fileout.Close

End Sub

如何使用VB脚本在excel中排序并打印这些行

以下是一种实现方法,根据您的需要调整它:

' Naive O(N^2) sort
Sub Sort(arr() As Long, students() As String)
    If UBound(arr) <= 1 Then Exit Sub

    Dim i As Long, j As Long
    For i = 0 To UBound(arr) - 1
        ' Look for the minimum index in the sub-array going from i
        Dim indexOfMin As Long
        indexOfMin = i
        For j = i To UBound(arr)
            If arr(j) < arr(indexOfMin) Then
                indexOfMin = j
            End If
        Next j
        ' Put the minimum mark at the beginning of the sub-array
        Dim tmp As Variant
        tmp = arr(i)
        arr(i) = arr(indexOfMin)
        arr(indexOfMin) = tmp
        ' Put the student with the minimum value at the beginning of the students sub-array
        tmp = students(i)
        students(i) = students(indexOfMin)
        students(indexOfMin) = tmp
    Next i
End Sub

Sub SortAndSave()
    Dim dataRange As Range
    Set dataRange = Range("A1:F9")

    Dim data As Variant
    data = dataRange.Value

    Dim NSubject As Long, NStudents As Long
    NSubject = UBound(data, 1) - 1
    NStudents = UBound(data, 2) - 1

    Dim text As String
    Dim i As Long, j As Long
    For i = 1 To NSubject
        ' Read marks and students names
        Dim subjectMarks() As Long
        ReDim subjectMarks(0 To NStudents - 1)
        Dim students() As String
        ReDim students(0 To NStudents - 1)
        For j = 1 To NStudents
            ' Use a big enough number 999 so that students with no mark will be pushed to the end
            subjectMarks(j - 1) = IIf(data(i + 1, j + 1) <> "-", data(i + 1, j + 1), 999)
            students(j - 1) = data(1, j + 1)
        Next j

        ' Sort marks and students
        Sort subjectMarks, students

        ' Build display row for subject
        Dim row As String
        row = ""
        For j = 1 To NStudents
            ' If there is a mark render the student name
            If subjectMarks(j - 1) <> 999 Then
                row = row & students(j - 1)
            ' Otherwise render NULL
            Else
                row = row & "NULL"
            End If
            ' Add a comma if not the latest
            If j <> NStudents Then
                row = row & ","
            End If
        Next j
        text = text & row
        ' Add a \r\n if not the latest
        If i <> NSubject Then
            text = text & vbCrLf
        End If
    Next i
End Sub

您想要VBScript还是VBA?它们与Excel的VB脚本不同,因为你提到了Excel宏,你的示例代码是VBA,我假设你指的是VBA,而不是VBScript。如果不是这样的话,您可以将其还原。此答案使用VBA,而不是VBScript。事实上,我得出了与您相同的结论:OP需要VBA。:)否则他会告诉我们…我想要VBA。谢谢你宝贵的帮助support@Pragmateek运行时,在行subjectMarks(j-1)=IIf(数据(i+1,j+1)“-”,数据(i+1,j+1),999)中获取“类型不匹配错误”script@user2986042您的数据范围中可能有错误的值,即既不是
-
也不是整数。
' Naive O(N^2) sort
Sub Sort(arr() As Long, students() As String)
    If UBound(arr) <= 1 Then Exit Sub

    Dim i As Long, j As Long
    For i = 0 To UBound(arr) - 1
        ' Look for the minimum index in the sub-array going from i
        Dim indexOfMin As Long
        indexOfMin = i
        For j = i To UBound(arr)
            If arr(j) < arr(indexOfMin) Then
                indexOfMin = j
            End If
        Next j
        ' Put the minimum mark at the beginning of the sub-array
        Dim tmp As Variant
        tmp = arr(i)
        arr(i) = arr(indexOfMin)
        arr(indexOfMin) = tmp
        ' Put the student with the minimum value at the beginning of the students sub-array
        tmp = students(i)
        students(i) = students(indexOfMin)
        students(indexOfMin) = tmp
    Next i
End Sub

Sub SortAndSave()
    Dim dataRange As Range
    Set dataRange = Range("A1:F9")

    Dim data As Variant
    data = dataRange.Value

    Dim NSubject As Long, NStudents As Long
    NSubject = UBound(data, 1) - 1
    NStudents = UBound(data, 2) - 1

    Dim text As String
    Dim i As Long, j As Long
    For i = 1 To NSubject
        ' Read marks and students names
        Dim subjectMarks() As Long
        ReDim subjectMarks(0 To NStudents - 1)
        Dim students() As String
        ReDim students(0 To NStudents - 1)
        For j = 1 To NStudents
            ' Use a big enough number 999 so that students with no mark will be pushed to the end
            subjectMarks(j - 1) = IIf(data(i + 1, j + 1) <> "-", data(i + 1, j + 1), 999)
            students(j - 1) = data(1, j + 1)
        Next j

        ' Sort marks and students
        Sort subjectMarks, students

        ' Build display row for subject
        Dim row As String
        row = ""
        For j = 1 To NStudents
            ' If there is a mark render the student name
            If subjectMarks(j - 1) <> 999 Then
                row = row & students(j - 1)
            ' Otherwise render NULL
            Else
                row = row & "NULL"
            End If
            ' Add a comma if not the latest
            If j <> NStudents Then
                row = row & ","
            End If
        Next j
        text = text & row
        ' Add a \r\n if not the latest
        If i <> NSubject Then
            text = text & vbCrLf
        End If
    Next i
End Sub
NULL,NULL,NULL,NULL,NULL
STUDENT 2,STUDENT 5,STUDENT 4,NULL,NULL
STUDENT 4,STUDENT 5,NULL,NULL,NULL
STUDENT 4,STUDENT 5,NULL,NULL,NULL
STUDENT 2,STUDENT 5,STUDENT 4,NULL,NULL
STUDENT 5,STUDENT 4,STUDENT 2,STUDENT 1,NULL
STUDENT 5,STUDENT 4,STUDENT 2,STUDENT 1,NULL
STUDENT 4,STUDENT 5,NULL,NULL,NULL