0

我从数据库中提取数据并手动将其输入到 Excel 报告模板中。

我想使用 VBA 代码查找导出的封闭 Excel 文件 ( Test.xls) 和数据(例如单元格中的示例B1:B21)。

单元格中的数据在B1:B21每行之间都有空格。所以垂直一列将如下所示

Data1  
Space  
Space  
Data2  
.... 

除了将空格放入 Excel 报告文件中之外,我希望它水平显示(A10:"Data1", B10:"Data2", C10:"data3"...)而不是垂直显示。

出于安全原因,我无法将数据直接从数据库提取到 Excel 模板。

4

1 回答 1

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
于 2013-10-29T11:36:13.233 回答