2017-10-18 213 views
0

我已經編寫了一個代碼,允許用戶選擇一個文件夾,然後遍歷該文件夾中的所有文件,將特定數據列複製到我的主文檔「PQ Analysis電子表格」中。將文本文件導入主excel文檔(VBA)?

我想改進此代碼,使其更通用。

有什麼辦法可以改變它,所以我不必指定'PQ分析電子表格'作爲主文檔?即所以它可以被稱爲任何用戶希望的。

此外,我目前打開每個文件到一個新的工作簿,並從那裏複製。我確定必須有一種方法可以直接從txt文件輸入到數組中,然後從那裏打印?

任何建議,將不勝感激。這是我寫的第一個VBA代碼,所以對於這種語言來說是非常新的!謝謝。

Sub tabdelim() 
Dim strFileToOpen 
Dim InputFile As Workbook 
Dim OutputFile As Workbook 

'Dialogue box to select file to open 
strFileToOpen = Application.GetOpenFilename _ 
(Title:="Please choose a file to open", _ 
FileFilter:="Text Files *.txt* (*.txt*),") 

If strFileToOpen = False Then 
    MsgBox "No file selected.", vbExclamation, "No file selected!" 
    Exit Sub 

Else 
    'Open selected file in new workbook 
    Workbooks.OpenText Filename:= _ 
    strFileToOpen, _ 
    Origin:=65001, StartRow:=1, DataType:=xlDelimited, TextQualifier:= _ 
    xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, Semicolon:=False, _ 
    Comma:=False, Space:=False, Other:=False, FieldInfo:=Array(Array(1, 1), _ 
    Array(2, 1), Array(3, 1), Array(4, 1), Array(5, 1), Array(6, 1), Array(7, 1), Array(8, 1), _ 
    Array(9, 1)), TrailingMinusNumbers:=True 


End If 


Set InputFile = ActiveWorkbook 

'Now, copy what you want from InputFile: 
ActiveSheet.Range("I3:I660").Copy 


'Now, paste to OutputFile worksheet: 
Windows("PQ Analysis spreadsheet.xls").Activate 
Set OutputFile = ActiveWorkbook 
Range("C43").Select 
ActiveSheet.Paste 


'Close InputFile 
InputFile.Close 


End Sub 

example of txt document input

+0

'昏暗strWorkbookName作爲字符串:strWorkbookName =的InputBox( 「選擇工作簿」)' –

+1

使用Power查詢會更簡單,更高效。 – Olly

回答

0

首先,發射器來選擇文件,並開始導入的每個文件:

Sub SelectFilesForImport() 
    Dim fd As FileDialog 
    Dim i As Long 

    'set and determine file picker behaviors 
    Set fd = Application.FileDialog(msoFileDialogFilePicker) 
    fd.AllowMultiSelect = True 

    'Launch file picker, exit if no files selected. 
    'Hold Ctrl to select multiple files. Ctrl+A to select all files 
    If Not fd.Show = -1 Then Exit Sub 

    'Start import selected files, file by file. 
    For i = 1 To fd.SelectedItems.Count 
     Call ImportFile(fd.SelectedItems(i)) 
    Next i 
End Sub 

第二子,進口一行行(W/O在Excel中打開文件)

Private Sub ImportFile(ByVal FilePathAndName As String) 
    Dim DataInTransit As String 
    Dim FileName  As String 
    Dim N    As Integer 

    N = FreeFile 
    Open FilePathAndName For Input As #N 
     Do While Not EOF(N) 
      Line Input #N, DataInTransit 

      ' ################################################## 
      ' Up to this point, "DataInTransit" is a single line text. 
      ' Now it depends on how you want to massage and put it into the worksheet. 
      ' You can also skip lines which do not fit into context _ 
       by adding conditional IF statements. 
      ' Modify below to suit your needs: 
       Arr = Split(DataInTransit, ";") 
       ActiveCell.Resize(1, UBound(Arr) + 1) = Split(DataInTransit, " ") 
       ActiveCell.Offset(1).Activate 
      ' ################################################## 

     Loop 
    Close #N 
End Sub 

對於放置導入的位置,我認爲直接放置到更簡單如上圖第二部分所示,然後偏移到下一行換行。但是如果你有很多計算可能會很慢(可以通過自動計算來解決)。否則,如您所建議的那樣,使用array來收集這些行,然後將它們全部放入工作表中。無論哪種方式,用戶只需選擇開始導入的範圍的左上角(最好提示一條消息讓他們選擇開始放置導入的單元格,否則它們可能會搞亂他們的整個工作表,p/s也允許他們取消提示中的宏以防萬一)。這可以通過簡單地將下面的行添加到第一個子部分來完成。

Dim k 
    k = Application.InputBox("Please select where to place the import.") 
    On Error GoTo Term 'If k is not a range, go to Term 
    Range(k).Activate 
    Exit Sub 

    Term: 
    End