插入复制的单元格,然后使用替换功能引用回原始单元格

时间:2017-02-01 22:06:35

标签: string replace insert offset

我目前正在尝试编写一个基本上采用列的宏,将其复制到右侧,然后更改某些引用以引用回到上一列。

我正在使用replace方法,但因为我正在寻找基于列字母和“2”的特定字符串,所以我使用了一个对象将列字母和“2”放在一起。更好的解释方法是我说我复制了B栏并将其插入并在C栏中复制了一份。

我现在想在我的公式中找到“C2和”C3“并将它们更改为”B2“和”B3“。

我认为这可以通过使用replace方法找到上述字符串并将它们偏移-1来完成。事实证明这很困难。有什么想法吗?

'duplicates column over 1'

ActiveCell.EntireColumn.Select
Selection.Copy
ActiveCell.Offset(0, 1).EntireColumn.Select
Selection.Insert Shift:=xlToRight
Selection.PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone
Application.CutCopyMode = False

'Declarations and Instantiations'
Dim rngo As Range, cell As Range, ranger As Range
Dim lookfor As String
Dim UsedRng As Range, LastRow As Long
Set rngo = Selection.EntireColumn
Set UsedRng = ActiveSheet.UsedRange
LastRow = UsedRng(UsedRng.Cells.Count).Row

rngo.Select

Do Until ActiveCell.Row = LastRow + 1

For Each cell In rngo

Col = SPLIT(ActiveCell(1).Address(1, 0), "$")(0) 'returns just the cell letter'

lookfor = (Col & "2") 'combines the column letter with the number(I BELIEVE THIS IS THE SOURCE OF THE ISSUE BUT IM NOT SURE IN WHAT WAY'

'starts to search the new column for "lookfor" which is just the designated string'
rngo.Replace _What:=lookfor, Replacement:="'offset lookfor by 1 column'",_SearchOrder:=xlByRows, MatchCase:=True

Next cell
ActiveCell.Offset(1, 0).Select
Loop

MsgBox ColumnName(Selection)
MsgBox lookfor

End Sub

1 个答案:

答案 0 :(得分:0)

解决:将“rngo.Replace”更改为“ActiveCell.Replace”