Live-Forum - Die aktuellen Beiträge
Anzeige
Archiv - Navigation
1720to1724
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

Frage zu einer UserForm

Frage zu einer UserForm
15.11.2019 11:01:06
Michael
Hallo Allerseits
Ich habe eine UserForm , die mir nach Eingabe der Such-Parameter, viele .xlsx Dateien durchsucht und eine Liste mit den gefundenen Daten ausgibt. Soweit funktioniert auch alles gut. Die Sache hat nur einen kleinen Mangel. Ich kann mir diese Liste nur anschauen aber nicht kopieren, um sie weiter zu bearbeiten. Was mir fehlt ist eine Möglichkeit diese zu exportieren. Ob in einen neuen Sheet oder in eine neue .xlsx ist dabei egal. Es würde auch schon reichen wenn ich die Liste von Hand markieren und kopieren könnte.
Ich hoffe ihr wisst Rat und Hilfe
Viele Grüße Michael
Hier der Code der UserForm

  • Option Explicit
    Private Const SEARCH_PATH As String = "\\SERVERNEU\Daten\lager\Listen und Faxe\Psionersatzlisten\2019\"
    ---------------------------------------------------------------------------------------------------------------------------------------------
    Private Sub cmdClose_Click()
    Unload Me
    End Sub
    

    ----------------------------------------------------------------------------------------------------------------------------------------------
    Private Sub cmdDirectory_Click()
    Dim strPath As String
    With Application.FileDialog(msoFileDialogFolderPicker)
    .InitialFileName = txtDirectory
    .Title = "Suche in Dateien Ordnerauswahl"
    .ButtonName = "Auswählen"
    .InitialView = msoFileDialogViewList
    If .Show = -1 Then
    strPath = .SelectedItems(1)
    If Right(strPath, 1)  "\" Then strPath = strPath & "\"
    End If
    End With
    If Len(strPath) Then
    txtDirectory = strPath
    Call SetCustProp("SearchPath", strPath)
    End If
    End Sub
    

    -------------------------------------------------------------------------------------------------------------------------------------------
    Private Sub cmsSearch_Click()
    Dim strfile As String, strFirst As String
    Dim objWB As Workbook, rng As Range
    Dim lngRow As Long, varRet As Variant, varSearch As Variant, varNum As Variant, varItemS As  _
    Variant, varItemN As Variant
    If Dir(txtDirectory, vbDirectory)  "" Then
    If Right(txtDirectory, 1)  "\" Then txtDirectory = txtDirectory & "\"
    If Len(txtName) Then
    If Len(txtNumber) Then
    With Application
    .ScreenUpdating = False
    .EnableEvents = False
    .AskToUpdateLinks = False
    .DisplayAlerts = False
    .Calculation = xlCalculationManual
    End With
    varSearch = Split(txtName, ",")
    varNum = Split(txtNumber, ",")
    lstResult.Clear
    strfile = Dir(txtDirectory & "*.xls*", vbNormal)
    Do While Len(strfile)
    strFirst = ""
    Set objWB = Workbooks.Open(txtDirectory & strfile)
    With objWB.Sheets("Tabelle1")
    For Each varItemS In varSearch
    Set rng = .Columns(1).Find(What:=varItemS, LookAt:=xlWhole, LookIn:=xlValues,  _
    MatchCase:=False, SearchFormat:=False, After:=.Cells(1, 1))
    If Not rng Is Nothing Then
    strFirst = rng.Address
    Do
    lngRow = Int((rng.Row - 5) / 22) * 22 + 6
    For Each varItemN In varNum
    If IsNumeric(varItemN) Then
    varRet = Application.Match(CLng(varItemN), .Rows(lngRow), 0)
    If IsNumeric(varRet) Then
    If .Cells(rng.Row, varRet) > 0 Then
    lstResult.AddItem strfile
    lstResult.List(lstResult.ListCount - 1, 1) = varItemS & " / " &  _
    varItemN
    lstResult.List(lstResult.ListCount - 1, 2) = .Cells(lngRow - 1,  _
    varRet)
    lstResult.List(lstResult.ListCount - 1, 3) = .Cells(rng.Row, varRet)
    lstResult.List(lstResult.ListCount - 1, 4) = txtDirectory & strfile &  _
    "|Tabelle1|" & .Cells(rng.Row, varRet).Address
    End If
    End If
    End If
    Next
    Set rng = .Columns(1).FindNext(rng)
    Loop While Not rng Is Nothing And rng.Address  strFirst
    End If
    Next
    .Parent.Close False
    End With
    strfile = Dir
    Loop
    If lstResult.ListCount = 0 Then
    lstResult.AddItem "Kein Treffer!"
    Else
    Call SortListBox(lstResult, 0, lstResult.ListCount - 1, 0)
    End If
    Else
    MsgBox "Bitte geben Sie eine Nummer ein!", vbInformation
    End If
    Else
    MsgBox "Bitte geben Sie einen Namen an!", vbInformation
    End If
    Else
    MsgBox "Verzeichnis nicht gefunden!", vbInformation
    End If
    ErrorHandler:
    With Application
    .ScreenUpdating = True
    .EnableEvents = True
    .AskToUpdateLinks = True
    .DisplayAlerts = True
    .Calculation = xlCalculationAutomatic
    End With
    Set objWB = Nothing
    End Sub
    

    ------------------------------------------------------------------------------------------------------------------------------------------
    Private Sub lstResult_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
    With lstResult
    If .ListIndex > -1 Then
    If .List(.ListIndex, 0)  "Kein Treffer!" Then
    Call Workbooks.Open(Split(.List(.ListIndex, 4), "|")(0))
    Call Application.GoTo(Sheets(Split(.List(.ListIndex, 4), "|")(1)).Range(Split(.List(. _
    ListIndex, 4), "|")(2)), False)
    End If
    End If
    End With
    End Sub
    

    Private Sub txtDirectory_Change()
    End Sub
    

    ---------------------------------------------------------------------------------------------------------------------------------------------
    Private Sub UserForm_Activate()
    txtDirectory = GetCustProp("SearchPath", SEARCH_PATH)
    End Sub
    

    Private Sub SortListBox(ByRef TheBox As MSForms.ListBox, LowerBound As Long, UpperBound As Long, _
    SortColumn As Byte)
    'original by Nepumuk
    Dim lngIndex1 As Long, lngIndex2 As Long
    Dim vntTemp As Variant, vntBuffer As Variant
    Dim bytIndex As Byte
    lngIndex1 = LowerBound
    lngIndex2 = UpperBound
    vntTemp = TheBox.List((LowerBound + UpperBound) \ 2, SortColumn)
    Do
    Do While TheBox.List(lngIndex1, SortColumn)  lngIndex2
    If LowerBound 

    ------------------------------------------------------------------------------------------------------------------------------------------
    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
    


  • 7
    Beiträge zum Forumthread
    Beiträge zu diesem Forumthread

    Betreff
    Datum
    Anwender
    Anzeige
    AW: Frage zu einer UserForm
    15.11.2019 11:24:10
    Hajo_Zi
    Zu Deiner Datei kann ich nichts schreiben, was wohl daran liegt das ich nicht auf fremde Rechner schaue.Ich baue keine Datei nach.
    Sollte die Datei verlinkt werden?

    AW: Frage zu einer UserForm
    15.11.2019 12:34:39
    Michael
    Hallo Hajo_Zi
    Die Listeneinträge sind tatsächlich verlinkt. Wenn das störend ist, kann das aber entfallen.
    Habe eine Beispieldatei hochgeladen. Die Dateien die Durchsucht werden sehen alle so aus wie der Sheet "EXVOOR". Habe aber darauf verzichtet davon auch einige hochzuladen.
    Viele Grüße Michael
    Beispieldatei: https://www.herber.de/bbs/user/133237.zip
    Anzeige
    AW: Frage zu einer UserForm
    15.11.2019 13:29:24
    Hajo_Zi
    Hallo Michael.
    intressant wäre muss ich eingeben um was zu finden? Ich teste das jetzt nicht aus.
    Warum Ordner? Es wird doch in Tabelle1 gesucht.
    Gruß Hajo
    AW: Frage zu einer UserForm
    15.11.2019 14:18:20
    fcs
    Hallo Michael,
    folgendes Makro sollte funktionieren, um den Inhalt der Listbox im Userform in ein Tabellenblatt zu übertragen.
    Die Spaltentitel im Code musst du noch anpassen.
    Ebenso die Case-Zeile, wenn bestimmte Spalten der Listbox ohne vorangestelltes "'" eingetragen werden sollen.
    Das Makro kannst dann entweder über eine zusätzliche Schalfläche starten oder du startest es automatisch nachdem das Makro die Sortierung der Listbox-Inhalte ausgeführt hat.
    LG
    Franz
    Sub ExportResults()
    'Inhalt der Listbox lstResult in Tabellenblatt übertragen
    Dim wks As Worksheet
    Dim Zeile As Long
    Dim spaBox As Integer, zeiBox As Integer
    'Neue Arbeitsmappe mit einem Tabellenblatt anlegen und Spaltentitel für Ausgabe
    Workbooks.Add Template:=xlWBATWorksheet
    Set wks = ActiveWorkbook.Worksheets(1)
    Zeile = 1
    With wks
    .Cells(Zeile, 1) = "Ordner"
    .Cells(Zeile, 2) = Me.txtDirectory
    Zeile = 3
    .Cells(Zeile, 1) = "file"
    .Cells(Zeile, 2) = "ItemS / ItemN "
    .Cells(Zeile, 3) = "Spa 03"
    .Cells(Zeile, 4) = "Spa 04"
    .Cells(Zeile, 5) = "Zell-Adresse"
    End With
    With Me.lstResult
    For zeiBox = 0 To .ListCount - 1
    Zeile = Zeile + 1
    For spaBox = 0 To .ColumnCount - 1
    Select Case spaBox
    Case 99 'für Zahlen oder wenn kein ' vorangestelt werden soll -  _
    Spaltennummern ggf. ergänzen
    wks.Cells(Zeile, spaBox + 1).Value = .List(zeiBox, spaBox)
    Case Else 'für Texte und Ziffernfolgen
    wks.Cells(Zeile, spaBox + 1).Value = "'" & .List(zeiBox, spaBox)
    End Select
    Next
    Next
    End With
    End Sub
    

    Anzeige
    AW: Frage zu einer UserForm
    15.11.2019 13:36:57
    Piet
    Hallo Michael
    du hast dorch bereits einen schönen Code der alles auflistet.
    Erweitere ihn einfach auf eine "Suchliste" die mit auflistet!
    Setze oben in Dim die neuen Dim Variablen und erweitere nur die For Next Schleife um den Teil der in der Suchliste auflistet. Die Tabelle "Suchliste" musst du natürlich neu erstellen. Dann sollte es klappen. Würde mich freuen wenn du damit klar kommst ....
    mfg Piet
    
    Dim SuLi As Worksheet, z As Long     'Suchliste deklarieren
    Set SuLi = Worksheets("Suchliste")   'Set Suchliste
    z = 2 '1.Zeile in Suchliste
    'For Next Schleife nur um Suchliste erweitern! das ist alles
    For Each varItemN In varNum
    If IsNumeric(varItemN) Then
    varRet = Application.Match(CLng(varItemN), .Rows(lngRow), 0)
    If IsNumeric(varRet) Then
    If .Cells(rng.Row, varRet) > 0 Then
    lstResult.AddItem strfile
    lstResult.List(lstResult.ListCount - 1, 1) = varItemS & " / " &  _
    varItemN
    lstResult.List(lstResult.ListCount - 1, 2) = .Cells(lngRow - 1,  _
    varRet)
    lstResult.List(lstResult.ListCount - 1, 3) = .Cells(rng.Row, varRet)
    lstResult.List(lstResult.ListCount - 1, 4) = txtDirectory & strfile &  _
    "|Tabelle1|" & .Cells(rng.Row, varRet).Address
    'alles parallel in "Suchliste" auflisten
    SuLi.Cells(z, 1) = z - 1
    SuLi.Cells(z, 2) = varItemS & " / " & varItemN
    SuLi.Cells(z, 3) = .Cells(lngRow - 1, varRet)
    SuLi.Cells(z, 4) = .Cells(rng.Row, varRet)
    SuLi.Cells(z, 5) = txtDirectory & strfile & "|Tabelle1|" & .Cells(rng. _
    Row, varRet).Address
    z = z + 1   'next Zeile
    End If
    End If
    End If
    Next
    

    Anzeige
    Hier ein Vorschlag zum Export
    15.11.2019 13:50:57
    EtoPHG
    Hallo Michael,
    Füge deiner Userform noch einen Button hinzu, mit dem du die Resultate exportieren kannst und weise diesem Button diesen Code (in der UF-Klasse) zu:
    Private Sub CommandButton1_Click()
    Const wsName As String = "ListExport"
    Dim wsExport As Worksheet
    Dim rX As Long, cX As Long
    If lstResult.ListCount = 0 Then
    MsgBox "FEHLER: Es sind noch keine Einträge in der Liste!", vbExclamation, wsName
    Exit Sub
    Else
    With ThisWorkbook
    On Error Resume Next
    Set wsExport = .Worksheets(wsName)
    On Error GoTo 0
    If wsExport Is Nothing Then
    With .Worksheets.Add(after:=.Worksheets(.Worksheets.Count))
    .Name = wsName
    End With
    Set wsExport = .Worksheets(wsName)
    End If
    End With
    End If
    wsExport.UsedRange.Clear
    With lstResult
    For rX = 2 To .ListCount + 1
    For cX = 1 To 5
    wsExport.Cells(rX, cX) = .List(rX - 2, cX - 1)
    Next cX
    Next rX
    End With
    MsgBox "Liste wurde nach " & wsName & " exportiert", vbInformation, wsName
    End Sub
    
    Gruess Hansueli
    Anzeige
    AW: Frage zu einer UserForm
    15.11.2019 23:59:29
    Michael
    Vielen Dank an alle die mir geholfen haben, habe die Varianten ausprobiert. Alles läuft prima.
    Sorry für die späte Rückmeldeung, musste unerwartet zu einem Kunden.
    Gruß
    Michael

    Links zu Excel-Dialogen

    Beliebteste Forumthreads (12 Monate)

    Anzeige

    Beliebteste Forumthreads (12 Monate)

    Anzeige
    Anzeige
    Anzeige