比较两个单元并根据比较运行不同的代码

时间:2019-01-28 22:29:42

标签: excel vba

我希望我的代码使用If语句比较单元格。当单元格不相等时,我要运行其他代码。

当我尝试执行Else If语句时,它在屏幕上显示为红色。

Dim WS1 As Worksheet: Set WS1 = ThisWorkbook.Sheets("Increments")
Dim WS2 As Worksheet: Set WS2 = ThisWorkbook.Sheets("Output")

Dim LR1 As Long, LR2 As Long, WS1_Cell As Range, WS2_Cell As Range

LR1 = WS1.Range("S" & WS1.Rows.Count).End(xlUp).Row
LR2 = WS2.Range("H" & WS2.Rows.Count).End(xlUp).Row

For Each WS1_Cell In WS1.Range("S1:S" & LR1)
    For Each WS2_Cell In WS2.Range("H1:H" & LR2)
        Else If WS1_Cell = WS2_Cell Then
            WS2_Cell.Offset(, 5).Value = WS1_Cell.Offset(, 5).Value
                Next WS2_Cell
Next WS1_Cell

       Else  WS1_Cell <> WS2_Cell Then

Dim wsCopy2 As Worksheet
Dim wsDest2 As Worksheet
Dim lCopyLastRow2 As Long
Dim lDestLastRow2 As Long
Dim wsCopy As Worksheet
Dim wsDest As Worksheet
Dim lCopyLastRow As Long
Dim lDestLastRow As Long

'Set variables for copy and destination sheets
  Set wsCopy2 = Worksheets("Increments")
  Set wsDest2 = Worksheets("Output")

  '1. Find last used row in the copy range based on data in column S
  lCopyLastRow2 = wsCopy2.Cells(wsCopy2.Rows.Count, "S").End(xlUp).Row

  '2. Find first blank row in the destination range based on data in column H
  'Offset property moves down 1 row
  lDestLastRow2 = wsDest2.Cells(wsDest2.Rows.Count, "H").End(xlUp).Offset(1).Row

  '3. Copy & Paste Data if match not found
  wsCopy2.Range("S3:X" & lCopyLastRow2).COPY
     wsDest2.Range("H" & lDestLastRow2).PasteSpecial xlValues

   End If

0 个答案:

没有答案