原始数据表中名称应该与金额保持一一对应,但是在实际录入过程(原数据在XLS表)中有可能出错,出现空白或金额与名称没有保持一一对应,如例子数量序号8与10,金额出错。请问如何在录入狐表后,有语句检查一次数据的准确性,找出空白或同一名称前后金额不一致的行。
此主题相关图片如下:数量.jpg

[此贴子已经被作者于2025/3/12 22:35:39编辑过]
网上问DEEPSEEK,发现金额错的序号没有检查出来
'定义一个字典用于记录名称和首次出现的金额
Dim dict As New Dictionary(Of String, Decimal)
' 用于存储错误信息
Dim errors As New List(Of String)
' 获取当前数据表
Dim dt As DataTable = DataTables("原始数据")
For Each Row As DataRow In dt.DataRows
Dim name As String = ""
Dim amount As Decimal = 0
'---------- 检查空值 ----------
'检查名称是否为空
If Row.IsNull("金额") Then
errors.Add("行号 " & Row("序号") & ": 金额为空")
Else
name = Row("名称").ToString().Trim()
End If
'---------- 检查金额一致性 ----------
If name <> "" AndAlso Not Row.IsNull("金额") Then
If dict.ContainsKey(name) Then
'如果字典中存在该名称,比较金额是否一致
If dict(name) <> amount Then
errors.Add("行号 " & Row("序号") + 1 & ": 名称【" & name & "】的金额与首次记录不一致(当前值:" & amount & ",首次值:" & dict(name) & ")")
End If
Else
'如果不存在,将名称和金额存入字典
dict.Add(name, amount)
End If
End If
Next
'---------- 输出检查结果 ----------
If errors.Count > 0 Then
Dim msg As New StringBuilder()
msg.AppendLine("发现以下错误:")
For Each err As String In errors
msg.AppendLine("? " & err)
Next
MessageBox.Show(msg.ToString(), "数据检查结果", MessageBoxButtons.OK, MessageBoxIcon.Error)
Else
MessageBox.Show("数据检查完成,未发现错误。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If