我希望我解释清楚
我有一个表格,可以在条目中键入并插入到工作表中,并且效果很好
现在我需要在键入产品时,其余的框将从表中获取它们的值(如果存在的话)
也就是说,如果该产品已经存在,将填充相应的值
然后我根据需要进行更改,然后单击“更新”按钮,然后更新同一行
所以我需要两件事
如果该产品已经存在,请在表格的其余框中填写适当的值
更新同一行产品
那是我的代码
Private Sub update_Click()
'When you click the Add button
'Populates the data in the sheet
'~~~~>>>>>> I do not know how to put on the same line of the selected product
Dim lRow As Long
Dim ws As Worksheet
Set ws = Worksheets("sheet")
lRow = ws.Cells(Rows.Count, 2).End(xlUp).Offset(1, 0).Row
With ws
'Me.ComboBox3.Value >>>> Here he should look up the row with the same cell value and write down the following values
.Cells(lRow, 2).Value = Me.TextBox2.Value
.Cells(lRow, 2).Value = Me.TextBox1.Value
End With
End Sub
Private Sub add_Click()
'Not related to here
End Sub
我很乐意从这里的专家那里获得帮助/概念/指导
注意:每种产品都有一个唯一的行
如果不清楚,请给我写信
答案 0 :(得分:1)
如果找到匹配的产品ID,您可以尝试自动填充其他2个字段。每次更改ComboBox1
时,它将使用Range.Find
方法查找产品ID。如果找到产品,则它将使用Column B
Column C
和Offset(r, c)
导入相应的值
您将需要修改Range.Find
方法的选项以满足您的需求。有很多选项,因此最好查看this链接并根据需要添加它们。听起来您像Lookin:= xlWhole
的初学者。
Option Explicit
Private Sub ComboBox1_Change()
Dim ws As Worksheet: Set ws = ThisWorkbook.Sheets("sheet")
Dim Found As Range
Set Found = ws.Range("A:A").Find(ComboBox1.Value)
If Not Found Is Nothing Then
Me.TextBox1 = Found.Offset(, 1)
Me.TextBox2 = Found.Offset(, 2)
Else 'Revert back to blank if nothing is found
Me.TextBox1 = ""
Me.TextBox2 = ""
End If
End Sub
答案 1 :(得分:0)
对于有需要的人,我已经在此处编写了完整的代码,并且效果很好
感谢@urdearboy
Option Explicit
Private Sub ComboBox1_Change()
Dim ws As Worksheet: Set ws = ThisWorkbook.Sheets("Sheets1")
Dim Found As Range
Set Found = ws.Range("A1:C3").Find(What:=ComboBox1.Value, MatchCase:=False)
If Not Found Is Nothing Then
Me.TextBox1 = Found.Offset(, 1)
Me.TextBox2 = Found.Offset(, 2)
Else
Me.TextBox1 = ""
Me.TextBox2 = ""
End If
End Sub
Private Sub update_Click()
Dim ws As Worksheet: Set ws = ThisWorkbook.Sheets("Sheets1")
Dim Found As Range
Set Found = ws.Range("A1:C3").Find(What:=ComboBox1.Value, MatchCase:=False)
If Not Found Is Nothing Then
Found.Offset(, 1).Value = Me.TextBox1.Value
Found.Offset(, 2).Value = Me.TextBox2.Value
End If
End Sub
Private Sub add_Click()
Dim lRow As Long
Dim ws As Worksheet
Set ws = Worksheets("Sheets1")
lRow = ws.Cells(Rows.Count, 2).End(xlUp).Offset(1, 0).Row
With ws
.Cells(lRow, 1).Value = Me.ComboBox1.Value
.Cells(lRow, 2).Value = Me.TextBox1.Value
.Cells(lRow, 3).Value = Me.TextBox2.Value
End With
End Sub