在水平定义的区域内填充空白空单元格

问题描述 投票:-1回答:3

我正试图用0填充某个区域的空白单元格。 reagion应该在当前工作簿中定义,但在sheet2中(不是当前工作表)。它应该填充的位置是当前区域中的列BU:CQ之间(不是所有100 000 000行)。只是在列BU和CQ之间定义表的行数。我知道问题在于定义区域...请参阅下面的代码。缺什么?

Sub FillEmptyBlankCellWithValue()
    Dim cell As Range
    Dim InputValue As String

    On Error Resume Next
    InputValue = "0"
    For Each cell In ThisWorkbook.Sheets("Sheet2").Range(BU).CurrentRegion
      '.Cells(Rows.Count, 2).End(xlUp).Row
      If IsEmpty(cell) Then
        cell.Value = InputValue
      End If
    Next
 End Sub

我有这个代码,我是积极的,有效!但我不选择!我想要一些指定工作表和固定范围的东西。现在我的想法是用所需的范围替换“选择”。 - 在这种情况下,特别是范围应该是1 - 在BU:CQ之间; 2 - 从第2行开始; 3 - 向下工作直到最后一行(非空=从A列到DE的表的结尾)

 Sub FillEmptyBlankCellWithValue()
     Dim cell As Range
     Dim InputValue As String
     On Error Resume Next
     For Each cell In Selection
        If IsEmpty(cell) Then
           cell.Value = "0"
        End If
     Next
 End Sub'

PS:我还需要指定工作表,因为执行代码的按钮将在同一工作簿中但不在同一工作表中。

excel vba excel-vba
3个回答
1
投票

使用SpecialsCells:

On Error Resume Next  'for the case the range would be all filled
With ws
    Intersect(.UsedRange, .Range("BU:CQ")).SpecialCells(xlCellTypeBlanks).Value = 0
End With
On Error GoTo 0

比循环更快!


0
投票

尝试使用cell()引用,例如:

For i = cells(1,"BU").Column to cells(1,"CQ").Column
    cells(1,i).value = "Moo"
Next i

在当前代码中,列出了Range(BU),这是不合适的语法。请注意,Range()可用于命名范围,例如Range(“TheseCells”),但实际的单元格引用被写为Range(“A1”)等。对于Cell(),您将使用Cells(行,列)。


EDIT1

使用if语句,使用第二个循环:

Dim i as long, j as long, lr as long
lr = cells(rows.count,1).end(xlup).row
For i = 2 to lr 'assumes headers in row 1
    For j = cells(1,"BU").Column to cells(1,"CQ").Column
        If cells(i,j).value = "" then cells(i,j).value = "Moo"
    Next j
Next i

0
投票

首先,您应该参考您正在使用的工作表:

Set ws = Excel.Application.ThisWorkbook.Worksheets(MyWorksheetName)

否则VBA将为您选择工作表,它可能是也可能不是您要使用的工作表。

然后使用它来指定特定工作表上的范围,例如ws.Rangews.Cells。这是一种更好的方法,用于指定您正在处理的工作表。

现在问你的问题:

我将使用以下语法引用该范围:

Dim MyRange As Range
Set MyRange = ws.Range("BU:CQ")

我会像这样迭代这个范围:

编辑:我测试了它,它的工作原理。显然你会想要改变范围和工作表参考;我认为你有足够的能力自己做这件事。我没有为我的工作表创建变量,因为引用工作表的另一种方法是使用属性窗口中的工作表(Name)属性,您可以将其设置为您想要的任何内容;这是一个免费的全局变量。

我在属性窗口中定义testWS的位置:

enter image description here

Public Sub test()
    Dim MyRange As Range
    Dim tblHeight As Long
    Dim tblLength As Long
    Dim offsetLen As Long
    Dim i As Long
    Dim j As Long

    With testWS
        'set this this to your "BU:CQ" range
        Set MyRange = .Range("P:W")
        'set this to "A:BU" to get the offset from A to BU
        offsetLen = .Range("A:P").Columns.Count - 1
        'set this to your "A" range
        tblHeight = .Range("P" & .Rows.Count).End(xlUp).Row
        tblLength = MyRange.Columns.Count
    End With

    'iterate through the number of rows
    For i = 1 To tblHeight
        'iterate through the number of columns
        For j = 1 To tblLength
            If IsEmpty(testWS.Cells(i, offsetLen + j).Value) Then
                testWS.Cells(i, offsetLen + j).Value = 0
            End If
        Next
    Next

End Sub

之前:

enter image description here

之后(我提前停止了,所以它没有遍历文件中的所有行):

enter image description here

如果有更好的方法,请告诉我。

© www.soinside.com 2019 - 2024. All rights reserved.