2016-05-17 77 views
0

所以我有一個名爲「商業計劃」,我有一個下拉列表單元格A2中的下拉菜單,這是一個名爲「設施」的範圍下拉選擇和所有儀表板數據被查找驅動。我想要做的是首先創建一個新的工作簿,而不是每個下拉選擇的新選項卡,選項卡的格式相同,但數據粘貼爲值。我試圖創建下面的代碼來將每個下拉選擇保存爲PDF,但我一直不成功。任何關於如何讓代碼工作的見解都會很棒。宏循環下拉,併爲每個下拉選擇創建一個工作表

Sub Worksheet_Generator() 

    Dim cell As Range 
    Dim wsSummary As Worksheet 
    Dim counter As Long 

    Set wsSummary = Sheets("Business Plans") 

    For Each cell In Worksheets("dd").Range("$C3:$C75") 
     If cell.Value = "" Then 
      counter = counter + 1 
      Application.StatusBar = "Processing file: " & counter & "/1042" 
     Else 
      counter = counter + 1 
      Application.StatusBar = "Processing file: " & counter & "/1042" 

      With wsSummary 
       .Range("$A$2").Value = cell.Value 
       ActiveSheet.Copy After:=Worksheets(Worksheets.Count) 
       ActiveSheet.Copy 
       With ActiveSheet.UsedRange 
        .Value = .Value 
       End With 
      End With 
     End If 
    Next cell 

Set wsSummary = Nothing 
End Sub 
+0

如果我們能夠獲得一些您正在嘗試完成的示例數據,那對我們能夠幫助您是非常有幫助的。此外,還有幾行代碼可以通過移動它們輕鬆刪除,在編寫代碼時儘量不要重複。 – Histerical

+0

我有點困惑。您想爲組合框中的每個選定項目創建新的工作簿/工作表,還是以PDF格式導出? – NuWin

+0

@NuWin無我想要的是我的下拉菜單中的每個選擇我想爲它創建一個選項卡,但我希望所有數據都是值。當前每個下拉觸發查找公式用於數據操作。我最好希望在新工作簿中新創建的選項卡,但同樣的工作簿也可以。 – user3666237

回答

0

我認爲你正在尋找類似於下面的東西(改編自copying-dynamic-rows-into-new-workbook-and-save-it)。

Option Explicit 
Sub grabber() 
    Dim thisWb As Workbook: Set thisWb = ThisWorkbook 
    Dim thisWs As Worksheet: Set thisWs = thisWb.Worksheets("dd") 'replace with relevant name 
    Dim newBook As Workbook 
    Dim newws As Worksheet 
    Dim pathToNewWb As String 
    Dim uKeys 
    Dim currentPath, columnWithKey, numCols, numRows, uKey, dataStartRow, columnKeyName 

    'nobody likes flickering screens 
    Application.ScreenUpdating = False 
    'remove any filter applied to the data 
    thisWs.AutoFilterMode = False 

    'get the path of the workbook folder 
    currentPath = Application.ThisWorkbook.Path 

    'Set the stage 
    '###Hardcode### 
    columnKeyName = "Facility" 'name of the column with the facility values 
    dataStartRow = 4 'this is a pure guess, correct as relevenat. Use the header row index 
    pathToNewWb = currentPath & "/Business Plans.xlsx" ' where to put the new excel, if you want a saveas prompt you should google "Application.FileDialog(msoFileDialogSaveAs)" 
    uKeys = Range("Facilities").Value 
    '###Hardcode End### 
    columnWithKey = thisWs.Range(dataStartRow & ":" & dataStartRow).Find(what:=columnKeyName, LookIn:=xlValues).Column 
    numCols = thisWs.UsedRange.Columns.Count 

    'extract the index of the last used row in the worksheet 
    numRows = thisWs.UsedRange.Rows.Count 

    'create the new workbook 
    Set newBook = Workbooks.Add 

    'loop the facilities, and do the work 
    For Each uKey In uKeys 

     'Filter the keys column for a unique key 
     thisWs.Range(thisWs.Cells(dataStartRow, 1), thisWs.Cells(numRows, numCols)).AutoFilter field:=columnWithKey, Criteria1:=uKey 

     'copy the sheet 
     thisWs.UsedRange.Copy 

     'Create a new ws for the facility, and paste as values 
     Set newws = newBook.Worksheets.Add 
     With newws 
      .Name = uKey 'I assume the name of the facility is the relevant sheet name 
      .Range("A1").PasteSpecial xlPasteValues 
     End With 

     'remove autofilter (paranoid parrot) 
     thisWs.AutoFilterMode = False 

    Next uKey 

    'save the new workbook 
    newBook.SaveAs pathToNewWb 
    newBook.Close 

End Sub 

編輯:

由於我還沒有看到你的數據,如果它需要一些修改,我不會感到驚訝。

首先,我嘗試對包含數據(### Hardcode ###位)的工作表「dd」的範圍進行「構建」,定義輸出的路徑,並確定可以過濾的列對應於命名範圍「設施」的值。

我檢索命名範圍「Facilities」(進入uKeys)的值,​​並創建輸出工作簿(newBook)。然後我們遍歷for循環中uKeys的每個值(uKey)。在循環中,我爲uKey應用了一個自動過濾器。過濾後,在newBook中創建工作表(newWs),並將過濾的工作表「dd」複製粘貼到newWs中。然後關閉自動過濾器,工作表「dd」返回到未過濾狀態。

最後,我們將newBook保存到所需位置,然後關閉它。

+0

N謝謝你的努力,但是你的代碼讓我失去了你可以請你解釋一下 – user3666237

+0

我已經做了一個嘗試的編輯,但如果你能讓我知道你在哪裏丟失,我會更好地幫助你 –

+0

我得到在說Hardcode的線上有錯誤。我不知道我是否想定製它。爲了解釋我想要做的是每次下拉選擇都會更改數據,並且對於每個下拉選擇,我希望首先在該工作簿中爲每個下拉選擇創建一個新工作簿和一個新選項卡。 – user3666237