2013-10-15 3 views
0

Excel을 사용하여 보고서 템플릿을 만들려고합니다. 현재 우리는 데이터베이스의 데이터를 가져와 수동으로 템플릿에 입력합니다.폐쇄 Excel 통합 문서 시트 1의 값을 기반으로 한 셀

내가하고 싶었던 것은 데이터로 채워진 내 보낸 Closed Excel 파일 (Test.Xls)을 조회하는 vba 코드를 작성하는 것이 었습니다 (B1 : B21 셀의 예제).

셀 B1 : B21의 데이터에는 각 줄 사이에 공백이 있습니다. 그래서 세로로 열은 다음과 같을 것입니다. (Data1, Space, Space, Data2 ...)

보고서를 Excel 파일에 놓고 가로로 옮기는 공간을 제외하고 이것을 원합니다 (A10 " Data1 ", B10"Data2 ", C10"data3 "...)이 있습니다.

나는 VBA에 대한 기본적인 이해를하고있다. (나는 보통 구글과 피스 코드를 함께 사용하여 원하는대로 처리한다. 그러나 이번에는 좀 더 복잡해 보인다.) 이 목표는 데이터 입력/복사에 소요되는 시간을 너무 줄이는 것입니다.

데이터베이스에서 Excel 템플릿으로 직접 데이터를 가져 오는 옵션은 보안상의 이유로 수행 할 수 없습니다.

편집 :

다음

해결 솔루션 :

Private Function GetValue(path, file, sheet, ref, v) 

path = "C:\Documents and Settings\sdavis\Desktop\Index\XXX\Results" 
file = "test.xls" 
sheet = "Sheet1" 
ref = "A1:R30" 



' Retrieves a value from a closed workbook 
Dim arg As String 
Dim p As Integer 
' Make sure the file exists 
If Right(path, 1) <> "\" Then path = path & "\" 
If Dir(path & file) = "" Then 
    GetValue = "File Not Found" 
    Exit Function 
End If 








' Create the argument 
arg = "'" & path & "[" & file & "]" & sheet & "'!" & _ 
Range(ref).Cells(v, 2).Address(, , xlR1C1) 



' Execute an XLM macro 
GetValue = ExecuteExcel4Macro(arg) 


End Function 

Sub TestGetValue() 

'Declare 
Dim v As Integer 

'Starting Point 
v = 21 

'File Location 
path = "C:\Documents and Settings\sdavis\Desktop\Index\XXX\Results" 
file = "test" 
sheet = "Sheet1" 

Application.ScreenUpdating = False 


    For C = 1 To 15 

     a = Cells(5, C).Address 
     Cells(5, C) = GetValue(path, file, sheet, a, v) 
     v = v + 3 
    Next C 


Application.ScreenUpdating = True 
End Sub 

다시 감사합니다, 알렉스 난 당신이 있지만, 경우에, 실제로 그것에서 읽을 수있는 XLS를 열어야합니다 생각

+0

vba에서 완료되면 Excel 인스턴스가 숨겨 질 수 있습니다. 즉, 다른 Excel 파일이 열리고 있다는 것을 사용자에게 표시하지 않습니다. – Jaycal

+0

[**'여기'**]에서보세요 (http://stackoverflow.com/questions/18481330/2-dimensional-array-vba-from-cell-contents-in-excel/18481730#18481730) 'Transpose' 함수에 대해 조금 배우십시오 :) –

+0

파일 열기, 새로운 시트에 열 복사/붙여 넣기, 공백 제거, 특수 전치 복사/붙여 넣기. –

답변

0
Private Function GetValue(path, file, sheet, ref, v) 

path = "C:\Documents and Settings\sdavis\Desktop\Index\XXX\Results" 
file = "test.xls" 
sheet = "Sheet1" 
ref = "A1:R30" 



' Retrieves a value from a closed workbook 
Dim arg As String 
Dim p As Integer 
' Make sure the file exists 
If Right(path, 1) <> "\" Then path = path & "\" 
If Dir(path & file) = "" Then 
    GetValue = "File Not Found" 
    Exit Function 
End If 








' Create the argument 
arg = "'" & path & "[" & file & "]" & sheet & "'!" & _ 
Range(ref).Cells(v, 2).Address(, , xlR1C1) 



    ' Execute an XLM macro 
GetValue = ExecuteExcel4Macro(arg) 


End Function 

Sub TestGetValue() 

'Declare 
Dim v As Integer 

'Starting Point 
v = 21 

'File Location 
path = "C:\Documents and Settings\sdavis\Desktop\Index\XXX\Results" 
file = "test" 
sheet = "Sheet1" 

    Application.ScreenUpdating = False 


    For C = 1 To 15 

     a = Cells(5, C).Address 
     Cells(5, C) = GetValue(path, file, sheet, a, v) 
     v = v + 3 
    Next C 


Application.ScreenUpdating = True 
End Sub 
관련 문제