2014-02-19 34 views
0

我有兩個電子表格複製,那裏是一個匹配

main.xlsm

enter image description here

uat.xlsx

enter image description here

如果有匹配在Main.xlsm中的列A(1)和uat.xlsx中的列B(2)之間,我想還要複製uat.xlsx中列C(3)中的值在main.xlsm中將N(14)列和在main.xlsm中的匹配行上的main.xlsm 中的uat.xlsx中的列D(4)中的值添加到Q(14)列中。

我已經在代碼上取得了一個良好的開端,但是想要添加上面的代碼 - 我該如何去做這件事?

Sub UAT_Update() 
Dim wshT As Worksheet 
    Dim wbk As Workbook 
    Dim wshS As Worksheet 
    Dim r As Long 
    Dim m As Long 
    Dim cel As Range 
    Application.ScreenUpdating = False 
    Set wshT = ThisWorkbook.Worksheets("Master") 
    On Error Resume Next 

    ' Check whether uat.xlsx is already open 
    Set wbk = Workbooks("uat.xlsx") 
     On Error GoTo 0 
     If wbk Is Nothing Then 
     ' If not, open it 
     Set wbk = Workbooks.Open("C:\Working\uat.xlsx") 
    End If 

    ' Set worksheet on uat.xlsx 
    Set wshS = wbk.Worksheets("owssvr") 
    m = wshT.Cells(wshT.Rows.Count, 1).End(xlUp).Row 

    ' Optional - clear columns on main.xlsm 
    ' wshT.Range(wshT.Cells(1, 13), wshT.Cells(m, 13)).ClearContents 

    ' Loop though cells in column A on main.xlsm 
    For r = 1 To m 
     ' Can we find the value in column B of uat.xlsm? 
     Set cel = wshS.Columns(2).Find(What:=wshT.Cells(r, 1).Value, _ 
      LookAt:=xlWhole, MatchCase:=False) 

     If Not cel Is Nothing Then 
      ' If so, enter "Yes" in column M - Comms Sent? 
       wshT.Cells(r, 13).Value = "Yes" 
      ' Enter "Yes" in column O - VDA Deployed? 
       wshT.Cells(r, 15).Value = "Yes" 
      ' Enter "5.6.200" in column P - Version 
       wshT.Cells(r, 16).Value = "5.6.200" 
     End If 
    Next r 

    ' Update column headers 
    wshT.Cells(1, 13).Value = "Comms Sent?" 
    wshT.Cells(1, 14).Value = "OTP" 
    wshT.Cells(1, 15).Value = "VDA Deployed?" 
    wshT.Cells(1, 16).Value = "VDA Version" 
    wshT.Cells(1, 17).Value = "Migration Date" 

    Application.ScreenUpdating = True 
End Sub 
+0

面對與上面的代碼中的任何問題? –

+0

你爲什麼用VBA做這個?你可以使用查詢公式來代替,它的速度要快得多,而且不需要啓動一個宏。 – teylyn

回答

1

我希望上面的代碼運行良好。

我想你應該改線 - >wshT.Cells(r, 15).Value = "Yes"wshT.Cells(r, 15).Value = cel.offset(0,1).value

更新時間:

If Not cel Is Nothing Then     
       wshT.Cells(r, 15).Value = cel.offset(0,1).value 

End If 
相關問題