簡體   English   中英

在Excel中找到特定值,並使用Vbscript在其下添加整行

[英]Find certain value in excel and add entire row below it using Vbscript

我想做的是編寫一個VB宏,該宏將掃描整列(F列)灼熱的“年度總計:”,然后在其下方插入整行。

我有這個:

子Macro2()''Macro2 Macro''鍵盤快捷鍵:Ctrl + a

Dim C As Variant
Dim FirstRow As Integer

With Worksheets(1).Range("F1:F4000")
    Set C = .Find("Year Total:", LookIn:=xlValues)
    If Not C Is Nothing Then
        FirstRow = C.Row
        Do
            C.Offset(1, 0).Insert Shift:=xlDown
            Set C = .FindNext(C)
        Loop While Not C Is Nothing And C.Row <> FirstRow
    End If


End With

結束子

但是,它僅在其中添加單元格,而不是整個行。

我也想添加到代碼中,因為它也可以在同一列中搜索“總計:”,並在其下添加三行。 我只是要編寫兩個腳本,但是如果我可以將它們全部融合在一起,那就太好了。

好了,您去了,我剛剛測試了一下,就可以了。 我敢肯定有一種更干凈的方法可以做到這一點,但這聽起來像是您正在尋求一種快速的解決方案,因此這有望為您服務。 :)

' Get last row
intLastRow = ActiveSheet.Range("F" & Rows.Count).End(xlUp).Row

' Loop till last row
intRow = 0
For Each strColFValue In ActiveSheet.Range("F1:F" & intLastRow).Value
    intRow = intRow + 1

    ' If found year, insert row below
    If InStr(UCase(strColFValue), "YEAR TOTAL:") > 0 Then
        Range("F" & intRow + 1).Select
        Selection.EntireRow.Insert
        intRow = intRow + 1
    End If

    ' If found grand total, insert 3 rows below
    If InStr(UCase(strColFValue), "GRAND TOTAL:") > 0 Then
        Range("F" & intRow + 1).Select
        Selection.EntireRow.Insert
        Selection.EntireRow.Insert
        Selection.EntireRow.Insert
        intRow = intRow + 3
    End If

Next

暫無
暫無

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

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