我正在尝试创建一个VBA宏,它将查看工作表的第一行(被认为是标题),它将在每个后续列中找到文本,然后在顶部添加一行,如果找到文本,则在上面添加文本它。我是VBA的新手并且一直在使用宏录音机来玩,到目前为止我得到了这个:
Sub textFind()
Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorLight2
.TintAndShade = -0.249977111117893
.PatternTintAndShade = 0
End With
With Selection.Font
.ThemeColor = xlThemeColorDark2
.TintAndShade = 0
End With
Rows("1:1").Select
With Selection
.HorizontalAlignment = xlCenter
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Range("A1").Select
ActiveCell.FormulaR1C1 = _
"=IF(COUNT(SEARCH({""Description"",""ITEM #"",""Short Description""},R[1]C)),""Sku"","""")"
Range("C1").Select
ActiveCell.FormulaR1C1 = _
"=IF(COUNT(SEARCH({""Product Name"",""Text"",""Short Description""},R[1]C)),""Name"","""")"
Range("D1").Select
ActiveCell.FormulaR1C1 = _
"=IF(COUNT(SEARCH({""Description"",""Text"",""Short Description""},R[1]C)),""Description"","""")"
End Sub
我的问题是我如何能够更改代码以便能够在整个行中找到文本,例如“Item#”,“Product ID”,“Product#”而不是像我的代码中那样在特定的单元格中找到它。谢谢!
编辑:添加图片以希望澄清我的要求。
这是使用R 1编辑运行代码后的结果。
答案 0 :(得分:0)
如果您只想保留自己的宏,但让Count(Search(
搜索整行,只需将R[1]C
更改为R[1]
。