【发布时间】: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”,如下你有表格的标题,你必须区分))