使用Excel vba查找并选择列B中的第一个空白单元格 [英] Find and select first blank cell in column B with Excel vba

查看:3631
本文介绍了使用Excel vba查找并选择列B中的第一个空白单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的代码可以很好地找到给定列中的第一个空单元格(这里是列B)。但是我需要的是一个代码,可以在该列中找到第一个空白单元格。

The below code works fine to find the first empty cell in a given column (here column B). But what I need is a code to find the first blank cell in that column.

Sub macro1()
    Dim sourceCol As Integer, rowCount As Integer, currentRow As Integer
    Dim currentRowValue As String
    sourceCol = 2   'column B has a value of 2
    rowCount = Cells(Rows.Count, sourceCol).End(xlUp).Row
    'for every row, find the first blank cell and select it
    For currentRow = 1 To rowCount
        currentRowValue = Cells(currentRow, sourceCol).Value
        If IsEmpty(currentRowValue) Or currentRowValue = "" Then
            Cells(currentRow, sourceCol).Select
        End If
    Next
End Sub

此外,它应该从第10行开始,而不是第1行。

Also, it should start looking from row 10 instead of row 1.

有人可以重写此代码来执行此操作吗?

Can somebody rewrite this code to do this?

推荐答案

我的问题是通过使用以下代码解决的。 / p>

My problem is solved by using the following code.

Sheets("sheet1").Select
Dim LR2 As Long, cell2 As Range, rng2 As Range
With Sheets("sheet1")
    LR2 = .Range("B" & Rows.Count).End(xlUp).Row
    For Each cell2 In .Range("B8:B" & LR2)
        If cell2.Value <> "" Then
            If rng2 Is Nothing Then
                Set rng2 = cell2
            Else
                Set rng2 = Union(rng2, cell2)
            End If
        End If
    Next cell2
    rng2.Select
End With

这篇关于使用Excel vba查找并选择列B中的第一个空白单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆