如何使用Excel表格对Excel工作表中的行进行排序并打印到文件?

用户名

我有一张叫学生的桌子。我正在尝试根据他的成绩打印相应的学生姓名。我想使用Excel宏打印到文件。

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

在此处输入图片说明

我想在VB中编写函数以将对应的行作为每个主题(0,1,2,3,... 7)进行排序,还将值打印到文件中。

输出(file.txt)

NULL,NULL,NULL,NULL,NULL
STUDENT 2 ,STUDENT 5 ,STUDENT 4 ,NULL,NULL
STUDENT 4,STUDENT 5,NULL,NULL,NULL
etc.. 

行中的列应按升序排序,如果任何列中的“-”应作为剩余值打印为NULL。

我已经写了

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

结果:

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何将多个Excel工作表打印到一个PDF文件中?

如何使用openpyxl在工作簿中对Excel工作表/标签进行排序

如何对Excel表中的数据行进行计数

报表生成器:将表结果打印到Excel中的不同工作表中

按列数对Excel电子表格中的所有行进行排序

如何使用Excel VBA打印到PDF?

如何将T-1日期值打印到Excel工作表

无法使用C#对excel工作表进行排序

Python打印到Excel文件

如何使用pandas对excel文件中的数据进行排序。并对重复项进行排序

使用VBA在Excel中对表格进行排序?

如何在Google表格中对行进行排序

如何根据Google表格中的背景颜色对行进行排序

使用python在Excel中对行进行分组

如何使用Javascript对表格行进行排序

Excel:如何按相同的列值对行进行排序

如何让Excel正确地对行进行重新排序?

Apache-POI在Excel中对行进行排序

如何使用 VBA 删除 Excel 工作表中除表格之外的所有内容?

对表格中的行进行排序

如何对分区中的表中的行进行排序?

如何从文件中读取数组,对其进行排序,然后将其打印到另一个文件中?

使用 VBA 在 Excel 文件的工作表之间进行循环

如何使用 Excel VBA 从使用文件浏览器选择的工作簿中复制工作表?

如何对MySQL表中的记录/行进行重新排序

Excel:基于多个列对行进行排序

在C#中,使用Spire.Xls,使用折叠/展开选项在折叠行上方对excel工作表行进行分组

在 Excel 中打印特定工作表

如何在BASH中对文件行进行排序