簡體   English   中英

Excel:VBA在行中查找和添加文本

[英]Excel: VBA Find & Add Text in Row

我正在嘗試創建一個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

我的問題是我該如何更改代碼以查找整個行中的文本,例如“商品編號”,“商品ID”,“商品#”,而不是像在我的代碼中那樣找到文本一個特定的單元格。 謝謝!

編輯:添加了圖像,希望可以澄清我的要求。

工作表1 這是使用R 1編輯運行代碼后的結果。

工作表1-CodeError 這是再次運行代碼后的結果,但是,我切換了“ Item#”列。

如果你只是想保持你的宏,但有你的Count(Search(搜索整個行,只是改變R[1]CR[1]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM