我得到的是两本工作簿,目前如果从属工作簿中的单元格与主工作表中的单元格相同,则会复制某些数据。此外,如果找不到但是一列等于是,它会将它添加到最后。 但是,我想要的是,如果它不能在主服务器中找到但存在于从服务器中,则从salve工作簿中删除它。 以下是我到目前为止:
Dim bolFound As Boolean
Dim lngLastRow As Long
Dim fpath As String
Dim owb As Workbook
Dim Master As Worksheet 'declare both
Dim Slave As Worksheet
fpath = "\\calum\Work\mastersheet.xlsm"
Set owb = Application.Workbooks.Open(fpath) 'open location and file
Set Master = ThisWorkbook.Worksheets("Tbl_Primary") 'sheet from workbook im in
Set Slave = owb.Worksheets("Schedule") 'sheet in workbook im copying too
'
Let x = 0
Do While x < 2 'loop through twice so it can add new data
lngLastRow = Slave.Cells.Find("*", searchorder:=xlByRows, searchdirection:=xlPrevious).Row + 1 'find last row
For j = 1 To 1000 '(the master sheet)
bolFound = False
For i = 1 To 1000 '(the slave sheet) 'for first 1000 cells
If Trim(Master.Cells(j, 2).Value2) = vbNullString Then Exit For 'if ID cell is blank jump to last line
If Master.Cells(j, 2).Value = Slave.Cells(i, 1).Value And _
Master.Cells(j, 65).Value = "Yes" Then
Slave.Cells(i, 5).Value = Master.Cells(j, 18).Value & ", " & Master.Cells(j, 19).Value & ", " & Master.Cells(j, 20).Value & ", " & Master.Cells(j, 22).Value 'If the ID equals that in the slave sheet and there is a yes ticked the copy address
Slave.Cells(i, 8).Value = Master.Cells(j, 5).Value
Slave.Cells(i, 10).Value = Master.Cells(j, 31).Value
Slave.Cells(i, 11).Value = Master.Cells(j, 33).Value
Slave.Cells(i, 12).Value = Master.Cells(j, 7).Value
Slave.Cells(i, 13).Value = Master.Cells(j, 30).Value
Slave.Cells(i, 15).Value = Master.Cells(j, 34).Value
bolFound = True
End If
Next
If bolFound = False And _
Master.Cells(j, 65).Value = "Yes" Then
Slave.Cells(lngLastRow, 1).Value = Master.Cells(j, 2).Value 'adding the new entry to the list
lngLastRow = lngLastRow + 1
Else
Slave.Cells(i, 1).EntireRow.Delete 'not sure how to delete row if bolfound=false and it isn't in the master?
End If
Next
x = x + 1
Loop
答案 0 :(得分:1)
但遗憾的是我无法发表评论但是使用Rows(i).Delete
删除行不会更容易吗?
对于Slave.Cells(i, 1).EntireRow.Delete
我总是1或1000 ?!
你的问题出在 i 当它删除这一行时的某个地方......如果它是1并且为1000个 j 循环执行此操作将删除所有数据。
在删除之前,可能需要其他类型语句来检查bolfound = false
。
我可以提供最好的建议,而不是将其解决。