简体   繁体   中英

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

About the problem, how to update if the cell ID is true with button via vba-excel, so I need to update some values from sheet 1 a table column to another sheet a List of data.

For sample :

In Sheet1 (Edit or update any values of cells)

在此处输入图片说明

In Sheet2 (Get the data updated if the ID is 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

I hope anyone help me to resolve the issue

Use Find() to locate the matching row (if present) then update that row: if not found then add a new row

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

EDIT: cell-by-cell if you need more control over what goes where

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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