1

我有一个文件“From.xls”,其中有一个包含数据的工作表“From”,该数据将更新到另一个具有工作表“To”的文件“To.xls”。

示例:“From”表在 From.xls 文件中有以下数据

Sno Name    Assigned    Staus   Delivered   Date    Account No
100 Packet 1    Team 1  Delivered   Yes 12-Dec-12   001
101 Packet 2    Team 1  Delivered   Yes 12-Dec-12   003
103 Packet 4    Team 2  Not Delivered   No      003
105 Packet 6    Team 2  Delivered   Yes 12-Dec-12   001
110 Packet 11   Team 2  Delivered   Yes 12-Dec-12   002
111 Packet 12   Team 2  Not Delivered   No      002
112 Packet 13   Team 2  Delivered   Yes 12-Dec-12   001
115 Packet 16   Team 2  Delivered   Yes 12-Dec-12   001
116 Packet 17   Team 11 Not Delivered   No      002

“To”表在 To.xls 文件中有以下数据

Sno Name    Assigned    Staus   Delivered   Date    Account No
100 Packet 1    Team 1      No      
101 Packet 2    Team 1      No      
102 Packet 3    Team 3      No      
103 Packet 4    Team 2      No      
104 Packet 5    Team 5      No      
105 Packet 6    Team 2      No      
106 Packet 7    Team 7      No      
107 Packet 8    Team 8      No      
108 Packet 9    Team 9      No      
109 Packet 10   Team 10     No      
110 Packet 11   Team 2      No      
111 Packet 12   Team 2      No      
112 Packet 13   Team 2      No      
113 Packet 14   Team3       No      
114 Packet 15   Team4       No      
115 Packet 16   Team 2      No      
116 Packet 17   Team 11     No      
117 Packet 18   Team7       No      
118 Packet 19   Team8       No      

我想根据检查 Sno 和 Name 的条件将“From”表中的所有行更新为“To”表,并一键获得最终的 To 表,如下所示。

Sno Name    Assigned    Staus   Delivered   Date    Account No
100 Packet 1    Team 1  Delivered   Yes 12-Dec-12   001
101 Packet 2    Team 1  Delivered   Yes 12-Dec-12   003
102 Packet 3    Team 3      No      
103 Packet 4    Team 2  Not Delivered   No      003
104 Packet 5    Team 5      No      
105 Packet 6    Team 2  Delivered   Yes 12-Dec-12   001
106 Packet 7    Team 7      No      
107 Packet 8    Team 8      No      
108 Packet 9    Team 9      No      
109 Packet 10   Team 10     No      
110 Packet 11   Team 2  Delivered   Yes 12-Dec-12   002
111 Packet 12   Team 2  Not Delivered   No      002
112 Packet 13   Team 2  Delivered   Yes 12-Dec-12   001
113 Packet 14   Team3       No      
114 Packet 15   Team4       No      
115 Packet 16   Team 2  Delivered   Yes 12-Dec-12   001
116 Packet 17   Team 11 Not Delivered   No      002
117 Packet 18   Team7       No      
118 Packet 19   Team8       No  
4

1 回答 1

0

这有点难以阅读,我认为很容易回答(如果我理解正确的话)。尽量让一切都清楚。如果 Sno 已交付,您要求复制粘贴数据吗?行是否对齐?Sno 100、101 等在 To 和 From 上是否相同?Sno 是按顺序出现的,还是必须搜索它们?

如果需要进行搜索,您可能希望使用 vba 来完成(尽管对于这么简单的事情,一些 VLOOKUP 和 IF 也可以在单元格内完成)。

复制粘贴条件到底是什么?我确信这可以解决。

编辑——示例代码:

Option Explicit

Sub CopyData()

const FIRST_ROW_FROM as Long 1 'Put the first row of the data in From
const LAST_ROW_FROM as Long = 4 'Put the last row of the data in From
const SNO_COL_FROM as Long = 1 'Put the column of the sno in From

const FIRST_ROW_TO as Long 1 'Put the first row of the data in To
const LAST_ROW_TO as Long = 4 'Put the last row of the data in To
const SNO_COL_TO as Long = 1 'Put the column of the sno in To

const NUM_COLS as long = 5 'Number of columns of data

Dim rowCounter as Long

Dim searchVal as String
Dim fileName as String

Dim errHappened as Bool = False

Dim foundCell as Range

Dim xlApp as Excel.Application
Dim fromBook as Workbook
Dim toBook as Workbook
Dim fromSheet as Worksheet
Dim toSheet as Worksheet


'Remove the comment mark (') below if you want the program to exit silently on an error
'On Error Resume Next

set xlApp = CreateObject("Excel.Application")

If xlApp is Nothing Then
    errHappened = True
    GoTo CleanUp
End If


'From

'make these xlsx or xlsm if that is what they are
fileName = Application.GetOpenFileName("Excel Workbooks, *.xls", "Select the From File")

If Instr(fileName, "From.xls") = 0 Then
    errHappened = True
    Goto CleanUp
End If

Set fromBook = xlApp.Open(fileName)

If fromBook is Nothing Then
    errHappened = True
    GoTo CleanUp
Endif


'To

fileName = Application.GetOpenFileName("Excel Workbooks, *.xls", "Select the To File")

If Instr(fileName, "To.xls") = 0 Then
    errHappened = True
    GoTo CleanUp
End If

Set toBook = xlApp.Open(fileName)

If toBook is Nothing Then
    errHappened = True
    GoTo CleanUp
Endif

Set fromSheet = fromBook.Worksheets("From")
Set toSheet = toBook.Worksheets("To")

If (fromSheet is Nothing) Or (toSheet is Nothing) Then
    errHappened = True
    GoTo CleanUp
End If

For rowCounter = FIRST_ROW_FROM to LAST_ROW_FROM

    searchVal = fromSheet.Cells(rowCounter, SNO_COL_FROM)
    Set foundCell = Nothing
    Set foundCell = toSheet.Range(toSheet.Cells(FIRST_ROW_TO, SNO_COL_TO), toSheet.Cells(LAST_ROW_TO, SNO_COL_TO) ).Find(searchVal,, xlValues, xlWhole)

    If foundCell is Nothing Then

        errHappened = True
        MsgBox("Could Not Find Sno " & searchVal & " in To.xls from row " & Cstr(rowCounter) & " in From.")

    Else

        fromSheet.Range(fromSheet.Cells(rowCounter, SNO_COL_FROM), fromSheet.Cells(rowCounter, SNO_COL_FROM + NUM_COLS -1 )).Copy
        foundCell.PasteSpecial Paste:= xlPasteAll

    End If

Next rowCounter

'If you feel comfortable with the results, you can uncomment toBook.Save here and get rid of the
'Safe Ending below.  Delete from Begin Safe Ending to End Safe Ending

'toBook.Save

'''''Begin Safe Ending

'Makes worksheets visible so that you can verify you are happy with the results.  If you are,
'save it.  Otherwise, close without saving.  I will not take responsibility for a Sub that
'Overwrites your files, so I am giving you the Safe Ending!

xlApp.Visible = True
Exit Sub

'''''End Safe Ending


CleanUp:

If Not fromBook is Nothing then
    fromBook.Saved = True
    fromBook.Close
End If

If Not toBook is Nothing then
    toBook.Saved = True
    toBook.Close
End If

If Not xlApp is Nothing Then
    xlApp.Close
End If

If errHappened Then
    MsgBox("There was an error",, "Error")
End If

End Sub

我是在 Mac 上写的,所以未经测试!!!请花时间在中断模式下运行代码并验证其完整性。

于 2012-12-14T07:08:08.900 回答