奇怪为什么这段代码无法正常工作。
Sub copyfirst()
For i = 1 To Sheet20.Cells(Rows.Count, 1).End(xlUp).Row
Set x = Sheet20.Range("A" & i)
'here i tell vba to find text in bold and in black
If x.Characters(i, 1).Font.Color = RGB(0, 0, 0) And x.Characters(1, 1).Font.Bold = True Then
res = x.Text
'then i copy it in another sheet in column 1
x.Copy Sheet21.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0)
Debug.Print x.Address
x.Offset(1, 2).Activate
'PROBLEM! here is the problem
Debug.Print x.Offset(1, 2).Address
Range(ActiveCell, ActiveCell.End(xlDown)).Copy
Sheet21.Cells(Rows.Count, 2).End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=True
Else: GoTo Nextiteration
End If
Nextiteration:
Next i
End Sub
所以问题出在x.Offset(1,2).Activate行中。实际上,x在A&i中,所以offset(1,2)应该给我C&i + 1。
相反,它给了我D列!为什么会这样?
这是x和x.0ffset(1,2)的debug.print地址
x.address = $ A $ 3
x.offset(1,2)= $ D $ 4
应该是C4!不是D4! 除了找到的最后一个变量,它总是给我D4,它在正确的地方给了我列C。
那是为什么?
答案 0 :(得分:0)
如果x是合并的单元格,则这样做。 如果x为range(“ a1”),则 x(1,1)等于range(“ a1”),x(2,2)= B2,x(2,3)= C2
x(2,3).activate