我已经在过去的几个小时里看过不同的解决方案和代码,但都没有工作过(新手到VBA)。 我从另一个使用俄语字符的网站接收文件,我需要将这些文件导入到最后一个使用行下面的现有电子表格以及数据使用Windows西里尔字符。VBA Excel导入
现有的电子表格确实有列,你知道我将如何格式化数据,以便在现有列标题下导入数据。
该数据是标签式的,但目前没有任何标题。
我设法找到一些适用于导入的代码,但是这将它放在单元格A1中,使其具有宏而不是另一个工作表并且没有列。任何帮助,将不胜感激。
Sub DoThis()
Dim TxtArr() As String, I As Long
'TxtArr = BrowseForFile("C:\Users\rjoss\Desktop\SVY")
TxtArr = Split(OpenMultipleFiles, vbCrLf)
For I = LBound(TxtArr, 1) To UBound(TxtArr, 1)
Import_Extracts TxtArr(I)
Next
End Sub
Sub Import_Extracts(filename As String)
'
Dim Tmp As String
Tmp = Replace(filename, ".txt", "")
Tmp = Mid(Tmp, InStrRev(Tmp, "\") + 1)
'
Range("A50000").End(xlUp).Offset(1, 0).Select
With ActiveSheet.QueryTables.Add(Connection:= _
"TEXT;" & filename _
, Destination:=Range("A50000").End(xlUp).Offset(1, 0))
.Name = Tmp
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 850
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = True
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = False
.TextFileOtherDelimiter = "~"
.TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
ActiveCell.EntireRow.Delete
End Sub
'code copied from here and modified to work
'http://www.tek-tips.com/faqs.cfm?fid=4114
Function OpenMultipleFiles() As String
Dim Filter As String, Title As String, msg As String
Dim I As Integer, FilterIndex As Integer
Dim filename As Variant
' File filters
Filter = "Text Files (*.txt),*.txt"
' Set Dialog Caption
Title = "Select File(s) to Open"
' Select Start Drive & Path
ChDrive ("C")
'ChDir ("c:\Files\Imports")
ChDir ("C:\Users\rjoss\Desktop\SVY")
With Application
' Set File Name Array to selected Files (allow multiple)
filename = .GetOpenFilename(Filter, FilterIndex, Title, , True)
' Reset Start Drive/Path
ChDrive (Left(.DefaultFilePath, 1))
ChDir (.DefaultFilePath)
End With
' Exit on Cancel
If Not IsArray(filename) Then
MsgBox "No file was selected."
Exit Function
End If
msg = Join(filename, vbCrLf)
OpenMultipleFiles = msg
End Function
是在相同的顺序现有列在文件中的数据,或者你将不得不重新安排文件中的数据? – barrowc 2013-02-14 13:23:27
您需要使用特定选项宏观记录手动文本导入。 – 2013-02-14 13:30:56
@barrowc它与文件中的数据的顺序相同,但是文件中的一些数据是无用的,我们将这些文件作为分割文件,这样我就得到了一个批量文件,它合并了数据,但没有去除无用的信息,我们想要的只是它的某些部分客户姓名,客户编号等。Peter L,谢谢你是否知道任何能够帮助我的资源,例如(我明白编码更好,当我看到它工作时)。谢谢你们! – Ryan 2013-02-14 13:52:14