2017-06-19 97 views
0

我只是試圖根據標題搜索特定列並在其之前插入一個新列(因此在列的左側)。現在,我的代碼搜索所有包含我的搜索的列,並在左側插入一列新的2列。例如,我要搜索,說Paid to Date只有頭,但它甚至插入一列旁邊Commission Paid to Date在基於行標題的特定列之前插入一個新列-VBA

Sheets(NewSheet).Select 
Set ColHeaders = Range("A4:CD4").Find("Paid to Date") 
If ColHeaders Is Nothing Then 
    MsgBox "Paid to Date Column was not found." 
    Exit Sub 
Else 
    Columns(ColHeaders.Column).Offset(0, -1).Insert 
End If 
+0

既然你知道你是在左邊插入2列,如何將偏移量改爲1?偏移(0,0)或者你可以移除偏移部分 – 99moorem

+1

Range.Find()方法有更多的標準。一個允許您將其設置爲僅查看整個單元格而不是部分。 Google'Range.Find()'。 –

+0

此外:我建議閱讀(並遵循!)[VBA最佳實踐:避免使用選擇或激活](https://stackoverflow.com/documentation/excel-vba/1107/vba-best-practices/9292/avoid-使用選擇或激活)和[VBA最佳實踐:永不假設工作表](https://stackoverflow.com/documentation/excel-vba/1107/vba-best-practices/9218/never-assume-the-工作表)來改善您的代碼。 –

回答

相關問題