簡體   English   中英

使用另一個 Excel 工作簿更新 Excel 工作簿中的數據庫

[英]Updating data base from an Excel workbook with another Excel workbook

我必須更新一個包含項目數據的 excel 數據庫。 每周我都必須下載我公司創建的新數據庫,其中包含新項目和舊項目的更新數據。 我想創建一個執行此操作的宏(更新舊項目中的新信息並添加新項目)。 項目名稱是唯一的。 我嘗試使用下一個代碼自動更新數據,但它沒有做任何事情(我的數據庫沒有改變),我不知道為什么(每個項目都是一行,項目中的每個數據都是一列)

    Sub UpdateData()

Dim h1 As Workbook 'workbook where the data is to be pasted
Dim s1 As Worksheet
Dim h2   As Workbook 'workbook from where the data is to copied
Dim s2 As Worksheet
Dim strName  As String   'name of the source sheet/ target workbook
Dim aCell As Range, bCell As Range
    Dim SearchString As String
    Dim ExitLoop As Boolean, matchFound As Boolean

'set to the current active workbook (the source book)
Set h2 = ActiveWorkbook
Set s2 = ActiveSheet

Set h1 = Workbooks.Open("C:\Users\BAICFL\Desktop\macro prueba.xlsx")
Set s1 = h1.Worksheets("Sheet1")


s2.Activate
Dim col As Long
Dim LastRow1 As Long
    Dim row As Long
Dim i As Integer
Dim j As Integer
with s1
LastRow1 = .Range("E" & .Rows.Count).End(xlUp).Row
End With
with s2
LastRow2 = .Range("E" & .Rows.Count).End(xlUp).Row
End With


For i = 1 To LastRow1
  For j = 1 To LastoRow2


If s2.Range("E" & j).Value = s1.Range("E" & i).Value Then

s1.Range("D" & i).Value = s2.Range("D" & j).Value
s1.Range("F" & i).Value = s2.Range("F" & j).Value
s1.Range("G" & i).Value = s2.Range("G" & j).Value
s1.Range("H" & i).Value = s2.Range("H" & j).Value
s1.Range("I" & i).Value = s2.Range("I" & j).Value
s1.Range("J" & i).Value = s2.Range("J" & j).Value
s1.Range("K" & i).Value = s2.Range("K" & j).Value
s1.Range("L" & i).Value = s2.Range("L" & j).Value
s1.Range("M" & i).Value = s2.Range("M" & j).Value
s1.Range("N" & i).Value = s2.Range("N" & j).Value
s1.Range("O" & i).Value = s2.Range("O" & j).Value
s1.Range("P" & i).Value = s2.Range("P" & j).Value
s1.Range("Q" & i).Value = s2.Range("Q" & j).Value
s1.Range("R" & i).Value = s2.Range("R" & j).Value
s1.Range("S" & i).Value = s2.Range("S" & j).Value
s1.Range("T" & i).Value = s2.Range("T" & j).Value


End If
Next
Next
End Sub

我相信您的代碼的主要問題是您將工作表聲明並設置為 AcitveWorkbook 和工作表相同,並且在使用多個工作簿時,您應該完全限定您的范圍,因為您可能正在查看另一個工作簿,而 VBA 將假設那是活躍的。

我還通過將一個范圍復制到您的目的地,在一行代碼中完成了數據傳輸。

你的第二個 For 循環也有錯別字,而不是 LastRow2 你有 LastRow2 ......

另外 i 和 j 應該聲明為 Long 而不是整數,看看下面的代碼:

Sub UpdateData()
Dim LastRow1 As Long, LastRow2 As Long, i As Long, j As Long
Dim h1 As Workbook
Dim s1 As Worksheet
Dim h2 As Workbook: Set h2 = ThisWorkbook
Dim s2 As Worksheet: Set s2 = h2.Worksheets("Sheet1")
'declare and set your workbook/worksheet amend as required

Set h1 = Workbooks.Open("C:\Users\BAICFL\Desktop\macro prueba.xlsx")
Set s1 = h1.Worksheets("Sheet1")

LastRow1 = s1.Cells(s1.Rows.Count, "E").End(xlUp).row
LastRow2 = s2.Cells(s2.Rows.Count, "E").End(xlUp).row

For i = 1 To LastRow1
    For j = 1 To LastRow2
        If s2.Range("E" & j).Value = s1.Range("E" & i).Value Then
             s1.Range("D" & i & ":T" & i).Copy s2.Range("D" & j & ":T" & j)
        End If
    Next j
Next i
End Sub

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM