繁体   English   中英

使用VBA-Excel获取工作表上的值ID以编辑或更新另一工作表中列表中的数据

[英]Get value ID on sheet to edit or update the data from list in another sheet using VBA-Excel

关于此问题,如何通过vba-excel用按钮更新单元格ID是否为真,因此我需要将表表的第1个表列中的某些值更新为数据表的另一个表中的值。

样品:

在Sheet1中 (编辑或更新单元格的任何值)

在此处输入图片说明

在Sheet2中 (如果ID为true,则获取更新的数据)

在此处输入图片说明

Sub update()
Dim LR1 As Long
With Sheets("Sheet2")
    LR1 = .Range("A" & .Rows.Count).End(xlUp).Row
    .Range("A" & LR1) = Sheets("Sheet1").Range("B2")
    .Range("B" & LR1) = Sheets("Sheet1").Range("B3")
    .Range("C" & LR1) = Sheets("Sheet1").Range("B4")
    .Range("D" & LR1) = Sheets("Sheet1").Range("B5")
End With
End Sub

我希望有人能帮助我解决问题

使用Find()找到匹配的行(如果存在),然后更新该行:如果找不到,则添加新行

Sub update()
Dim f As Range, sht1 As Worksheet
Set sht1 = Sheets("Sheet1")

With Sheets("Sheet2")
    Set f = .Range("A:A").Find(what:=sht1.Range("B1").Value, lookat:=xlWhole)
    If f Is Nothing Then Set f = .Range("A" & .Rows.Count).End(xlUp).Offset(1, 0)
    f.Resize(1, 5).Value = Application.Transpose(sht1.Range("B1:B5").Value)
End With
End Sub

编辑:如果您需要更多的控制权去逐个单元

Sub update()

    Dim f As Range, sht1 As Worksheet
    Set sht1 = Sheets("Sheet1")

    With Sheets("Sheet2")
        Set f = .Range("A:A").Find(what:=sht1.Range("B1").Value, lookat:=xlWhole)
        If f Is Nothing Then Set f = .Range("A" & .Rows.Count).End(xlUp).Offset(1, 0)
        With f.EntireRow
            .Cells(1).Value = sht1.Range("B1").value
            'skip one cell then copy the rest...
            .Cells(3).Resize(1, 4).Value = Application.Transpose(sht1.Range("B4:B5").Value)
            'etc etc
        End With

    End With

End Sub

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM