2014-09-09 2 views
0

C 친애하는 SO-커뮤니티자동 가져 오기 (매일, CSV 및 XLS -> XLS (m))

내가 다음과 같은 문제/도전있어

:

나는 자동으로 매일 가져와야 어떤 데이터를 하나의 "master-xls"에 저장합니다. 원본 데이터와 통합 된 데이터가 동일한 구조로 구성되어 있습니다 (아래의 예제를 참조하십시오)

VBA (권장) 또는 VBA없이 - 데이터를 자동으로 가져올 수 있습니까? 소스 파일 (파일 이름은 문자열과 실제 날짜의 조합입니다)을 "대상 파일"에 넣으십시오.

도움말 및 팁을 매우 높이 평가합니다! Plz는 이미 올바른 예제를 제시하는 대신 올바른 방향으로 나를 가리 킵니다.

새 소스 파일의 데이터가 이미 존재하는 데이터에 추가되는 것이 중요합니다.

최고의 소원, 누가 복음

소스 파일 :
* source 1
* source 2

마스터 파일
* master xls

답변

0

마침내 csv 가져 오기를 자동화했습니다. 솔루션의 일부는 원래 여기에서 찾을 수 있습니다

Sub listfiles_dir() 
Dim objFSO As Object 
Dim objFolder As Object 
Dim objFile As Object 
Dim i As Integer 

Dim lastrow As Integer 
Dim lastcolumn As Integer 

Dim wb As Excel.Workbook 
Dim ws As Excel.Worksheet 

Dim header As Boolean 
header = True 

Set wb = ActiveWorkbook 
Set ws = wb.Sheets("raw") 
ws.Activate 

ws.Cells.ClearContents 

Application.DisplayAlerts = False 
Application.ScreenUpdating = False 

'Create an instance of the FileSystemObject 
Set objFSO = CreateObject("Scripting.FileSystemObject") 
'Get the folder object 
'Set objFolder = objFSO.GetFolder(".\data") 
Set objFolder = objFSO.GetFolder(ThisWorkbook.Path & "\data") 


i = 1 
'loops through each file in the directory and prints their names and path 
For Each objFile In objFolder.Files 
    'print file name 
    'Cells(i + 1, 1) = objFile.Name 
    'print file path 
    'Cells(i + 1, 2) = objFile.Path 
    i = i + 1 

    Debug.Print (objFile.Path) 

    If header = True Then 
     lastrow = 5 
    Else 
     lastrow = ws.Range("A" & Rows.Count).End(xlUp).row + 1 'gets you the last row 
    End If 

    Call import_csv(ws, objFile.Path, header, lastrow) 

    lastcolumn = ws.Range("$A$" & CStr(lastrow)).End(xlToRight).Column + 1 
    Cells(lastrow, lastcolumn) = objFile.Name 

    Debug.Print (lastcolumn) 

    If header = True Then 
     header = False 
    End If 

Next objFile 

Application.DisplayAlerts = True 
Application.ScreenUpdating = True 

End Sub 


'import files 


Sub import_csv(sheet As Worksheet, fname As String, header As Boolean, row As Integer) 
' 
' importCSV Macro 
' 
Dim startingrow As Integer 
startingrow = 1 

If header = False Then 
    startingrow = 2 
End If 

Debug.Print ("$A$" & CStr(row)) 



With sheet.QueryTables.Add(Connection:= _ 
    "TEXT;" & fname, Destination:=Range(_ 
    "$A$" & CStr(row))) 
    .FieldNames = True 
    .RowNumbers = False 
    .FillAdjacentFormulas = False 
    '.PreserveFormatting = True 
    .RefreshOnFileOpen = False 
    .RefreshStyle = xlInsertDeleteCells 
    .SavePassword = False 
    '.SaveData = True 
    .AdjustColumnWidth = True 
    .RefreshPeriod = 0 
    .TextFilePromptOnRefresh = False 
    .TextFileStartRow = startingrow 
    .TextFileParseType = xlDelimited 
    .TextFileTextQualifier = xlTextQualifierDoubleQuote 
    .TextFileConsecutiveDelimiter = False 
    .TextFileTabDelimiter = False 
    .TextFileSemicolonDelimiter = True 
    .TextFileCommaDelimiter = False 
    .TextFileSpaceDelimiter = False 
    .TextFileTrailingMinusNumbers = True 
    .Refresh BackgroundQuery:=False 
End With 
End Sub 
아래 http://software-solutions-online.com/2014/03/05/list-files-and-folders-in-a-directory/

내 솔루션입니다

0

내가 정확한 방향을 가리 키도록하겠습니다, 내가 너를 올바르게 이해한다고 가정 할 때.

당신이 열고 Excel 스프레드 시트에서 읽을하고자하는 경우

이 유용 할 것입니다 :

Dim cnn As ADODB.Connection 
Dim rst As ADODB.Recordset 
Dim cmd As ADODB.Command 

'Set up the Connection to Excel 
Set cnn = New ADODB.Connection 
With cnn 
    .Provider = "Microsoft.ACE.OLEDB.12.0" 'or whatever your provider is 
    .ConnectionString = "Data Source="C:\My_source_file.xlsx';Extended Properties='Excel 12.0 Xml;HDR=NO;IMEX=1';" 
    .Open 
End With 

'Set up the command to get all that mess out the spreadsheet. 
Set cmd = New ADODB.Command 
With cmd 
    .ActiveConnection = cnn 
    .CommandText = "SELECT * FROM [WhateverSheetHasMyData$]" 
End With 

'Load up the recordset with everything in the worksheet. 
Set rst = New ADODB.Recordset 
With rst 
    .CursorLocation = adUseClient 
    .CursorType = adOpenDynamic 
    .LockType = adLockOptimistic 
    .Open cmd 
End With 

이것은 당신이 당신이 가고 싶은 방향으로 가야한다. 다른 스프레드 시트 또는 데이터베이스 테이블과 같은 다른 문서에로드 한 데이터를 입금하는 명령을 사용하는 방법을이 방법에서 추정 할 수 있습니다.

... 
Dim ws As Excel.Worksheet 
Dim lastrow As Integer 

Set ws = wb.Sheets(1) 'wb being your workbook object; you could also use the sheet name instead of the index here 
ws.Activate 
lastrow = ws.Cells.SpecialCells(11).Row 'gets you the last row 

그래서, 당신은 당신의 삽입을위한 출발점으로 그 lastrow + 1 개 값을 사용할 수 있습니다 : 그것은 정보를 추가로 제공 할 때

또한, Excel은 멋진 일이있다. 여담으로

,

는 "도움말 및 팁 대단히 감사합니다! Plz은 올바른 방향으로 나를 가리 키도록 귀찮게하지 않습니다 ..."

^일반적으로 안 좋은 일을 to say이 부분들. 특히 당신이 "당신의 도움에 감사하지만 제발 나를 도와주지 마세요"라고했을 때.

재미있게 보내십시오.

관련 문제