ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

搜索
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 效率神器,一键搞定繁琐工作
HR薪酬管理数字化实战 Excel 2021函数公式学习大典 Excel数据透视表实战秘技 打造核心竞争力的职场宝典
让更多数据处理,一键完成 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南
查看: 327|回复: 0

[求助] 录取错误都是未录取!

[复制链接]

TA的精华主题

TA的得分主题

发表于 2023-7-2 19:59 | 显示全部楼层 |阅读模式
这个是chatgpt生成的代码帮我看看哪里逻辑错误,谢谢各路大侠!
Sub Admissions()
    Dim sheet1 As Worksheet
    Dim sheet2 As Worksheet
    Dim sheet3 As Worksheet
    Dim dict As Object
    Dim lastRow1 As Long
    Dim lastRow2 As Long
    Dim lastRow3 As Long
    Dim i As Long
    Dim j As Long
    Dim k As Long
    Dim school As String
    Dim minCount As Long
    Dim score As Long
    Dim admissionScore As Long
    Dim admissionSchool As String
    Dim quotaSchools As Dictionary
    Dim quotaMinCounts As Dictionary
    Dim quotaCounts As Dictionary
    Dim graduateSchools As Dictionary
   
    Set sheet1 = Worksheets("Sheet1")
    Set sheet2 = Worksheets("Sheet2")
    Set sheet3 = Worksheets("Sheet3")
    Set dict = CreateObject("Scripting.Dictionary")
    Set quotaSchools = New Dictionary
    Set quotaMinCounts = New Dictionary
    Set quotaCounts = New Dictionary
    Set graduateSchools = New Dictionary
   
    ' 获取sheet2表中的学校志愿和最低录取分数线
    lastRow2 = sheet2.Cells(Rows.Count, "E").End(xlUp).Row
    For i = 2 To lastRow2
        school = sheet2.Cells(i, "E").Value
        minCount = sheet2.Cells(i, "K").Value
        dict(school) = minCount
    Next i
   
    ' 获取sheet3表中的指标录取志愿学校和最低录取人数
    lastRow3 = sheet3.Cells(Rows.Count, "B").End(xlUp).Row
    For i = 2 To lastRow3
        school = sheet3.Cells(i, "B").Value
        For j = 1 To 3
            quotaSchools(school & j) = sheet3.Cells(i, j + 1).Value
            quotaMinCounts(school & j) = sheet3.Cells(i, j + 1).Offset(1, 0).Value
            quotaCounts(school & j) = 0
        Next j
    Next i
   
    ' 遍历sheet1表中的考生数据
    lastRow1 = sheet1.Cells(Rows.Count, "A").End(xlUp).Row
    For i = 2 To lastRow1
        score = sheet1.Cells(i, "H").Value
        admissionScore = -1
        admissionSchool = ""
        
        ' 获取毕业学校
        graduateSchool = sheet1.Cells(i, "J").Value
        graduateSchools(sheet1.Cells(i, "A").Value) = graduateSchool
        
        ' 检查第一志愿是否是指标录取志愿学校
        For j = 1 To 3
            school = quotaSchools(graduateSchool & j)
            If school = sheet1.Cells(i, "B").Value Or school = sheet1.Cells(i, "C").Value Then
                ' 检查该志愿学校是否还有指标名额
                If quotaCounts(school & j) < quotaMinCounts(school & j) Then
                    admissionScore = dict(school)
                    admissionSchool = school
                    quotaCounts(school & j) = quotaCounts(school & j) + 1
                    Exit For
                End If
            End If
        Next j
        
        sheet1.Cells(i, "I").Value = IIf(admissionScore = -1, "未录取", admissionSchool)
    Next i
   
    ' 在sheet2表中显示录取人数
    For i = 1 To 3
        sheet2.Cells(i + 1, "L").Value = quotaCounts(quotaSchools(graduateSchool & 1) & i)
    Next i
End Sub

录取.rar

157.12 KB, 下载次数: 4

您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

手机版|关于我们|联系我们|ExcelHome

GMT+8, 2024-11-16 21:47 , Processed in 0.035344 second(s), 11 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

沪公网安备 31011702000001号 沪ICP备11019229号-2

本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任!     本站特聘法律顾问:李志群律师

快速回复 返回顶部 返回列表