【发布时间】:2017-02-01 22:06:34
【问题描述】:
我目前正在尝试编写一个宏,该宏基本上采用一列,将其复制到右侧,然后更改某些引用以引用回上一列。
我正在使用替换方法,但是因为我正在寻找基于列字母和“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
【问题讨论】:
标签: string replace insert offset