使用Excel vba查找并选择B列中的第一个空白单元格
下面的代码可以很好地查找给定列(此处为B列)中的第一个空单元格。但我需要的是一个代码来查找该列中的第一个空白单元格使用Excel vba查找并选择B列中的第一个空白单元格,vba,excel,Vba,Excel,下面的代码可以很好地查找给定列(此处为B列)中的第一个空单元格。但我需要的是一个代码来查找该列中的第一个空白单元格 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
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行开始
有人可以重写此代码来完成此操作吗?这样的代码是否就是您想要的:
Sub test()
Dim ws As Worksheet
Set ws = ActiveSheet
For Each cell In ws.Columns(2).Cells
If IsEmpty(cell) = True Then cell.Select: Exit For
Next cell
End Sub
这将遍历活动工作表B列中的每个单元格,并选择它遇到的第一个空单元格。要将工作表设置为特定的工作表,请将set ws=ActiveSheet
更改为set ws=Sheets(“EnterSheetNameHere”)
或者您可以尝试使用:
Sub test()
Dim ws As Worksheet
Set ws = ActiveSheet
For Each cell In ws.Columns(2).Cells
If Len(cell) = 0 Then cell.Select: Exit For
Next cell
End Sub
我的问题是通过使用以下代码解决的
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
Sheets(“sheet1”)。选择
尺寸LR2为等长,cell2为范围,rng2为范围
附页(“第1页”)
LR2=.Range(“B”和Rows.Count).End(xlUp).Row
对于范围内的每个单元2(“B8:B”和LR2)
如果cell2.Value为“”,则
如果rng2什么都不是,那么
设置rng2=cell2
其他的
设置rng2=Union(rng2,单元2)
如果结束
如果结束
下一单元2
rng2.选择
以
只要我的两分钱
该函数将查找某个区域中遇到的第一个空白单元格,因此它应该处理列和行
'Find first BLANK cell in a given range, returnt a range (one cell)
Function FirstBlank(ByVal rWhere As Range) As Range
Dim vCell As Variant
Dim answer As Range
Set answer = Nothing
For Each vCell In rWhere.Cells
If Len(vCell.Formula) = 0 Then
Set answer = vCell
Exit For
End If
Next vCell
Set FirstBlank = answer
End Function
然后对单元格执行任何操作。尝试此代码以选择单元格B10下方的第一个空单元格。但它需要B10和B11被预先占用 范围(“B10”)。结束(xlDown)。偏移量(1,0)。选择 或
范围(“B100000”)。结束(xlUp)。偏移量(1,0)。选择空白单元格和空白单元格之间的区别是什么?是的,空白单元格和空白单元格之间的区别是什么?可能的重复项