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
既然你知道你是在左边插入2列,如何将偏移量改为1?偏移(0,0)或者你可以移除偏移部分 – 99moorem
Range.Find()方法有更多的标准。一个允许您将其设置为仅查看整个单元格而不是部分。 Google'Range.Find()'。 –
此外:我建议阅读(并遵循!)[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-工作表)来改善您的代码。 –