【问题标题】:Locating first empty cell in a column and ask user to input定位列中的第一个空单元格并要求用户输入
【发布时间】:2015-09-11 12:28:52
【问题描述】:

我在 B 列上有一个包含不同日期的大 excel 文件(它从第 7 行开始并跳转到第 9 行。例如,6 月 6 日 - B7,6 月 7 日 - B9,6 月 8 日 - B11)。 我想要一个宏来定位第一个空单元格(跳1行..例如单元格B7上有一个日期,所以宏应该验证单元格B9是否为空,而不是B8)然后要求用户输入此单元格上的日期。可能吗?

谢谢!

Sub BallMillInspection()

Dim BallMillNumber As String

' IDENTIFY WHAT BALL MILL
BallMillNumber = InputBox("Enter Ball Mill Number, e.g. 1")
If BallMillNumber = vbNullString Then Exit Sub
If BallMillNumber > 5 Then
MsgBox "This Ball Mill does not exist!"
End If

MsgBox "You are starting the inspection of Ball Mill " & BallMillNumber

    Dim vSheet As Worksheet
Set vSheet = Sheets("BM " & BallMillNumber)

With vSheet

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
    For currentRow = 7 To rowCount Step 2
    currentRowValue = Cells(currentRow, sourceCol).Value
    If IsEmpty(currentRowValue) Or currentRowValue = "" Then
        Cells(currentRow, sourceCol) = InputBox("Enter Date")
    End If
Next

End With

End Sub

【问题讨论】:

  • 是的,有可能……
  • @KazJaw 你有例子吗?
  • 您需要向我们展示您尝试过的东西,然后我们会帮助您做对。除非他们心情非常好或无聊,否则没有人会为你做这件事。
  • 检查this like '如何询问?和“如何搜索?”在 StackOverflow...
  • @KazJaw 我现在无法回答我自己的问题,所以.. 我会尝试使用评论。 Dim sourceCol As Integer, rowCount As Integer, currentRow As Integer Dim currentRowValue As String sourceCol = 2 'B 列的值为 2 rowCount = Cells(Rows.Count, sourceCol).End(xlUp).Row '对于每一行,找到第一个空白单元格并选择它 For currentRow = 1 To rowCount currentRowValue = Cells(currentRow, sourceCol).Value If IsEmpty(currentRowValue) or currentRowValue = "" Then Cells(currentRow, sourceCol)=inputbox("Enter Date") End If下一个

标签: excel vba


【解决方案1】:

在您的循环中,只需添加第 2 步即可移动两个单元格

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
'added Step 2
For currentRow = 1 To rowCount Step 2
    currentRowValue = Cells(currentRow, sourceCol).Value
    If IsEmpty(currentRowValue) Or currentRowValue = "" Then
        Cells(currentRow, sourceCol) = InputBox("Enter Date")
    End If
Next

【讨论】:

  • 当我更改 currentRow=7(因为它从第 7 行开始)时,它不再起作用了。我只想找到从 B7 开始的第一个空单元格,然后输入日期
  • 你能多说一些不工作的地方吗?我用从 B7 开始的日期运行它(跳过每隔一行并删除奇数行中的一些日期)并且它工作正常。我什至删除了 B7 日期,它仍然运行到完成。
  • 嘿..谢谢你的帮助!我用整个代码再次编辑了我的问题,这样你就可以看到我在做什么。首先,我必须阅读用户将输入日期的工作表(SheetBM)。然后,使用此工作表.. 用户将在从 B7 开始的第一个空单元格中输入日期。我也不知道如何选择之前选择的工作表(我知道有一个“with”命令,但它总是给我一个错误消息)/
  • 它只是不运行。它读取代码的第一部分,但是当它到达“空单元格查找器”部分时,它会停止并且没有错误消息
  • 当我使用 currentRow=1 运行时,它会运行(但要求输入两次日期)。但是已经填充的日期从第 7 行开始。当我更改 currentRow=7... 它没有运行:(
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-15
  • 1970-01-01
  • 2023-02-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多