我已經在過去的幾個小時裏看過不同的解決方案和代碼,但都沒有工作過(新手到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