Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel UserForm在创建新数据之前检查现有列表_Excel_Vba - Fatal编程技术网

Excel UserForm在创建新数据之前检查现有列表

Excel UserForm在创建新数据之前检查现有列表,excel,vba,Excel,Vba,我正在寻找一种方法,通过UserForm在我的列表中只获取唯一的ID值 我有大量的文本框被填满了,但在我的整个列表中,一个叫做“CHARGE”的文本框应该只出现一次。因此,我需要一些检查,以便在将新位置添加到列表之前启动宏 我的唯一ID是:Me.E1GCharge 我的代码: Private Sub SaveData() 'Copy input values to sheet. Dim lRow As Long Dim ws As Worksheet Set ws = sheet1 lRow =

我正在寻找一种方法,通过UserForm在我的列表中只获取唯一的ID值

我有大量的文本框被填满了,但在我的整个列表中,一个叫做“CHARGE”的文本框应该只出现一次。因此,我需要一些检查,以便在将新位置添加到列表之前启动宏

我的唯一ID是:
Me.E1GCharge

我的代码:

Private Sub SaveData()
'Copy input values to sheet.
Dim lRow As Long
Dim ws As Worksheet
Set ws = sheet1
lRow = ws.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).row
With ws

'ME.E1GCharge should exist only one time in my list. 
'If i try to make by mistake it one more time it should be not possible
    .Cells(lRow, 1).Value = Me.E1GCharge.Value
    .Cells(lRow, 3).Value = Me.E1GMatName.Value
    .Cells(lRow, 4).Value = Me.E1Gtype.Value
    .Cells(lRow, 5).Value = Me.E1GMatNumber.Value
    .Cells(lRow, 6).Value = Format(Me.E1GExpiryDate.Value, "mmm.yyyy")
    .Cells(lRow, 7).Value = Me.E1GBoxPcs.Value
    .Cells(lRow, 8).Value = Me.E1GAmmount.Value
    .Cells(lRow, 9).Value = Me.E1GUnit.Value
    .Cells(lRow, 10).Value = Me.E1Gkonz.Value

End With
'Clear input controls.
Me.E1GMatName.Value = ""
Me.E1Gtype.Value = ""
Me.E1GMatNumber.Value = ""
Me.E1GExpiryDate.Value = ""
Me.E1GBoxPcs.Value = ""
Me.E1GAmmount.Value = ""
Me.E1Gkonz.Value = ""
Me.E1GUnit.Value = ""

Call GetData

End Sub

您可以循环查看所有现有的Vlaue,并检查您要添加的费用是否已经存在:

Dim i as Integer
For i = 1 To lRow
  If ws.Cells(i, 1).Value = Me.E1GCharge.Value then
    Msgbox "This Value already exists!"
    Exit Sub
  End If
next i