DeepSeek合并Excel数据

:有一个excel表格,有两个表单分别是语文成绩和数学成绩,每个表从第一列起依次是学号、姓名、成绩,请帮我写一段VBA,把以上两个表中成绩合成到第三个表中,要求相同学号、姓名的同学信息合成一行。

DeepSeek:
你可以使用以下VBA代码将“语文成绩”和“数学成绩”两个表单中的成绩合并到第三个表单中。假设第三个表单名为“合并成绩”,代码会将相同学号和姓名的学生信息合并到一行。

Sub MergeScores()
    Dim wsChinese As Worksheet
    Dim wsMath As Worksheet
    Dim wsMerged As Worksheet
    Dim lastRowChinese As Long
    Dim lastRowMath As Long
    Dim lastRowMerged As Long
    Dim i As Long, j As Long
    Dim studentId As String
    Dim studentName As String
    Dim chineseScore As Variant
    Dim mathScore As Variant
    Dim found As Boolean
    
    ' 设置工作表
    Set wsChinese = ThisWorkbook.Sheets("语文成绩")
    Set wsMath = ThisWorkbook.Sheets("数学成绩")
    Set wsMerged = ThisWorkbook.Sheets("合并成绩")
    
    ' 获取语文成绩表的最后一行
    lastRowChinese = wsChinese.Cells(wsChinese.Rows.Count, 1).End(xlUp).Row
    ' 获取数学成绩表的最后一行
    lastRowMath = wsMath.Cells(wsMath.Rows.Count, 1).End(xlUp).Row
    
    ' 清空合并表的内容
    wsMerged.Cells.Clear
    
    ' 添加表头
    wsMerged.Cells(1, 1).Value = "学号"
    wsMerged.Cells(1, 2).Value = "姓名"
    wsMerged.Cells(1, 3).Value = "语文成绩"
    wsMerged.Cells(1, 4).Value = "数学成绩"
    
    ' 初始化合并表的行数
    lastRowMerged = 1
    
    ' 遍历语文成绩表
    For i = 2 To lastRowChinese
        studentId = wsChinese.Cells(i, 1).Value
        studentName = wsChinese.Cells(i, 2).Value
        chineseScore = wsChinese.Cells(i, 3).Value
        
        ' 在合并表中查找是否已有该学生的记录
        found = False
        For j = 2 To lastRowMerged
            If wsMerged.Cells(j, 1).Value = studentId And wsMerged.Cells(j, 2).Value = studentName Then
                wsMerged.Cells(j, 3).Value = chineseScore
                found = True
                Exit For
            End If
        Next j
        
        ' 如果没有找到,则添加新行
        If Not found Then
            lastRowMerged = lastRowMerged + 1
            wsMerged.Cells(lastRowMerged, 1).Value = studentId
            wsMerged.Cells(lastRowMerged, 2).Value = studentName
            wsMerged.Cells(lastRowMerged, 3).Value = chineseScore
        End If
    Next i
    
    ' 遍历数学成绩表
    For i = 2 To lastRowMath
        studentId = wsMath.Cells(i, 1).Value
        studentName = wsMath.Cells(i, 2).Value
        mathScore = wsMath.Cells(i, 3).Value
        
        ' 在合并表中查找是否已有该学生的记录
        found = False
        For j = 2 To lastRowMerged
            If wsMerged.Cells(j, 1).Value = studentId And wsMerged.Cells(j, 2).Value = studentName Then
                wsMerged.Cells(j, 4).Value = mathScore
                found = True
                Exit For
            End If
        Next j
        
        ' 如果没有找到,则添加新行
        If Not found Then
            lastRowMerged = lastRowMerged + 1
            wsMerged.Cells(lastRowMerged, 1).Value = studentId
            wsMerged.Cells(lastRowMerged, 2).Value = studentName
            wsMerged.Cells(lastRowMerged, 4).Value = mathScore
        End If
    Next i
    
    ' 自动调整列宽
    wsMerged.Columns.AutoFit
    
    MsgBox "成绩合并完成!"
End Sub

代码说明:

  1. 工作表设置:代码首先设置了三个工作表对象 wsChinesewsMath 和 wsMerged,分别对应“语文成绩”、“数学成绩”和“合并成绩”三个表单。

  2. 获取最后一行:通过 End(xlUp) 方法获取“语文成绩”和“数学成绩”表的最后一行。

  3. 清空合并表:在合并之前,清空“合并成绩”表的内容。

  4. 添加表头:在“合并成绩”表中添加表头。

  5. 遍历语文成绩表:遍历“语文成绩”表中的每一行,将学号、姓名和语文成绩插入到“合并成绩”表中。如果学号和姓名已经存在,则只更新语文成绩。

  6. 遍历数学成绩表:遍历“数学成绩”表中的每一行,将学号、姓名和数学成绩插入到“合并成绩”表中。如果学号和姓名已经存在,则只更新数学成绩。

  7. 自动调整列宽:合并完成后,自动调整“合并成绩”表的列宽。

使用方法:

  1. 打开Excel文件,按 Alt + F11 打开VBA编辑器。

  2. 在VBA编辑器中,插入一个新模块(Insert > Module)。

  3. 将上述代码粘贴到模块中。

  4. 关闭VBA编辑器,返回Excel。

  5. 按 Alt + F8,选择 MergeScores 宏并运行。

运行后,“合并成绩”表中将包含所有学生的学号、姓名、语文成绩和数学成绩。

:成绩合并后,按学号从低到高排序

DeepSeek:

在合并成绩后,你可以通过VBA代码对“合并成绩”表按学号从低到高进行排序。以下是修改后的代码,添加了排序功能:

Sub MergeAndSortScores()
    Dim wsChinese As Worksheet
    Dim wsMath As Worksheet
    Dim wsMerged As Worksheet
    Dim lastRowChinese As Long
    Dim lastRowMath As Long
    Dim lastRowMerged As Long
    Dim i As Long, j As Long
    Dim studentId As String
    Dim studentName As String
    Dim chineseScore As Variant
    Dim mathScore As Variant
    Dim found As Boolean
    
    ' 设置工作表
    Set wsChinese = ThisWorkbook.Sheets("语文成绩")
    Set wsMath = ThisWorkbook.Sheets("数学成绩")
    Set wsMerged = ThisWorkbook.Sheets("合并成绩")
    
    ' 获取语文成绩表的最后一行
    lastRowChinese = wsChinese.Cells(wsChinese.Rows.Count, 1).End(xlUp).Row
    ' 获取数学成绩表的最后一行
    lastRowMath = wsMath.Cells(wsMath.Rows.Count, 1).End(xlUp).Row
    
    ' 清空合并表的内容
    wsMerged.Cells.Clear
    
    ' 添加表头
    wsMerged.Cells(1, 1).Value = "学号"
    wsMerged.Cells(1, 2).Value = "姓名"
    wsMerged.Cells(1, 3).Value = "语文成绩"
    wsMerged.Cells(1, 4).Value = "数学成绩"
    
    ' 初始化合并表的行数
    lastRowMerged = 1
    
    ' 遍历语文成绩表
    For i = 2 To lastRowChinese
        studentId = wsChinese.Cells(i, 1).Value
        studentName = wsChinese.Cells(i, 2).Value
        chineseScore = wsChinese.Cells(i, 3).Value
        
        ' 在合并表中查找是否已有该学生的记录
        found = False
        For j = 2 To lastRowMerged
            If wsMerged.Cells(j, 1).Value = studentId And wsMerged.Cells(j, 2).Value = studentName Then
                wsMerged.Cells(j, 3).Value = chineseScore
                found = True
                Exit For
            End If
        Next j
        
        ' 如果没有找到,则添加新行
        If Not found Then
            lastRowMerged = lastRowMerged + 1
            wsMerged.Cells(lastRowMerged, 1).Value = studentId
            wsMerged.Cells(lastRowMerged, 2).Value = studentName
            wsMerged.Cells(lastRowMerged, 3).Value = chineseScore
        End If
    Next i
    
    ' 遍历数学成绩表
    For i = 2 To lastRowMath
        studentId = wsMath.Cells(i, 1).Value
        studentName = wsMath.Cells(i, 2).Value
        mathScore = wsMath.Cells(i, 3).Value
        
        ' 在合并表中查找是否已有该学生的记录
        found = False
        For j = 2 To lastRowMerged
            If wsMerged.Cells(j, 1).Value = studentId And wsMerged.Cells(j, 2).Value = studentName Then
                wsMerged.Cells(j, 4).Value = mathScore
                found = True
                Exit For
            End If
        Next j
        
        ' 如果没有找到,则添加新行
        If Not found Then
            lastRowMerged = lastRowMerged + 1
            wsMerged.Cells(lastRowMerged, 1).Value = studentId
            wsMerged.Cells(lastRowMerged, 2).Value = studentName
            wsMerged.Cells(lastRowMerged, 4).Value = mathScore
        End If
    Next i
    
    ' 按学号从低到高排序
    With wsMerged
        .Sort.SortFields.Clear
        .Sort.SortFields.Add Key:=.Range("A2:A" & lastRowMerged), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        .Sort.SetRange .Range("A1:D" & lastRowMerged)
        .Sort.Header = xlYes
        .Sort.Apply
    End With
    
    ' 自动调整列宽
    wsMerged.Columns.AutoFit
    
    MsgBox "成绩合并并排序完成!"
End Sub

新增的排序功能说明:

  1. 排序范围:使用 With wsMerged 语句指定排序范围为“合并成绩”表的 A1:D 到最后一行的区域。

  2. 排序字段:通过 .Sort.SortFields.Add 方法添加排序字段,按“学号”列(A列)进行升序排序。

  3. 排序设置

    • Key:指定排序的列(A列)。

    • Order:设置为 xlAscending,表示升序排序。

    • Header:设置为 xlYes,表示包含表头。

  4. 应用排序:通过 .Sort.Apply 方法应用排序。

使用方法:

  1. 打开Excel文件,按 Alt + F11 打开VBA编辑器。

  2. 在VBA编辑器中,插入一个新模块(Insert > Module)。

  3. 将上述代码粘贴到模块中。

  4. 关闭VBA编辑器,返回Excel。

  5. 按 Alt + F8,选择 MergeAndSortScores 宏并运行。

运行后,“合并成绩”表将按学号从低到高排序,并包含所有学生的学号、姓名、语文成绩和数学成绩。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

liufangshun

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值