2

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

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

提前致谢!

编辑:在我的 OP 中,我没有提到任何包含“所有客户”值的行理想情况下都会突出显示并以粗体、增大的字体显示。

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

4

5 回答 5

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

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

于 2013-01-03T23:18:31.137 回答
1

假设这是在第一张纸上(“sheet 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 
于 2013-01-03T23:22:09.173 回答
1

从我的一篇文章中改编的这段代码是有效的,并且避免了循环

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

代码

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
于 2013-01-05T09:08:26.633 回答
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
于 2013-01-04T05:56:54.320 回答
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
于 2015-03-17T11:36:40.593 回答