各位大侠帮帮忙吧!
Sub 保存1() With Sheet1 b1 = .Cells(1, 2).Value '合同号 a3 = .Cells(3, 1).Value '设备名称 B3 = .Cells(3, 2).Value '设备型号 C3 = .Cells(3, 3).Value '单价 d3 = .Cells(3, 4).Value '数量 E3 = .Cells(3, 5).Value '金额 End With Dim rg As Integer, flg As Boolean, i As Integer flg = True rg = Sheet2.Range("A1048576").End(xlUp).Row If IsNumeric([b1]) = True Then MsgBox "已保存": Exit Sub With Sheet2 .Range("C1:C65536").NumberFormatLocal = "@" '设置列C为文本格式,不然总当成日期格式 For i = 2 To rg If .Cells(i, 1) = b1 Then '如果设备合同里有相同订单号则覆盖数据 .Cells(i, 1) = b1 '合同号 .Cells(i, 2) = a3 '设备名称 .Cells(i, 3) = B3 '设备型号 .Cells(i, 4) = C3 '单价 .Cells(i, 5) = d3 '数量 .Cells(i, 6) = E3 '金额 flg = False End If Next If flg Then '如果没有任何相同覆盖,则添加新的设备合同数据 rg = rg + 1 .Cells(rg, 1) = b1 '合同号 .Cells(rg, 2) = a3 '设备名称 .Cells(rg, 3) = B3 '设备型号 .Cells(rg, 4) = C3 '单价 .Cells(rg, 5) = d3 '数量 .Cells(rg, 6) = E3 '金额 End If End With End Sub