2016-03-29 197 views
-1

我試圖創建一個包含兩個條目的表單: - 文件夾號碼 - 文件夾中的toms清單 這是爲了歸檔目的。表格分爲4個部分,將打印在歸檔盒的標籤上。 文件夾編號從1到1500,其中一些含有1名湯的文件,其中一些人到10。現在,我通過它看起來像這樣的表只複製這樣做manualy:Excel創建自動填充表單

table

我需要在形式上唯一的事情是TOM NUMBER從表中

form

我試圖用VLOOKUP,但它只返回已搜索文件夾數第一排。 所以bascially我想要一個函數,將採取從標籤形式的文件夾號碼,並找到所有的toms被分配並寫在下面。文件夾號碼中的前3位並不重要,只有最後4位被認爲是最重要的變量

+0

你能告訴我「Toms」是否爲文件嗎?如果是的話,他們在哪裏? – JamTay317

+0

沒有,他們只是條目歸檔列表在我們的內部標準名稱: OPO A1W¯¯---------- DL XXX 的文件夾號碼寫成 1XX B20 ZE 122011 YYYY – Arkejn

+0

謝謝我正在研究一個解決方案。它可能需要幾個。 – JamTay317

回答

1

不幸的是,vlookup無法正常工作,您將不得不使用數組文件夾。我假設你將有一個名爲[文件夾] 的表格,並且我將創建一個帶有一些vba的表單表單,以便如何執行此操作。
1.通過選擇文件夾數據集並按下ctl + T創建表。 Folder Table

  • Alt + F11進入的Visual basic編輯
  • 在頂部選擇插入==>用戶窗體
  • ,按F4鍵,在屬性窗口命名形式FileFinder如果不選擇視圖
  • 你的工具箱maynot出現=>工具箱打開
  • 拖2個標籤,列表框2和2個按鈕,你可以格式化你喜歡的。
    enter image description here
    7.Create一個新的模塊一樣添加用戶窗體只能選擇模塊
  • 複製粘貼此代碼

    Public Function CreateWorksheet(Optional name As String = "") As Worksheet Dim ws As Worksheet Set ws = ThisWorkbook.Sheets.Add If name <> "" Then ws.name = name Set Create = ws End Function Public Function LastRow() As Integer 'gets last row from column A LastRow = ActiveSheet.Cells(Rows.count, 1).End(xlUp).Row End Function Public Function DistintFolders() As String() Dim list() As String Dim counter As Integer For Each cell In ActiveSheet.Range("E2:E" & LastRow) If Not IsInList(list, cell.Value, counter) Then counter = counter + 1 ReDim Preserve list(1 To counter) list(counter) = cell.Value End If Next cell DistintFolders = list End Function Public Function TomNumberByFolder(folderName As Variant) As String() Dim list() As String Dim counter As Integer Dim rowNumber As Integer For Each cell In ActiveSheet.Range("B2:B" & LastRow) rowNumber = rowNumber + 1 If IsCorrectFolder(folderName, rowNumber) Then counter = counter + 1 ReDim Preserve list(1 To counter) list(counter) = cell.Value End If Next cell TomNumberByFolder = list End Function Public Function IsInList(ByRef list() As String, compare As String, count As Integer) As Boolean Dim l As Variant If compare = "" Then IsInList = True Exit Function End If If count = 0 Then IsInList = False Exit Function End If For Each l In list If l = compare Then IsInList = True Exit Function End If Next l IsInList = False End Function Public Function IsCorrectFolder(folderName As Variant, rowNumber As Integer) As Boolean IsCorrectFolder = (ActiveSheet.Range("E" & rowNumber).Value = folderName) End Function

  • 雙擊窗體並粘貼此代碼

  • `

    Private Sub btnCancel_Click() 
        Unload Me 
    End Sub 
    
    Private Sub btnCreate_Click() 
    Dim ws As Worksheet 
        If lstTom.ListCount = 0 Then 
         MessageBox "Please select a folder" 
        End If 
        Set ws = ThisWorkbook.Sheets.Add 
        ws.Cells(1, 1).Value = "Tom Number" 
    
        ws.Cells(2, 1).Resize(Me.lstTom.ListCount, 1) = Me.lstTom.list 
    End Sub 
    
    Private Sub lstFolder_Click() 
        Dim folder As String 
        If ActiveSheet.name <> "Data" Then ThisWorkbook.Sheets("Data").Activate 'please name this whatever your datasheet is called 
        For i = 0 To lstFolder.ListCount - 1 
         If lstFolder.Selected(i) Then 
          Me.lstTom.Clear 
    
         For Each s In TomNumberByFolder(lstFolder.list(i)) 
           With lstTom 
            .AddItem s 
           End With 
          Next s 
         End If 
        Next i 
    End Sub 
    
    Private Sub UserForm_Initialize() 
    
        For Each s In DistintFolders 
         With lstFolder 
          .AddItem s 
         End With 
        Next s 
    End Sub 
    

    `
    請注意,您可能需要更改表名稱,如果你想我會送你這個。

    Download Here

    +0

    非常感謝,現在我發現我的表單有另一個問題,或許你可以幫我解決它 – Arkejn

    +0

    有什麼問題? – JamTay317

    +0

    我會在問題 – Arkejn