我从数据库中提取数据并手动将其输入Excel报告模板。
我希望VBA代码能够使用数据查找导出的已关闭Excel文件(Test.xls
)(单元格B1:B21
中的示例)。
单元格B1:B21
中的数据在每一行之间有空格。所以垂直一列就像下面的
Data1
Space
Space
Data2
....
除了要放入Excel报表文件的空格外,我想要这个,并且水平显示(A10:"Data1"
,B10:"Data2"
,C10:"data3"
...)而不是垂直显示。< / p>
出于安全原因,我无法将数据直接从数据库提取到Excel模板。
答案 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