2017-02-01 49 views
1

我目前正在嘗試編寫一個宏,該宏本質上需要一列,將其複製到右側,然後更改某些引用以引用回前一列。插入複製的單元格,然後使用替換函數引用回原始單元格

我正在使用替換方法,但因爲我正在尋找基於列字母和「2」的特定字符串,所以我使用了一個對象將列字母和「2」組合在一起。更好的解釋方法是說我複製B列,插入它,並取得了列重複C.

我現在想找到「C2和‘C3’在我的公式,並更改爲‘B2’和‘B3’。

我認爲這可以通過使用替換方法找到上述字符串並將它們抵消-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 

回答

0

解決:將「rngo.Replace」更改爲「ActiveCell.Replace」

相關問題