Live-Forum - Die aktuellen Beiträge
Datum
Titel
28.03.2024 21:12:36
28.03.2024 18:31:49
Anzeige
Archiv - Navigation
1220to1224
Aktuelles Verzeichnis
Verzeichnis Index
Übersicht Verzeichnisse
Vorheriger Thread
Rückwärts Blättern
Nächster Thread
Vorwärts blättern
Anzeige
HERBERS
Excel-Forum (Archiv)
20+ Jahre Excel-Kompetenz: Von Anwendern, für Anwender
Inhaltsverzeichnis

Datensatz über Userform Nr vergeben

Datensatz über Userform Nr vergeben
Matthias
Hallo zusammen,
habe mir eine Userform aufgebaut, wo ich noch folgendes brauche:
sobald ich die DATEN aus den Textboxen in die Zellen schreibe, soll nun noch dem Datensatz ein Nr/ID zugewiesen werden. (in der selben Zeile)
Sollte später aus den eingegeben Daten ein Datensatz gelöscht werden, darf die Nr/ID nicht nochmals auftauchen.
verwendetes Makro Datensatz schreiben:
Private Sub CommandButton2_Click()
Dim xZeile As Long
If TextBox1 = "" Then Exit Sub
If ComboBox1.ListIndex = 0 Then
xZeile = [A65536].End(xlUp).Row + 1
Else
xZeile = ComboBox1.ListIndex + 1
End If
Worksheets("Kunden").Visible = True
Worksheets("Kunden").Unprotect
Cells(xZeile, 1) = TextBox1
Cells(xZeile, 2) = TextBox2
Cells(xZeile, 3) = TextBox3
Cells(xZeile, 4) = TextBox4
Cells(xZeile, 5) = TextBox5
Cells(xZeile, 6) = TextBox6
Cells(xZeile, 7) = TextBox7
Cells(xZeile, 8) = TextBox8
TextBox1 = ""
TextBox2 = ""
TextBox3 = ""
TextBox4 = ""
TextBox5 = ""
TextBox6 = ""
TextBox7 = ""
TextBox8 = ""
Columns("A:I").Sort Key1:=Range("A2"), Order1:=xlAscending, Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
UserForm_Initialize
Worksheets("Kunden").Protect
End Sub
Die Nr/Id muss in Spalte I (9)
Und Makro:
Private Sub UserForm_Initialize()
Dim aRow, i As Long
Application.EnableEvents = False
ComboBox1.Clear
aRow = [A65536].End(xlUp).Row
ComboBox1.AddItem "neue Person hinzufügen"
For i = 2 To aRow
ComboBox1.AddItem Cells(i, 1) & ", " & Cells(i, 2)
Next i
ComboBox1.ListIndex = 0
Application.EnableEvents = True
End Sub
Makro zum löschen:
Private Sub CommandButton1_Click()
Worksheets("Kunden").Visible = True
Worksheets("Kunden").Unprotect
If ComboBox1.ListIndex > 0 Then
Rows(ComboBox1.ListIndex + 1).Delete
TextBox1 = ""
TextBox2 = ""
TextBox3 = ""
TextBox4 = ""
TextBox5 = ""
TextBox6 = ""
TextBox7 = ""
TextBox8 = ""
UserForm_Initialize
Worksheets("Kunden").Protect
End If
End Sub

Danke für jegliche Hilfe.

5
Beiträge zum Forumthread
Beiträge zu diesem Forumthread

Betreff
Benutzer
Anzeige
AW: Datensatz über Userform Nr vergeben
10.07.2011 12:27:58
Josef

Hallo Matthias,
da gibt's viele Möglichkeiten. Hier eine bei der die Nummer in den Dokumentegenschaften hinterlegt wird.
Diesen Code in eine allgemeines Modul.
' **********************************************************************
' Modul: Modul1 Typ: Allgemeines Modul
' **********************************************************************

Option Explicit

Private Function GetCustProp(propName As String, Optional propValue As Variant) As Variant
  ' Wert aus Dateieigenschaft auslesen. Wenn nicht vorhanden
  ' Anlegen und Optional mit Startwert belegen
  
  Dim propType As MsoDocProperties
  
  If Not IsMissing(propValue) Then
    Select Case VarType(propValue)
      Case vbString
        propType = msoPropertyTypeString
      Case vbBoolean
        propType = msoPropertyTypeBoolean
      Case vbByte, vbInteger, vbLong
        propType = msoPropertyTypeNumber
      Case vbSingle, vbDouble
        propType = msoPropertyTypeFloat
      Case vbDate
        propType = msoPropertyTypeDate
      Case Else
    End Select
  End If
  
  With ThisWorkbook
    On Error GoTo NoName
    GetCustProp = .CustomDocumentProperties(propName).Value
    Exit Function
    NoName:
    If Err.Number = 5 Then
      Err.Clear
      .CustomDocumentProperties.Add _
        Name:=propName, _
        LinkToContent:=False, _
        Type:=propType, _
        Value:=propValue
      GetCustProp = propValue
    End If
  End With
End Function


Private Function SetCustProp(propName As String, propValue As Variant)
  ' Wert in Dateieigenschaft schreiben. Wenn nicht vorhanden
  ' Anlegen und Wert eintragen
  
  Dim propType As MsoDocProperties
  
  Select Case VarType(propValue)
    Case vbString
      propType = msoPropertyTypeString
    Case vbBoolean
      propType = msoPropertyTypeBoolean
    Case vbByte, vbInteger, vbLong
      propType = msoPropertyTypeNumber
    Case vbSingle, vbDouble
      propType = msoPropertyTypeFloat
    Case vbDate
      propType = msoPropertyTypeDate
    Case Else
  End Select
  
  With ThisWorkbook
    On Error GoTo NoName
    .CustomDocumentProperties(propName).Value = propValue
    Exit Function
    NoName:
    If Err.Number = 5 Then
      Err.Clear
      .CustomDocumentProperties.Add _
        Name:=propName, _
        LinkToContent:=False, _
        Type:=propType, _
        Value:=propValue
    End If
  End With
End Function


Public Function newID() As String
  Dim lngID As Long
  lngID = GetCustProp("nextID", 0)
  newID = "ID" & Format(lngID + 1, "00000")
  SetCustProp "nextID", lngID + 1
End Function


Und den Code von CommandButton2 so ändern.
Private Sub CommandButton2_Click()
  Dim xZeile As Long
  If TextBox1 = "" Then Exit Sub
  If ComboBox1.ListIndex = 0 Then
    xZeile = [A65536].End(xlUp).Row + 1
  Else
    xZeile = ComboBox1.ListIndex + 1
  End If
  Worksheets("Kunden").Visible = True
  Worksheets("Kunden").Unprotect
  Cells(xZeile, 1) = TextBox1
  Cells(xZeile, 2) = TextBox2
  Cells(xZeile, 3) = TextBox3
  Cells(xZeile, 4) = TextBox4
  Cells(xZeile, 5) = TextBox5
  Cells(xZeile, 6) = TextBox6
  Cells(xZeile, 7) = TextBox7
  Cells(xZeile, 8) = TextBox8
  If Cells(xZeile, 9) = "" Then Cells(xZeile, 9) = newID
  TextBox1 = ""
  TextBox2 = ""
  TextBox3 = ""
  TextBox4 = ""
  TextBox5 = ""
  TextBox6 = ""
  TextBox7 = ""
  TextBox8 = ""
  
  Columns("A:I").Sort Key1:=Range("A2"), Order1:=xlAscending, Header:=xlGuess, _
    OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
  UserForm_Initialize
  Worksheets("Kunden").Protect
End Sub



« Gruß Sepp »

Anzeige
Danke, das hätt ich nie geschafft!
10.07.2011 13:09:57
Matthias
Hallo Josef,
besten Danke, ich hätte das nie nicht hinbekommen,
bin da einfach neidisch drauf wie ihr das immer hinbekommt,
aber genau das ist das, was mich wiederum anspornt dazu zu lernen.
Gruß Matthias
ZusatzFrage: ID auf "0" setzen ?
10.07.2011 16:17:04
Matthias
Hallo,
hab da glatt noch ein Problemchen, wie setzte ich den Zähler bzw. die ID auf NUll ,
wenn die Datenbank mal gelöscht wird.
Thx , Gruß matthias
AW: ZusatzFrage: ID auf "0" setzen ?
10.07.2011 16:38:24
Josef

Hallo Matthias,
das geht am einfachsten so.

Sub resetID()
  On Error Resume Next
  ThisWorkbook.CustomDocumentProperties("nextID").Delete
End Sub



« Gruß Sepp »

Anzeige
Danke! 1a
10.07.2011 17:10:51
Matthias
Thx - Josef,..........
Gruß Matthias

Links zu Excel-Dialogen

Beliebteste Forumthreads (12 Monate)

Anzeige

Beliebteste Forumthreads (12 Monate)

Anzeige
Anzeige
Anzeige