在Excel中的字符串后插入空行

时间:2013-01-03 22:59:21

标签: excel excel-vba vba

我正在尝试在Excel 2010中创建一个宏,该宏查找工作表中值为“所有客户”的每个单元格。每次找到该值时,我都需要在其下方插入一个空白行。认为这将是非常简单,但我搜索了很多论坛,并尝试使用一些示例代码,我无法让它正常工作。对于VBA来说,我是一个完整的新手。以为我会在这里发帖,然后去做一些关于VBA基础知识的阅读。

如果有人有任何良好的培训资源,请发布这些资源。

提前致谢!

编辑:在我的OP中,我忽略了提及任何包含“所有客户”值的行将理想地突出显示并以粗体,增加大小的字体。

这些操作是旧的Crystal Report查看/格式化程序在拉动报表时自动处理的操作。根据软件制造商的技术支持,在我们升级程序之后,我了解到已经删除了这种类型的格式化功能,并发布了更新版本的程序。如果在发行说明中对此进行了定义,我将无法执行升级。无论如何,这就是我在这次宏观灾难中发现自己的方式。

5 个答案:

答案 0 :(得分:1)

Public Sub InsertRowAfterCellFound()

    Dim foundRange As Range
    Set foundRange = Cells.Find(What:="yourStringOrVariant", After:=ActiveCell) 'Find the range with the occurance of the required variant

    Rows(foundRange.Row + 1 & ":" & foundRange.Row + 1).Insert 'Insert a new row below the row of the foundRange row

    foundRange.Activate 'Set the found range to be the ActiveCell, this is a quick and easy way of ensuring you aren't repeating find from the top

End Sub

您可能需要在代码中添加错误处理,因为如果找不到具有指定值的单元格,您将收到错误。

答案 1 :(得分:1)

假设这是在第一张纸上(“纸张1”),这是一个缓慢的答案:

Sub InsertRowsBelowAllCustomers()

    'Set your worksheet to a variable
    Dim sheetOne as Worksheet 
    Set sheetOne = Worksheets("Sheet1")

    'Find the total number of used rows and columns in the sheet (where "All Customers" could be)
    Dim totalRows, totalCols as Integer
    totalRows = sheetOne.UsedRange.Rows.Count
    totalCols = sheetOne.UsedRange.Columns.Count

    'Loop through all used rows/columns and find your desired "All Customers"
    Dim row, col as Integer
    For row = 1 to totalRows
        For col = 1 to totalCols
            If sheetOne.Cells(row,col).Value = "All Customers" Then
                  Range(sheetOne.Cells(row,col)).Select
                  ActiveCell.Offset(1).EntireRow.Insert
                  totalRows = totalRows + 1 'increment totalRows because you added a new row
                  Exit For  
            End If 
        Next col
    Next row
End Sub 

答案 2 :(得分:1)

来自an article of mine here的代码这样的代码很有效,可以避免循环

  1. 它会加粗并增加找到文本的字体大小(在整个行中,正如Tim指出的那样,您应该指定是否仅指单元格)
  2. 在匹配项下方添加一个空白行
  3. <强>码

    Option Explicit
    
    Const strText As String = "All Customers"
    
    Sub ColSearch_DelRows()
        Dim rng1 As Range
        Dim rng2 As Range
        Dim rng3 As Range
        Dim cel1 As Range
        Dim cel2 As Range
        Dim strFirstAddress As String
        Dim lAppCalc As Long
        Dim bParseString As Boolean
    
        'Get working range from user
        On Error Resume Next
        Set rng1 = Application.InputBox("Please select range to search for " & strText, "User range selection", ActiveSheet.UsedRange.Address(0, 0), , , , , 8)
        On Error GoTo 0
        If rng1 Is Nothing Then Exit Sub
    
        'Further processing of matches
        bParseString = True
    
        With Application
            lAppCalc = .Calculation
            .ScreenUpdating = False
            .Calculation = xlCalculationManual
        End With
    
        'a) match string to entire cell, case insensitive
        'Set cel1 = rng1.Find(strText, , xlValues, xlWhole, xlByRows, , False)
    
        'b) match string to entire cell, case sensitive
        'Set cel1 = rng1.Find(strText, , xlValues, xlWhole, xlByRows, , True)
    
        'c)match string to part of cell, case insensititive
         Set cel1 = rng1.Find(strText, , xlValues, xlPart, xlByRows, , False)
    
        'd)match string to part of cell, case sensititive
        ' Set cel1 = rng1.Find(strText, , xlValues, xlPart, xlByRows, , True)
    
        'A range variable - rng2 - is used to store the range of cells that contain the string being searched for
        If Not cel1 Is Nothing Then
            Set rng2 = cel1
            strFirstAddress = cel1.Address
            Do
                Set cel1 = rng1.FindNext(cel1)
                Set rng2 = Union(rng2.EntireRow, cel1)
            Loop While strFirstAddress <> cel1.Address
        End If
    
        'Further processing of found range if required
        If bParseString Then
            If Not rng2 Is Nothing Then
            With rng2
            .Font.Bold = True
            .Font.Size = 20
            .Offset(1, 0).EntireRow.Insert
            End With
        End If
        End If
    
        With Application
            .ScreenUpdating = True
            .Calculation = lAppCalc
        End With
    
    End Sub
    

答案 3 :(得分:0)

此函数从最后一行开始,然后返回到第一行,在A列上包含“所有客户”的每个单元格后面插入一个空行:

Sub InsertRowsBelowAllCustomers()
  Dim R As Integer
  For R = UsedRange.Rows.Count To 1 Step -1
    If Cells(R, 1) = "All Customers" Then Rows(R + 1).Insert
  Next R
End Sub

答案 4 :(得分:0)

错误是因为未在使用范围中指定工作表。 我稍微修改了代码,我的文本在AJ列中,并在单元格上方插入一行。

Dim R As Integer

For R = ActiveSheet.UsedRange.Rows.Count To 1 Step -1

  If Range("AJ" & R) = "Combo" Then Rows(R).Insert

Next R