2017-10-17 80 views
0

我完全是VBA的新手,我試圖編寫一個excel模塊來提取工作簿的每個工作薄上的特定部分,並將它們格式化並輸出到一張新的工作簿上。通過多個工作表的數據提取

到目前爲止,我有這個;

Public Sub extractCol() 

    ' Find FF&E Section, Add 3 rows and Identify relevant columns. 

    Dim rFind As Range 

    With Range("A:A") 
     Set rFind = .Find(What:="FF&E", LookAt:=xlWhole, MatchCase:=False, 
     SearchFormat:=False) 
     If Not rFind Is Nothing Then 

      NumRange = rFind.Row + 3 ' Find FF&E line and add three 
      CRange = "C" & NumRange & ":" & "C" & NumRange + 100 ' Define First 100 
      Lines in Column C 
      ERange = "E" & NumRange & ":" & "E" & NumRange + 100 ' Define First 100 
      Lines in Column E 
      KRange = "K" & NumRange & ":" & "K" & NumRange + 100 ' Define First 100 
      Lines in Column K 
      MRange = "M" & NumRange & ":" & "M" & NumRange + 100 ' Define First 100 
      Lines in Column M 


      Set range1 = Union(Range(CRange), Range(ERange), Range(KRange), 
      Range(MRange)) ' Combine individual column ranges in to one selection 
      range1.Copy ' Copy new combined range 

      Set NewBook = Workbooks.Add ' Open new Workbook 
      ActiveCell.PasteSpecial Paste:=xlPasteValues ' Paste to new Workbook 

     End If 

    End With 

End Sub 

這太好了,因爲它提取了我想要正確的位,但它只做當前工作表。我如何循環執行所有表單?

其次,我想將所有結果粘貼到同一張紙上?

最後,我有下面的腳本提取表格名稱和格式。理想情況下,我想在上面的輸出中添加一列,根據來自哪張表來顯示此數據。

Function FindRoom() 

    shtName = ActiveSheet.Name 

    Dim arr() As String 
    arr = VBA.Split(shtName, " ") 

    xCount = UBound(arr) 
    If xCount < 1 Then 
     FindRoom = "" 
    Else 
     FindRoom = arr(xCount) 
    End If 
End Function 

對不起,我知道這不是一個簡單的答案的問題,但任何幫助,將不勝感激,哪怕它只是指着我正確的方向。

+0

有很多代碼示例說明如何遍歷表單。 For-Next循環是標準的,你只需要引用一個工作表變量。你是否嘗試過這條大道並被卡住了?你的意思是你想要在新的工作簿中獲得所有結果嗎? – SJR

+0

我確實遇到過這個問題,但因爲無法正常工作而卡住了。我希望它遍歷每個工作表並將結果添加到新的工作簿工作表。我的假設是,我需要循環發生,而不是每當它找到最後一行時添加一個新的工作簿,並添加到它設置的第一個工作簿中。 –

+0

你可以發表你已經嘗試過嗎? FindRoom函數的問題在於它在某些情況下返回一個空字符串? – SJR

回答

0

試試這個。我已經添加了一個工作表變量ws。這會將工作表名稱放在新工作簿的A列中,並將數據放在B列中。我還爲所有變量添加了聲明。

Public Sub extractCol() 

'Find FF&E Section, Add 3 rows and Identify relevant columns. 

Dim rFind As Range, CRange As String, ERange As String, KRange As String, MRange As String 
Dim ws As Worksheet 
Dim NewBook As Workbook 
Dim NumRange As Long 

Set NewBook = Workbooks.Add ' Open new Workbook 

For Each ws In ThisWorkbook.Worksheets 
    With ws 
     Set rFind = .Range("A:A").Find(What:="FF&E", LookAt:=xlWhole, MatchCase:=False, SearchFormat:=False) 
     If Not rFind Is Nothing Then 
      NumRange = rFind.Row + 3 ' Find FF&E line and add three 
      CRange = "C" & NumRange & ":" & "C" & NumRange + 100 ' Define First 100 Lines in Column C 
      ERange = "E" & NumRange & ":" & "E" & NumRange + 100 ' Define First 100 Lines in Column E 
      KRange = "K" & NumRange & ":" & "K" & NumRange + 100 ' Define First 100 Lines in Column K 
      MRange = "M" & NumRange & ":" & "M" & NumRange + 100 ' Define First 100 Lines in Column M 

      Set range1 = Union(.Range(CRange), .Range(ERange), .Range(KRange), .Range(MRange)) ' Combine individual column ranges in to one selection 
      range1.Copy ' Copy new combined range 
      NewBook.Sheets(1).Range("B" & Rows.Count).End(xlUp)(2).PasteSpecial Paste:=xlPasteValues ' Paste to new Workbook 
      NewBook.Sheets(1).Range("A" & Rows.Count).End(xlUp)(2).Resize(range1.Rows.Count).Value = FindRoom(ws) 
     End If 
    End With 
Next ws 

End Sub 

Function FindRoom(ws As Worksheet) 

    shtName = ws.Name 

    Dim arr() As String 
    arr = VBA.Split(shtName, " ") 

    xCount = UBound(arr) 
    If xCount < 1 Then 
     FindRoom = "" 
    Else 
     FindRoom = arr(xCount) 
    End If 
End Function 
+0

它失敗了,因爲它無法運行我認爲的FindRoom功能。一旦我用只是一個字符串替換FindRoom部分,它工作正常。我在哪裏必須將FindRoom功能放在腳本中,以便我可以在這裏調用它?謝謝您的幫助。循環肯定是工作,這給了我希望! –

+0

我的錯誤 - 我更新了函數來添加一個參數,但忘記了包含修改後的代碼。現在就試試。 – SJR

+1

剛剛又去了一次。這是我的錯誤。你的代碼是完美的。十分感謝你的幫助。作品一種享受。 –

相關問題