我正在开发一个excel电子表格,它从CSV文件中获取数据(由外部系统自动生成)。 我用过:
数据 - >获取外部数据 - >从文本
它完美无缺!
但是我无法将导入的数据格式化为表格: - (
它提供以下信息:
您的选择与一个或多个外部数据范围重叠。是否要将选择转换为表并删除所有外部连接?
有没有办法在不破坏连接的情况下将导入的数据格式化为表格?
由于 马丁
答案 0 :(得分:1)
这应该对您有用 - 确保您有一个名为Data的选项卡,并将公共const更改为该文件的路径。我假设您知道如何处理此代码,如果没有让我知道。
Public Const feedDir = "C:\Program Files\Common Files\System\data.csv" 'change this to the path of the file
Sub loadDataWrapper()
'''check file is in directory before proceding
If Dir(feedDir) <> "" Then
fileToLoad = feedDir
Else
MsgBox "No file available to load. Please check the path and try again."
Exit Sub
End If
Call loadData(fileToLoad)
End Sub
Sub loadData(ByVal fileToLoad As String)
Dim fso As Object, textFile As Object: Set fso = CreateObject("Scripting.FileSystemObject")
Dim textFileStr As String
Dim textFileArr As Variant
Dim outputArr() As Variant
Dim oneRow As Variant
Dim numRows, numColumns As Long
'''open the text file and read into memory as is
Set textFile = fso.OpenTextFile(fileToLoad, 1)
textFileStr = textFile.ReadAll
textFile.Close
Set textFile = Nothing
Set fso = Nothing
'''find number of rows and columns of text file
textFileArr = Split(textFileStr, Chr(10))
numRows = UBound(textFileArr)
numColumns = UBound(Split(textFileArr(0), ","))
ReDim outputArr(numRows, numColumns)
'''go through every line and insert into array
For ii = 0 To (numRows - 1)
oneRow = Split(textFileArr(ii), ",")
For jj = 0 To numColumns
outputArr(ii, jj) = oneRow(jj)
Next jj
Next ii
'''output array to Worksheet
Worksheets("Data").Range("A2:Z1048576").ClearContents
Worksheets("Data").Range("A2").Resize(numRows + 1, numColumns + 1).Value = outputArr
End Sub
答案 1 :(得分:0)
数据透视表是否满足您的要求?
Insert>PivotTable>Use External Data Source Radio Button