输入框值插入右侧行中的下一个空单元格
Input box value insert in the next empty cell in row to the right
我正在尝试向右侧的下一个空单元格添加来自用户表单文本框的数据(如果数据已经存在)。意思是如果 "E1" 有日期,添加到 "F1" 等等,但只是范围 "E1:S1".
报告截图如下:
这是我到目前为止所得到的(但它在 E1 时停止):
Private Sub CommandButton1_Click()
If Range("E1") = "" Then Range("E1") = UserForm2.TextBox1.Value Else
Range("E1").End(xlToRight) = UserForm2.TextBox1.Value
If Range("E2") = "" Then Range("E2") = UserForm2.TextBox2.Value Else
Range("E2").End(xlToRight) = UserForm2.TextBox2.Value
End Sub
End(xlToRight 只会到达填充单元格的末尾,而不是下一个打开的单元格。找到最后一个填充单元格后,您需要再移动一列。使用 Cells(),我更喜欢盯着最远的列并返回。
Private Sub CommandButton1_Click()
If Range("E1").Value = "" Then Range("E1").Value = UserForm2.TextBox1.Value Else
Cells(1, Cells(1, Columns.Count).End(xlToLeft).Column + 1).Value = UserForm2.TextBox1.Value
If Range("E2").Value = "" Then Range("E2").Value = UserForm2.TextBox2.Value Else
Cells(2, Cells(2, Columns.Count).End(xlToLeft).Column + 1).Value = UserForm2.TextBox2.Value
End Sub
我正在尝试向右侧的下一个空单元格添加来自用户表单文本框的数据(如果数据已经存在)。意思是如果 "E1" 有日期,添加到 "F1" 等等,但只是范围 "E1:S1".
报告截图如下:
这是我到目前为止所得到的(但它在 E1 时停止):
Private Sub CommandButton1_Click()
If Range("E1") = "" Then Range("E1") = UserForm2.TextBox1.Value Else
Range("E1").End(xlToRight) = UserForm2.TextBox1.Value
If Range("E2") = "" Then Range("E2") = UserForm2.TextBox2.Value Else
Range("E2").End(xlToRight) = UserForm2.TextBox2.Value
End Sub
End(xlToRight 只会到达填充单元格的末尾,而不是下一个打开的单元格。找到最后一个填充单元格后,您需要再移动一列。使用 Cells(),我更喜欢盯着最远的列并返回。
Private Sub CommandButton1_Click()
If Range("E1").Value = "" Then Range("E1").Value = UserForm2.TextBox1.Value Else
Cells(1, Cells(1, Columns.Count).End(xlToLeft).Column + 1).Value = UserForm2.TextBox1.Value
If Range("E2").Value = "" Then Range("E2").Value = UserForm2.TextBox2.Value Else
Cells(2, Cells(2, Columns.Count).End(xlToLeft).Column + 1).Value = UserForm2.TextBox2.Value
End Sub