【问题标题】:Select first Empty Cell from a selected cell (or range), then add a Value (date), then offset and inputext从选定的单元格(或范围)中选择第一个空单元格,然后添加一个值(日期),然后是偏移量和输入文本
【发布时间】:2023-03-14 15:00:01
【问题描述】:

我一直在四处寻找好的示例,但找不到我需要的。
这里是上下文:该代码用于包含大约 50 个供应商的销售跟踪器工作表(他们每个人都可以增加价值,而且他们中的大多数人对 Excel 一无所知)。

我想选择第一个空单元格(他们可以输入的第一个值是 B5,而不是更高,因为表格顶部包含一些说明)。事实上,从这个单元格(日期值在 B 列,从第 5 行开始),第二个日期值在 B6 中

Add the Date (date or now) as activecell.value
Then 2 cells to the right activecell.offset(0,2)
And insert the value of the textbox (their ID).

现在,我可以添加日期和文本框 ID。
这是我到目前为止所拥有的:

Sub CommandButton1_click()

Dim Input_ID As String, Date_in As String

Date_in = Format(Now, "DD-MMM")
ActiveCell.Value = Date_in
Input_ID = InputBox("SVP entré votre ID ", "Data Entry Form")
ActiveCell.Offset(0, 2) = Input_ID

End Sub

但是是否可以使该命令/按钮仅适用于“B”列?因为我没有为他们添加一个日期和他们的 ID 到另一个列。
PS:我或多或少是从 VBA 开始的,我到处学习一点,所以如果你可以添加您的代码中有一些解释,我很感激。谢谢

Edit1:从评论发帖

Sub Date_insert_click() 

Dim Input_ID As String, Date_in As String 
Dim ws As Worksheet 
Set ws = ActiveSheet 'change to your actual worksheet 
'Dim Date_in As Date 
Date_in = Format(Now, "DD-MMM")
With ws.Range("B" & ws.Rows.Count).End(xlUp) 
    If .Row >= 4 Then .Offset(1, 0).Value = Date_in Else Exit Sub 
    Input_ID = InputBox("SVP entré votre ID ", "Data Entry Form") 
    If Input_ID <> "" Then .Offset(1, 1).Value = Input_ID Else .Offset(1, 0).Value = ""
End With 

End Sub 

但我发现了一个弱点。如果我选择像K378 这样的单元格,
我仍然可以添加值(date_In 或输入框的值),但看不到它,因为单元格未处于活动状态。

【问题讨论】:

    标签: vba date excel


    【解决方案1】:

    按照评论试试这个:

    Sub CommandButton1_click()
    
    Dim Input_ID As String, Date_in As String
    Dim ws As Worksheet
    
    Set ws = Thisworkbook.Sheets("Sheet1") 'change to your actual worksheet
    Date_in = Format(Now, "DD-MMM")
    
    With ws.Range("B" & ws.Rows.Count).End(xlUp)
        If .Row >= 4 Then .Offset(1, 0).Value = Date_in Else Exit Sub
        Input_ID = InputBox("SVP entré votre ID ", "Data Entry Form")
        If Input_ID <> "" Then .Offset(1, 2).Value = Input_ID Else .Offset(1, 0).Value = ""
    End With
    
    End Sub
    

    Edit1:按要求解释

    问:为什么要将Worksheet 对象传递给变量?
    答:HERE 是对这个问题的一些解释。此外,它还使您的代码更具可读性和易于调试。

    解释代码:

    'This line simply finds the last cell in Column B
    With ws.Range("B" & ws.Rows.Count).End(xlUp)         
        'other code here
    End With
    

    为什么使用With?我用with是因为所有的编码都集中在Column B上,其他数据输入也是引用它。
    你也可以在我上面提供的链接中看到使用它的优点的解释。

    With ws.Range("B" & ws.Rows.Count).End(xlUp)
        'Since we used With, you can directly access the Range properties
        'The following line uses the Row and Offset property
        'Row returns the row number of the range you're workning on
        'Offset literally offets the range you are currently working on
        If .Row >= 4 Then .Offset(1, 0).Value = Date_in Else Exit Sub
        'This next line is already known to you, no need to explain
        Input_ID = InputBox("SVP entré votre ID ", "Data Entry Form")
        'Next line checks if Input_ID is supplied
        'If yes, we use offset to get to the 2nd column from the current range
        'If no, we delete the Date_In value
        If Input_ID <> "" Then .Offset(1, 2).Value = Input_ID Else .Offset(1, 0).Value = ""
    End With
    

    我希望我已经解释得够多了。
    但是,如果您仍然需要更多解释,只需将其注释掉即可。
    如果您在某处遇到困难,请发布另一个问题。

    【讨论】:

    • 嗨,我测试了它,似乎可以工作但是,我很欣赏一些解释:ligne“Set Ws ....”的用途我知道“Ws”是用于工作表的,但可以我们不使用它/在代码中添加它?许多其他工作表中使用了相同的命令,如果我可以避免调整“.sheets("sheet1") 部分,我将不胜感激。谢谢
    • 嗨,经过一些测试,我修改为: Sub Date_insert_click() Dim Input_ID As String, Date_in As String Dim ws As Worksheet Set ws = ActiveSheet '更改为您的实际工作表 'Dim Date_in As Date Date_in = Format(Now, "DD-MMM") With ws.Range("B" & ws.Rows.Count).End(xlUp) If .Row >= 4 Then .Offset(1, 0).Value = Date_in Else Exit Sub Input_ID = InputBox("SVP entré votre ID ", "Data Entry Form") If Input_ID "" Then .Offset(1, 1).Value = Input_ID Else .Offset(1, 0).Value = " " 以结束子结束
    • 嗨,经过一些测试,我修改为: Sub Date_insert_click() Dim Input_ID As String, Date_in As String Dim ws As Worksheet Set ws = ActiveSheet '更改为您的实际工作表 'Dim Date_in As Date Date_in = Format(Now, "DD-MMM") With ws.Range("B" & ws.Rows.Count).End(xlUp) If .Row >= 4 Then .Offset(1, 0).Value = Date_in Else Exit Sub Input_ID = InputBox("SVP entré votre ID ", "Data Entry Form") If Input_ID "" Then .Offset(1, 1).Value = Input_ID Else .Offset(1, 0).Value = " " End With End Sub 但是,我发现了一个弱点,如果我在下面的任意位置选择一个单元格(如 K378)
    • ,我仍然可以添加值(date_In 或输入框的值)但看不到它,因为单元格未处于活动状态。所以
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-10-15
    • 1970-01-01
    • 2016-09-20
    • 1970-01-01
    • 1970-01-01
    • 2019-03-14
    • 1970-01-01
    相关资源
    最近更新 更多