2012-02-03 3 views
3

다른 통합 문서의 연결 위치를 업데이트하는 데 사용할 매크로가있는 통합 문서가 있습니다. VBA 스크립트는 폴더를 만들고 log.txt라는 데이터가 들어있는 로그 파일과 그래프 및 데이터의 세부 분류를 볼 수있는 데이터로 채워지도록 미리 형식이 지정된 Excel 파일 복사본을 채 웁니다. 그것은 문이 사용 된 횟수를 추적하는 문 열림 로그입니다.다른 통합 문서의 연결 속성을 수정하려면 Excel VBA 스크립트를 컴파일하십시오.

여기에 제가 지금까지 생각 해낸 VBA 코드입니다. 참고 : C++로 프로그래밍 한 지 2 년이되었지만 10 년 만에 손대지 않았습니다. 코드를 검색하고 심지어 연결을 수동으로 리프레시 할 때 취하는 매크로를 기록하려고했습니다. 그러나 내가 시도하고 해당 코드를 사용하면 "런타임 오류 1004"응용 프로그램 정의 또는 개체 정의 오류를 제공합니다.

다음은 코드입니다. 아래쪽에 주석 처리 된 비트는 수동으로 연결을 변경하여 기록한 매크로의 결과입니다.

도움을 많이 받으실 수 있습니다.

Sub Lof_File_Macro() 
' Log_file_Macro Macro 
' Runs script for monthly counts ' 
Dim strfolder1, strmonthno, strmonth, stryear, strfoldername, strfile, strmonyr, stlogfile, strfutfile  
'date strings defined using date functions - ofset for 28 days to allow running anytime within 20 days into the next month whilereturning correct month 
strmonthno = Month(Date - 28) 
strmonth = MonthName((strmonthno), True) 
stryear = Year(Date - 28)  

strmonyr = " " & strmonth & " " & stryear 
strfolder = "C:\Users\jtaylor7\Desktop\futures\People Counter" & strmonyr 
strfile = "Futures People" & strmonyr & ".xls"  

strlogfile = strfolder & "\" & "log" & strmonyr & ".txt" 
strfutfile = strfolder & "\" & strfile 

MkDir (strfolder)  

FileCopy "C:\Users\jtaylor7\Desktop\futures\log.log", strlogfile 
FileCopy "C:\Users\jtaylor7\Desktop\futures\template.xls", strfutfile  

'Workbooks.Open Filename:=strfutfile 
'ActiveWorkbook.Connections.AddFromFile (strlogfile) 
' 
'  
' Perform data connection modification on file 

'' Windows(strfutfile).Activate 
' With ActiveWorkbook.Connections("log") 
'  .Name = "log" 
'  .Description = "" 
' End With 
' Range("$A$1:$H$1").Select 
'With Selection.QueryTable 
'  .Connection = "TEXT;strlogfile" 
'  .TextFilePlatform = 850 
'  .TextFileStartRow = 1 
'  .TextFileParseType = xlDelimited 
'  .TextFileTextQualifier = xlTextQualifierDoubleQuote 
'  .TextFileConsecutiveDelimiter = False 
'  .TextFileTabDelimiter = False 
'  .TextFileSemicolonDelimiter = False 
'  .TextFileCommaDelimiter = True 
'  .TextFileSpaceDelimiter = False 
'  .TextFileOtherDelimiter = "/" 
'  .TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1) 
'  .TextFileTrailingMinusNumbers = True 
'  .Refresh BackgroundQuery:=False 
' End With 
' Range("I4").Select 
' ActiveWorkbook.Connections("log").Refresh 
'' Windows("Run Me.xls").Activate 
    ' 
End Sub 

나는 조금 어리석은 사람이 누구인지 알고 싶습니다.

답변

4

트릭을해야합니다.

Pls는이

Sub LogFile_Macro() 
Dim strFolder As String 
Dim strMonthno As String 
Dim strMonth As String 
Dim strYear As String 
Dim strFoldername As String 
Dim strFile As String 
Dim strMonyr As String 
Dim strLogfile As String 
Dim strFutfile As String 
Dim wb As Workbook 

'date strings defined using date functions - ofset for 28 days to allow running anytime within 20 days into the next month whilereturning correct month 
strMonthno = Month(Date - 28) 
strMonth = MonthName((strMonthno), True) 
strYear = Year(Date - 28) 
strMonyr = " " & strMonth & " " & strYear 
strFolder = "C:\temp\People Counter" & strMonyr 
strFile = "Futures People" & strMonyr & ".xls" 

strLogfile = strFolder & "\" & "log" & strMonyr & ".txt" 
strFutfile = strFolder & "\" & strFile 

On Error Resume Next 
MkDir strFolder 
If Err.Number <> 0 Then 
    MsgBox "cannot create path", vbCritical 
    Exit Sub 
End If 
On Error GoTo 0 

FileCopy "C:\temp\futures\log.log", strLogfile 
FileCopy "C:\temp\futures\template.xls", strFutfile 

Set wb = Workbooks.Open(strFutfile) 
With wb.Sheets(1).QueryTables.Add(Connection:="TEXT;" & strLogfile, Destination:=Range("A1:H1")) 
    .Name = "log" 
    .TextFilePlatform = 850 
    .TextFileStartRow = 1 
    .TextFileParseType = xlDelimited 
    .TextFileTextQualifier = xlTextQualifierDoubleQuote 
    .TextFileCommaDelimiter = True 
    .TextFileOtherDelimiter = "/" 
    .TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1) 
    .TextFileTrailingMinusNumbers = True 
    .Refresh BackgroundQuery:=False 
    .Refresh 
End With 
Windows("Run Me.xls").Activate 
End Sub 
+0

완벽한 아래로 내 테스트에서 경로를 업데이트합니다. 매우 감사합니다. 왜 내가 뭘 잘못했는지 설명 할 수 있을까요? 나는 코드의 차이점을 이해하지만 왜 내가 제대로 작동하지 않았는지 모르겠다. 대단히 고마워. –

+0

조. 너는 아주 가까웠다. 이 라인은 계단식 프로세스에서 새로운 연결'ActiveWorkbook.Connections.AddFromFile (strlogfile)'을 생성하고있었습니다. 하지만 더 큰 문제는 'ActiveWorkbook.Connections ("log")를 사용하여 "log"로 이름을 지정하기 전에 "log"라는 연결로 작업하려고 시도한 곳입니다. – brettdj

+0

아, 그래, 문제가있는 것을 볼 수 있습니다. 파일에 로그라는 이름으로 복사 된 연결이있어서 내가 사용한 코드가 수정되었습니다. 당신의 도움에 대해 대단히 고마워요. 나는 어둠 속에서 조금 실수하고있었습니다. 22 시간 만에 현상금을 수여할만한 가치가있었습니다 :-) –

관련 문제