注册 登录
编程论坛 VB6论坛

如何编写VBA覆盖原来保存的文件

cdhilite 发布于 2018-02-02 14:00, 5976 次点击
请各位大侠:
[
只有本站会员才能查看附件,请 登录

里面如果"订单"的编号在“合同设备”表里面已经有了,我想在“合同设备”里覆盖新的数据怎么编写VBA?谢谢!
3 回复
#2
cdhilite2018-02-05 08:22
各位大侠帮帮忙吧!
#3
wmf20142018-02-05 08:56
按钮代码如下,应该符合你的需求
程序代码:
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
#4
cdhilite2018-02-05 16:19
谢谢大侠!美女就是不一样哈
1