【问题标题】:Fill down blank cells in multiple columns in specific sheets填写特定工作表中多列中的空白单元格
【发布时间】:2015-04-26 21:30:28
【问题描述】:

我有以下列

Score   Name    Last Name
5460    Name1   Lastname1
2620        
340     
470     
3445    Name2   Lastname2
1290        
1170        
7460    Name3   Lastname3
3520        
360     
1500        
5048    Name4   Lastname4
830     
490     
1883        
620     
1208        
185     
426     
653     


我想填写 columns B & C 中的空单元格,直到 Column A 中的最后一行(在本例中为 row 21 )。
结果

Score   Name    Last Name
5460    Name1   Lastname1
2620    Name1   Lastname1
340     Name1   Lastname1
470     Name1   Lastname1
3445    Name2   Lastname2
1290    Name2   Lastname2
1170    Name2   Lastname2
7460    Name3   Lastname3
3520    Name3   Lastname3
360     Name3   Lastname3
1500    Name3   Lastname3
5048    Name4   Lastname4
830     Name4   Lastname4
490     Name4   Lastname4
1883    Name4   Lastname4
620     Name4   Lastname4
1208    Name4   Lastname4
185     Name4   Lastname4
426     Name4   Lastname4
653     Name4   Lastname4


目前我正在尝试调整这个 vba 代码:

Sub FillColBlanksSpecial()

Dim wks As Worksheet
Dim rng As Range
Dim rng2 As Range
Dim LastRow As Long
Dim col As Long
Dim lRows As Long
Dim lLimit As Long

Dim lCount As Long
On Error Resume Next

lRows = 2
lLimit = 1000

Set wks = ActiveSheet
With wks

   col = .Range("b1,c1").Column

   Set rng = .UsedRange  'try to reset the lastcell
   LastRow = .Cells.SpecialCells(xlCellTypeLastCell).Row
   Set rng = Nothing

    lCount = .Columns(col).SpecialCells(xlCellTypeBlanks).Areas(1).Cells.Count

    If lCount = 0 Then
        MsgBox "No blanks found in selected column"
        Exit Sub
    ElseIf lCount = .Columns(col).Cells.Count Then
        Do While lRows < LastRow
            Set rng = .Range(.Cells(lRows, col), .Cells(lRows + lLimit, col)) _
                           .Cells.SpecialCells(xlCellTypeBlanks)
            rng.FormulaR1C1 = "=R[-1]C"
            lRows = lRows + lLimit
        Loop
    Else
        Set rng = .Range(.Cells(2, col), .Cells(LastRow, col)) _
                       .Cells.SpecialCells(xlCellTypeBlanks)
        rng.FormulaR1C1 = "=R[-1]C"
    End If
End With

End Sub


问题 1:此代码仅在 B 列 上运行。如何在B & C 栏中运行 正如您在代码中看到的,我尝试了col = .Range("b1,c1").Column,但无济于事。

问题 2:如何在名称以 -A-B 结尾的工作表中仅在同一工作簿中运行?

在同一个工作簿中,我有数百张工作表,但我想在以上述后缀结尾的工作表中运行。 谢谢。

【问题讨论】:

  • 空单元格是否真的是空白的,或者它们是否可以用公式等中的零长度字符串(例如“”)填充?
  • @Jeeped 他们真的是空白。里面什么都没有。
  • 请复制并粘贴您的数据(格式为代码),而不是发布图片链接。如果链接失效,我们将不知道您在说什么。谢谢。
  • @WaiHaLee 我添加了复制粘贴乐趣的代码。

标签: vba excel


【解决方案1】:

您可以在循环中使用与活动工作表相同的变量来循环工作表集合,检查工作表名称以查看它是否需要处理。

Sub FillColBlanksSpecial()
    Dim wks As Worksheet

    For Each wks In Worksheets
        If Right(wks.Name, 2) = "-A" Or Right(wks.Name, 2) = "-B" Then
            With wks
                With .Cells(1, 1).CurrentRegion
                    With .Columns("B:C")
                        If CBool(Application.CountBlank(.Cells)) Then
                            .SpecialCells(xlCellTypeBlanks).FormulaR1C1 = "=r[-1]c"
                        End If
                    End With
                    'un comment the next line if you want the formulas to revert to values only
                    '.Cells = .Cells.Value
                End With
            End With
        End If
    Next wks

End Sub

我添加了一行来将重复的公式恢复为其基础值,但留下了注释。如果您只喜欢值,请取消注释该行。

【讨论】:

  • 我无法运行你的代码,它给出了一个错误。我应该将我的代码的任何部分添加到您的代码中吗?还是您的代码应该独立工作?我应该如何适应它?
  • @Mpondomise - 我在原始回复中有拼写错误。我在 5 分钟的“宽限期”内更正了此问题,因此它可能不会显示为编辑。你能重试吗?如果您仍然收到错误,那么错误在哪一行,错误代码是什么?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-11-15
  • 2021-03-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多