Я не уверен, что понимаю, чего вы пытаетесь достичь. Я не понимаю ваш код, который, кажется, просто очищает столбец B листа 2. Я не понимаю, почему вы используете макрос для задания формулы.
Код ниже делает то, что я думаю, вы пытаетесь сделать. Если нет, я надеюсь, что мой код даст вам достаточно идей, чтобы вы могли создать код, который вы ищете.
Полагаю, вы не очень знакомы с Excel Basic. Извините, если следующее оскорбляет ваши знания. Я полагаю, вы бы скорее были оскорблены, чем сбиты с толку.
Sub Test2()
' This is revised coding. I had not read the question carefully enough
' so my original code did not do what was required.
Dim Pos2 As Integer ' The 1s and 2s in variable names
Dim RowCrnt As Integer ' identify the variable as being for Sheet1
Dim RowMax As Integer ' or Sheet2. The same row variables are
Dim S1ColAB() As Variant ' used for both sheets.
Dim S2ColAB() As Variant
Dim Value1 As String
Dim Value2 As String
With Sheets("Sheet2")
' I generally use Find instead of End(xlUp) for reasons I no longer
' remember. This searches column A (Columns("A")) for anything ("*")
' starting from cell A1 (Range("A1")) and moving backwards
' (xlPrevious) until it finds a value.
RowMax = .Columns("A").Find("*", .Range("A1"), xlFormulas, , _
xlByRows, xlPrevious).Row
' Range(Cells(A,B),Cells(C,D)) defines a rectangle of cells with Row=A,
' Col=B as top left and Row=C, Col=D as bottom right.
' The following statement loads the contents of a block of cells to a
' variant array. Another question has led to a discussion about the value
' of using variant arrays in this way. I have found that moving values
' from one sheet to another can be very slow so I believe it is useful in
' this situation.
S2ColAB = .Range(.Cells(1, 1), .Cells(RowMax, 2)).Value
' Warning about moving values from a cell into a string or variant variable
' and then from the variable into another cell.
' =========================================================================
' When moving the value from the variable to the cell, Excel will
' misinterpret the value if it can.
'
' For example, if the value is 13/1/11 (13 January 2011 here in England)
' this value will be correctly transferred into the new cell. But if the
' value is 4/1/11 (4 January 2011), Excel will recognise this as a valid
' American date and set the new cell to 1 April 2011. The damage that bug
' caused by corrupting a third my dates! I had tested my code towards the
' end of a month and it worked perfectly until the next month.
'
' In this example, string $12.03 becomes currency 12.03 and displays
' here as £12.03.
End With
With Sheets("Sheet1")
' Load the matching cells from sheet 1
S1ColAB = .Range(.Cells(1, 1), .Cells(RowMax, 2)).Value
End With
With Sheets("Sheet2")
For RowCrnt = 1 To RowMax
' I move the Column A values for matching row from the arrays to string
' variables so I can amend their values without losing the original
' values. This was essential with my original code and I have not
' changed it since I think it makes the code easier to understand and
' probably marginally faster.
Value1 = S1ColAB(RowCrnt, 1)
Value2 = S2ColAB(RowCrnt, 1)
' The following code removes anything following a hyphen from Value 2.
' It then removes all commas and dots from both Value1 and Value2. If
' the final values are the same, it moves the Column B of Sheet1 to
' Sheet2.
Pos2 = InStr(1, Value2, "-")
If Pos2 <> 0 Then
' Pos2 is not zero so extract the portion of Value2 up to the "-"
' and then trim trailing spaces.
Value2 = RTrim(Mid(Value2, 1, Pos2 - 1))
End If
' Replace all commas with nothing.
Value1 = Replace(Value1, ",", "")
' Replace all dots with nothing.
Value1 = Replace(Value1, ".", "")
' Merge the two replaces into a single statement.
Value2 = Replace(Replace(Value2, ",", ""), ".", "")
If Value1 = Value2 Then
' If the modified values are equal, copy the Column 2 (B) from
' Sheet1 to Sheet2.
.Cells(RowCrnt, 2).Value = S1ColAB(RowCrnt, 2)
End If
Next
End With
End Sub
Надеюсь, это поможет. Возвращайся, если я не объяснил себя адекватно.