【问题标题】:Copy header to rows where some are blank将标题复制到一些空白的行
【发布时间】:2019-01-07 07:39:17
【问题描述】:

我有工作代码可以从工作表中删除空白行和不必要的项目。

我有一种情况需要将标题(黄色)复制到 A 列。
就像在示例中一样:将单元格 B1 复制到 A3、A4、A5 并将单元格 B6 复制到 A7、A8 等等。
如果空白,我没有任何成功。我应该申请什么条件才能做到这一点?

Sub Delete_Blank_Rows()
    Dim lRow As Long
    Dim iCntr As Long
    Dim wks As Worksheet
    Dim LngLastRow As Long, lngLastCol As Long, lngIdx As Long, _
        lngColCounter As Long
    Dim blnAllBlank As Boolean
    Dim UserInputSheet As String
    Set wks = Sheets("FNDWRR")
    With wks
        'Now that our sheet is defined, we'll find the last row and last column
        LngLastRow = .Cells.Find(What:="*", LookIn:=xlFormulas, _
                             SearchOrder:=xlByRows, _
                             SearchDirection:=xlPrevious).Row
        lngLastCol = .Cells.Find(What:="*", LookIn:=xlFormulas, _
                             SearchOrder:=xlByColumns, _
                             SearchDirection:=xlPrevious).Column
        'Since we need to delete rows, we start from the bottom and move up
        For lngIdx = LngLastRow To 1 Step -1
            'Start by setting a flag to immediately stop checking
            'if a cell is NOT blank and initializing the column counter
            blnAllBlank = True
            lngColCounter = 2
            'Check cells from left to right while the flag is True
            'and the we are within the farthest-right column
            While blnAllBlank And lngColCounter <= lngLastCol
                'If the cell is NOT blank, trip the flag and exit the loop
                If .Cells(lngIdx, lngColCounter) <> "" Then
                    blnAllBlank = False
                Else
                    lngColCounter = lngColCounter + 1
                End If
            Wend
            'Delete the row if the blnBlank variable is True
            If blnAllBlank Then
                .Rows(lngIdx).Delete
            End If
        Next lngIdx
    End With
    lRow = 45000
    For iCntr = lRow To 1 Step -1
        If Cells(iCntr, 7).Value = "Functional Currency" Then
            Rows(iCntr).Delete
        End If
    Next
    Range("b1").EntireColumn.Insert  
End Sub

【问题讨论】:

  • 如果单元格为空白,我可以看到您正在删除该行,但找不到需要复制标题的任何地方?我错过了什么吗?什么会触发副本?如果 A 列中的单元格为空白,则复制标题?
  • 您可以使用背景颜色。这可以给你一个想法:“如果 B 列中单元格的背景是黄色的,则将值复制到 A 列下面的单元格中,直到背景为蓝色的单元格”......(如果您的孔板遵循以下模式,这将起作用你的例子(除了“Amt Ord”,如下你有表格的标题,你必须区分))

标签: vba header copy rows


【解决方案1】:

试试这个:

Sub copyHeaders()
    Dim lastRow As Integer
    Dim holdName As String

    lastRow = ActiveSheet.Cells.Find("*", searchorder:=xlByRows, searchdirection:=xlPrevious).Row

    For r = 1 To lastRow
        If Cells(r, 1) = "Hold Name" Then
            holdName = Cells(r, 2).Value
            GoTo NextRow
        End If
        If IsEmpty(Cells(r, 1)) And Not IsNull(holdName) Then Cells(r, 1).Value = holdName
NextRow:
    Next r

End Sub

【讨论】:

  • 这是为了快速帮助。我检查了它返回空值的代码,并且没有将单元格 b1 粘贴到 A2、A3、A4 等。
  • A2、A3 等最初是空单元格吗?
  • 是的!他们是空的。 A3、A4、A5 等等,直到它在相应的行中检测到另一个“Hold Name”。
  • 对不起。它正在寻找“保留名称”而不是“保留名称”(大写)。我编辑了代码。它现在应该可以工作了。
  • 非常感谢!这段代码真的很流畅。但是,如果 A 列是银行并且不包含“Hold Name”,我应该更新什么
猜你喜欢
  • 2022-01-17
  • 1970-01-01
  • 1970-01-01
  • 2016-12-31
  • 2014-08-21
  • 2018-04-23
  • 1970-01-01
  • 2022-08-18
  • 1970-01-01
相关资源
最近更新 更多