简体   繁体   English

在可变范围的单元格中循环,并用特定字符填充空单元格

[英]Cycle through a variable range of cells and fill empty cells with a certain character

I am learning VBA as I go along and have managed to compile a lot of code from a range of sources but am finding it hard to solve my current problem. 我正在学习VBA,并设法从多种资源中编译了很多代码,但是发现很难解决当前的问题。 I have read a lot of solutions regarding working with ranges but I have been unable to adapt any of the ones that I have seen to resolve my issue. 我已经阅读了很多有关使用范围的解决方案,但是我无法适应我所看到的解决问题的方法。

I would like a macro which, when a Command Button is pressed, will identify the last used row in a range of cells (which will increase in row count over time) then check each row for any empty cells within the range and filling these with the letter 'N' if there is data in the same row in Column A. 我想要一个宏,当按下命令按钮时,它将识别单元格区域中最后使用的行(随着时间的推移,行数会增加),然后检查每一行中是否有该范围内的任何空单元格,并用如果列A的同一行中有数据,则字母'N'。

I currently have the following code: 我目前有以下代码:

Private Sub CBtnFillAll_Click()
'
' EmptyCharacteristic Macro
' Fills empty cells in the characteristics columns with 'N'
'

Dim Lastrow As Integer
Dim rCell As Range
Dim rRng As Range

Lastrow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row
Set rRng = ActiveSheet.Range("$H$3:$S" & Lastrow)

For Each rCell In rRng.Rows
    If rCell.Value = "" And ActiveSheet.Cells(rRng.Row, 1).Value <> "" Then
    rCell.Value = "N"
    End If
Next rCell

End Sub

I am checking Column A as there is additional data starting in Column B in rows which I do not want to include in the range. 我正在检查A列,因为在B列中我不想包含在行中的行中还有其他数据。 The range to check will always be between Columns H and S. 检查范围将始终在H列和S列之间。

I am currently getting a 'Type Mismatch' error in the following line: 我当前在以下行中收到“类型不匹配”错误:

If rCell.Value = "" And ActiveSheet.Cells(rRng.Row, 1).Value <> "" Then

Please can someone help me with the syntax in this final part? 请问有人可以帮助我完成最后一部分中的语法吗?

Many thanks in advance. 提前谢谢了。

Your approach is completely correct, but there is one thing that creates the problem: 您的方法是完全正确的,但是有导致问题的一件事:

Change your For Each line to this: For Each rCell in rRng . 将您的For Each行更改For EachFor Each rCell in rRng

There are a few things I would have done differently, so here is my complete code: 我会做一些不同的事情,所以这是我完整的代码:

Sub test()
Application.ScreenUpdating = False
Dim Lastrow As Long
Dim rCell As Range
Dim aCell As Range
Dim rRng As Range
Dim Currentrow As Long

Lastrow = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row
Set rRng = ActiveSheet.Range("H3:S" & Lastrow)

For Each rCell In rRng
    Currentrow = rCell.Row
    Set aCell = ActiveSheet.Range("A" & Currentrow)
    If Not IsEmpty(aCell.Value) And IsEmpty(rCell.Value) Then
    rCell.Value = "N"
    End If
Next
Application.ScreenUpdating = True
End Sub

I think that this may do what you are looking for 我认为这可能会满足您的需求

Sub CBtnFillAll_Click()
' EmptyCharacteristic Macro
' Fills empty cells in the characteristics columns with 'N'
'
    Dim rCell   As Range, _
        rRng    As Range

    For Each rRng In ActiveSheet.UsedRange.Columns("A:A").Cells
        If IsEmpty(rRng) Then GoTo NextRow
        For Each rCell In rRng.Offset(0, 7).Resize(1, 12)
            If IsEmpty(rCell) Then rCell.Value = "N"
        Next rCell
NextRow:
    Next rRng

End Sub

If you are looking to omit the first two lines of your sheet, then you can change 如果您希望省略工作表的前两行,则可以更改

For Each rRng In ActiveSheet.UsedRange.Columns("A:A").Cells

to

For Each rRng In ActiveSheet.[A3].Resize(ActiveSheet.UsedRange.Rows.Count-2)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM