如果活动单元格值=偏移单元格值,则

时间:2019-05-02 14:00:43

标签: excel vba offset

我正在尝试在Excel VBA中使用“偏移”来确定2个单元格的值是否相同。脚本拒绝Offset,提示“未定义Sub或Function”

我在许多论坛上进行了广泛搜索,但没有成功

Dim b As Integer
Dim detail As String
Dim wbk As Workbook
Dim ws As Worksheet

Set wbk = Workbooks("Commission Statements.xlsm")
With Workbooks("Commission Statements.xlsm").Sheets("Required Data")
b = 2
Do Until IsEmpty(.Cells(b, 1))

'If A2 = A1 AND B2 = B1
'Copy cells in columns D:M
'Go to Worksheet named in column A
'Paste
If ActiveCell.Value = ActiveCell(Offset(-1)).Value And ActiveCell(Offset(0, 1)).Value = ActiveCell(Offset(-1, 1)).Value Then
.Range(Cells(b, 4), .Cells(b, 13)).Copy
detail = .Cells(b, 1).Value
Sheets(detail).Range("B" & Rows.Count).End(xlUp).Offset(1).PasteSpecial xlValues
End If

'If A2 = A1 BUT B2 > B1
'Go to Worksheet named in Column A
'Copy cells B8:K9
'Go to bottom line of data in column K
'Paste
'Return to Active Cell in "Required Data" Worksheet
'Copy cells in columns D:M
'Go to Worksheet named in column A
'Locate last row of data in column K and offset 1 row
'Paste
ElseIf ActiveCell.Value = ActiveCell(Offset(-1)).Value And ActiveCell(Offset(0, 1)).Value > ActiveCell(Offset(-1, 1)).Value Then
detail = .Cells(b, 1).Value
ws.Range("B8:K9").Copy
Range("" & Rows.Count).End(xlUp).Offset(3).PasteSpecial xlPasteValues
ws.Activate "Required Data"
Range(Cells(b, 4), .Cells(b, 13)).Copy
detail = .Cells(b, 1).Value
Sheets(detail).Range("B" & Rows.Count).End(xlUp).Offset(1).PasteSpecial xlValues

b = b + 1

Loop
End With
End Sub

A列中的每个单元格值都有一个相关的工作表,我需要粘贴到该工作表。 我正在寻找的是缩进显示的代码。有两种可能的情况:

  • A中的单元格=单元格(偏移量(-1))AND B中的单元格=单元格(偏移量(-1)

或:

  • A中的单元格=单元格(offset(-1))AND B中的单元格<>单元格(offset(-1)

0 个答案:

没有答案