2017-08-14 33 views
0

我有下面的代碼的麻煩了相當數量:VBA測試兩個值,如果是不同的,複製

Sub TestEmail() 

    Dim i As Long 
    Dim LastRow As Long 
    Dim a As Worksheet 
    Dim b As Worksheet 
    Dim strText 
    Dim ObjData As New MSForms.DataObject 
    Set a = Workbooks("Book2").Worksheets(1) 
    Set b = Workbooks("Book1").Worksheets(1) 
    LastRow = a.Cells(Rows.Count, "A").End(xlUp).Row 

    For i = 2 To LastRow 
     If Not IsError(Application.Match(a.Cells(i, 7).Value, b.Columns(3), 0)) And IsError(Application.Match(a.Cells(i, 4).Value, b.Columns(11), 0)) Then 
      a.Range("D" & i).Copy 
      ObjData.GetFromClipboard 
      strText = Replace(ObjData.GetText(), Chr(10), "") 
      b.Range("K" &).Value = b.Range("K" &).Value & "/" & strText 
     End If 
    Next i 

End Sub 

我面臨兩個問題,一是有我難住了,另一種是由於缺乏的知識:

IF之後的行應該檢查兩個工作簿中的兩個值(數字)是否匹配,以及兩個其他值(文本)是否匹配。如果全部都是true,那麼它必須從Book2中複製一個值並將其添加到book1中的單元格。

的問題是:

-The宏似乎並沒有當值匹配或不承認。

- 在「End If」之前的最後一行,我不知道如何告訴excel將文本複製到第二個檢查中不匹配的單元格中。

如果我不夠清楚,我很抱歉,這很難解釋。

我希望有一位專家知道如何使這項工作。

在此先感謝

+0

您正在使用'如果不是條件1和條件2',所以你說如果它不符合這兩個條件,那麼你運行代碼。你想要做的是嵌套如果語句https://stackoverflow.com/questions/34852883/vba-nested-if-statement然而,一個是'如果'和另一個'如果不是' – danieltakeshi

+0

@danieltakeshi我改爲嵌套如果聲明和它似乎爲我的第一個問題做了訣竅。非常感謝你! – PaulRey

+0

你在第二個問題上的目標是什麼?你能舉個例子嗎?您現在正在做的是使用value_from_Book1_cell/value_from_Book2_cell更改Book1「K#」中的值。用Chr替換空白(10) – danieltakeshi

回答

0
  • 您使用If Not condition 1 And condition 2,所以你說,如果它不符合這兩個條件,那麼你運行代碼。你想什麼是Nested If Statements然而,一個是If和其他If Not
  • 要複製你缺少的i後「K」 &:b.Range("K" & i) = b.Range("K" & i).Value & "/" & strText
  • 細胞的地址在該範圍的功能,這裏面你情況是:

//It is the cell of the email from the first Workbook tou are copying, where you input the column D

a.Range("D" & i).Copy

//Add to Workbook b in column K the value from Cell K#/value copied

b.Range("K" & i) = b.Range("K" & i).Value & "/" & strText

你也可以把它像這樣:b.Range("K" & i) = b.Range("K" & i).Value & "/" & a.Range("D" & i)

這種方式,您都匹配行,所以只有當ID是關於這兩個工作簿相同的行會工作。如果不是,你將不得不使用Nesting Loops.Find Function

編輯:

  • 如果我的理解是,下面的代碼可能,如果你讓你的應用程序的一些變化,工作,因爲我沒有沒有要測試的數據和列,等等。試着去實現它。

LastRowa = a.Cells(Rows.Count, "A").End(xlUp).Row 
LastRowb = b.Cells(Rows.Count, "A").End(xlUp).Row 
For i = 2 To LastRowa 
    'Address of String to look for 
    LookForString = a.Worksheets(1).Cells(i, 4) '4 is the COLUMN_INDEX 
    'Range to look on Workbook a 
    With a.Worksheets(1).Range("D1:D" & LastRowa) 'choose column to look 
     'Function .Find String on book a 
    Set mail_a = .Find(LookForString, LookIn:=xlValues) 
     If Not mail_a Is Nothing Then 
     FirstAddress = mail_a.Address 
      Do ' Actions here 
       'Range to look on Workbook b 
       With b.Worksheets(1).Range("K1:K" & LastRowb) 'choose column to look 
        'Function .Find on Workbook b 
        Set mail_b = .Find(LookForString, LookIn:=xlValues) 
        If Not mail_b Is Nothing Then 
        FirstAddress = mail_b.Address 
         Do 'Actions 
         'Verify if two other values (text) don't match 
         If Not WRITE_MATCH_CONDITION_HERE Then 
          'No need to verify of they are equal because the .Find function used the same reference 
          'I will use .Cells with .Row and .Column just to show another way to do it and make it dynamic 
          b.Cells(mail_b.Adress.Row, mail_b.Adress.Column) = b.Cells(mail_b.Adress.Row, mail_b.Adress.Column).Value & "/" & a.Cells(mail_a.Adress.Row, mail_a.Adress.Column) 'choose columns 
         End If 
        Set mail_b = .FindNext(mail_b) 
         Loop While Not mail_b Is Nothing And mail_b.Address <> FirstAddress 
        End If 
       End With 
       Set mail_a = .FindNext(mail_a) 
      Loop While Not mail_a Is Nothing And mail_a.Address <> FirstAddress 
     End If 
    End With 
    Next i 
    End Sub 

P.S:該<>上缺少mail_a.Address <> FirstAddress和mail_b。地址<> FirstAddress,當我張貼

 

+0

不幸的是他們不在同一條線上。這是我所提出的**嵌套循環**: 'If Not IsError(Application.Match(a.Cells(i,7).Value,b.Columns(3),0) )然後 如果IsError(Application.Match(a.Cells(i,4).Value,b.Columns(11),0))然後' 我似乎無法找到一種方法來使**。Find Function * *做我所需要的,因爲我不能確切地告訴它要找什麼。 – PaulRey

相關問題