1

データを持つシート「From」があるファイル「From.xls」があります。このデータは、シート「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 に答える