如何使用宏将现有数据从Sheet1更新到Sheet2?

时间:2018-01-17 00:32:06

标签: vba excel-vba excel

我只是想问一下是否有人有这方面的代码。 我在Sheet2中保存了数据,我想使用Sheet1中的新数据更新它。在下面的示例中,代码将在Sheet2中搜索Family“Oh”,并在单击“更新”按钮时使用Sheet1中的更新信息更新其详细信息。以下是

的屏幕截图

Sheet 1中:

Sheet1

Sheet 2中:

Sheet2

尝试了这段代码,但我无法让它正常工作

Sub FindValues()

Dim lookUpSheet As Worksheet, updateSheet As Worksheet
Dim valueToSearch As String
Dim i As Integer, t As Integer

Set lookUpSheet = Worksheets("sheet1")
Set updateSheet = Worksheets("sheet2")

'get the number of the last row with data in sheet1 and in sheet2
lastRowLookup = lookUpSheet.Cells(Rows.Count, "A").End(xlUp).Row
lastRowUpdate = updateSheet.Cells(Rows.Count, "A").End(xlUp).Row

'for every value in column A of sheet2
For i = 1 To lastRowUpdate
 valueToSearch = updateSheet.Cells(i, 1)
 'look the value in column A of sheet1
 For t = 1 To lastRowLookup
    'if found a match, copy column B value to sheet1 and proceed to the next value
    If lookUpSheet.Cells(t, 1) = valueToSearch Then
        updateSheet.Cells(i, 2) = lookUpSheet.Cells(t, 2)
        Exit For
    End If
 Next t
 Next i

End Sub

提前感谢您的帮助

2 个答案:

答案 0 :(得分:1)

以下内容应该符合您的期望,我已对代码进行了评论,以便您了解它的作用:

Sub FindValues()
    Dim lookUpSheet As Worksheet, updateSheet As Worksheet
    Dim valueToSearch As String
    Dim i As Long, t As Long

    Set lookUpSheet = Worksheets("Sheet1")
    Set updateSheet = Worksheets("Sheet2")

    lastRowLookup = lookUpSheet.Cells(Rows.Count, "A").End(xlUp).Row
    lastRowUpdate = updateSheet.Cells(Rows.Count, "A").End(xlUp).Row
    'get the number of the last row with data in sheet1 and in sheet2

    For i = 2 To lastRowLookup 'i = 2 to last to omit the first row as that row is for headers

        valueFamily = lookUpSheet.Cells(i, 1) 'Family, 1 = Column A
        valueDOB = lookUpSheet.Cells(i, 2) 'DOB, 2 = Column  B
        valueName = lookUpSheet.Cells(i, 3) 'Name, 3 = Column C
        valueAge = lookUpSheet.Cells(i, 4) 'Age, 4 = Column D
        'above get the values from the four column into variables

        For t = 2 To lastRowUpdate 't = 2 to last to omit the first row as that row is for headers
            If updateSheet.Cells(t, 1) = valueFamily And updateSheet.Cells(t, 2) = valueDOB And updateSheet.Cells(t, 3) = valueName Then
            'if family, dob and name match, then
            updateSheet.Cells(t, 4) = valueAge
            'update age value
            Exit For
            End If
        Next t
    Next i
End Sub

这可以缩短而不使用变量和比较单元格,如下所示:

Sub FindValues()
    Dim lookUpSheet As Worksheet, updateSheet As Worksheet
    Dim i As Long, t As Long

    Set lookUpSheet = Worksheets("Sheet1")
    Set updateSheet = Worksheets("Sheet2")

    lastRowLookup = lookUpSheet.Cells(Rows.Count, "A").End(xlUp).Row
    lastRowUpdate = updateSheet.Cells(Rows.Count, "A").End(xlUp).Row
    'get the number of the last row with data in sheet1 and in sheet2

    For i = 2 To lastRowLookup 'i = 2 to last to omit the first row as that row is for headers
        For t = 2 To lastRowUpdate 't = 2 to last to omit the first row as that row is for headers
            If updateSheet.Cells(t, 1) = lookUpSheet.Cells(i, 1) And updateSheet.Cells(t, 2) = lookUpSheet.Cells(i, 2) And updateSheet.Cells(t, 3) = lookUpSheet.Cells(i, 3) Then
            'if family, dob and name match, then
            updateSheet.Cells(t, 4) = lookUpSheet.Cells(i, 4)
            'update age value
            Exit For
            End If
        Next t
    Next i
End Sub

您遇到的问题是,您需要让IF语句查看前3个单元格而不是单个值,因此在条件之间使用AND来比较所有三个单元格。

答案 1 :(得分:0)

这是使用adodb更新到sql的方法。

Sub UpdateSQL()

    Dim Cn As Object
    Dim strConn As String, Name As String
    Dim Ws As Worksheet
    Dim strSQL As String
    Dim i As Integer
    Dim vDB

    strConn = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
        "Data Source=" & ThisWorkbook.FullName & ";" & _
            "Extended Properties=Excel 12.0;"
    Set Ws = Sheets(1)
    Name = Sheets(2).Name
    With Ws
        vDB = .Range("a2", .Range("d" & Rows.Count).End(xlUp))
    End With

    Set Cn = CreateObject("ADODB.Connection")
    Cn.Open strConn

    For i = 1 To UBound(vDB, 1)
        strSQL = "UPDATE [" & Name & "$] set Age=" & vDB(i, 4) & " where Family = '" & vDB(i, 1) & "' AND DOB =#" & vDB(i, 2) & "# AND Name='" & vDB(i, 3) & "' "
        Cn.Execute strSQL
    Next i

    Cn.Close
    Set Cn = Nothing
End Sub