求助大家个问题,如何用VB实现,将分隔符为|的TXT的某列k1,按照导入的excel表的数据进行替换?

我有一张excel表,有id,k1,k2三列,还有一份TXT文件,分隔符为|,表中有数据列id和k1,现在我想用VB实现这个功能:把excel表和txt表导入,然后进行处理,根据excel表中id,k1,k2的数据,将txt表中的k1那一列,对应修改成k2,比如

excel表:id k1 k2
a 1 3
b 2 4
txt表: id k1
c 1
d 2
e 1
f 1
将这两个表导入程序后,点击处理,然后导出txt文件,其他数据不变,k1列变成:3,4,3,3。要求写出详细的程序编写过程,包括表的导入,处理和导出的全过程。谢谢。
id列不一样,只根据k1和k2去替换。

简单倒是简单,但你没说清楚:
id列的数据是不是一样?要不要查找匹配?
=>是不是只要K1相同就把k1换成K2? 还有记事本的k1和excel的k1是不是一一对应的?
如果以上答案是肯定的,添加三个按钮,另外在工程-引用里添加 mircrosoft excel,代码如下:
Private Declare Function GetOpenFileName Lib "comdlg32.dll" Alias "GetOpenFileNameA" (pOpenfilename As OPENFILENAME) As Long
Private Type OPENFILENAME
lStructSize As Long
hwndOwner As Long
hInstance As Long
lpstrFilter As String
lpstrCustomFilter As String
nMaxCustFilter As Long
nFilterIndex As Long
lpstrFile As String
nMaxFile As Long
lpstrFileTitle As String
nMaxFileTitle As Long
lpstrInitialDir As String
lpstrTitle As String
flags As Long
nFileOffset As Integer
nFileExtension As Integer
lpstrDefExt As String
lCustData As Long
lpfnHook As Long
lpTemplateName As String
End Type
Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet
Dim xlpath As String, txtpath As String
Dim txt() As String, i As Integer
'按钮1,导入excel
Private Sub Command1_Click()
Dim OpenFile As OPENFILENAME
Dim lReturn As Long
Dim sFilter As String
OpenFile.lStructSize = Len(OpenFile)
OpenFile.hwndOwner = Form1.hWnd
OpenFile.hInstance = App.hInstance
sFilter = "Files type (*.xls)" & Chr(0) & "*.xls" & Chr(0)
OpenFile.lpstrFilter = sFilter
OpenFile.nFilterIndex = 1
OpenFile.lpstrFile = String(257, 0)
OpenFile.nMaxFile = Len(OpenFile.lpstrFile)
OpenFile.lpstrFileTitle = OpenFile.lpstrFile
OpenFile.lpstrDefExt = "xls"
OpenFile.nMaxFileTitle = OpenFile.nMaxFile
OpenFile.lpstrInitialDir = App.Path
OpenFile.lpstrTitle = "Select Excel File"
OpenFile.flags = 0
lReturn = GetOpenFileName(OpenFile)
xlpath = OpenFile.lpstrFile
End Sub
'按钮2,导入TXT
Private Sub Command2_Click()
Dim OpenFile As OPENFILENAME
Dim lReturn As Long
Dim sFilter As String
OpenFile.lStructSize = Len(OpenFile)
OpenFile.hwndOwner = Form1.hWnd
OpenFile.hInstance = App.hInstance
sFilter = "Files type (*.txt)" & Chr(0) & "*.txt" & Chr(0)
OpenFile.lpstrFilter = sFilter
OpenFile.nFilterIndex = 1
OpenFile.lpstrFile = String(257, 0)
OpenFile.nMaxFile = Len(OpenFile.lpstrFile)
OpenFile.lpstrFileTitle = OpenFile.lpstrFile
OpenFile.lpstrDefExt = "txt"
OpenFile.nMaxFileTitle = OpenFile.nMaxFile
OpenFile.lpstrInitialDir = App.Path
OpenFile.lpstrTitle = "Select txt File"
OpenFile.flags = 0
lReturn = GetOpenFileName(OpenFile)
txtpath = OpenFile.lpstrFile
End Sub
'按钮3,导出TXT,假设txt和excel的中的k1是一一对应的,且excel存放数据的表为sheet1,如果不是,请改下
Private Sub Command3_Click()
If xlpath <> "" And txtpath <> "" Then
Set xlApp = CreateObject("Excel.Application")
Set xlBook = xlApp.Workbooks.Open(xlpath)
i = 0
Open txtpath For Input As #1
Do While Not EOF(1)
ReDim Preserve txt(i)
i = i + 1
Line Input #1, txt(i)
Loop
Close #1

For i = 1 To UBound(txt)
txt(i) = Left(txt(i), InStr(txt(i), "|")) & xlBook.Worksheets("sheet1").Range("C" & i + 1) '看看的excel表的名字是否为sheet1,如果不是,改一下
Next

Open txtpath For Output As #2
For i = 0 To UBound(txt)
Print #2, txt(i)
Next
Close #2

xlApp.DisplayAlerts = False
xlBook.Close
Set xlApp = Nothing
End If
End Sub
温馨提示:内容为网友见解,仅供参考
无其他回答

Excel表格中求数据分列的VBA代码,请将D列的数据分列在I列、J列、K列
Sub 按星号分列()'以*为分隔符,连续*只算1个。对所选中的单元格进行处理 Dim m As Range, tmpStr As String, s As String, i As Integer Dim x As Long, y As Long, subStr As String If Selection.Columns.Count > 1 Then MsgBox "所选择的数据只能是一列!请重新选择后执行!", vb...

如何用Excel VBA读取某一文件夹下批量TXT文本内的某行某列?
打开office excel;Alt+F11打开VBA编辑器;复制如下代码到模块中;F5运行该模块;按照提示进行如下步骤即可。Sub xxx()Dim myFile As StringDim myText As StringDim myString As StringDim i As SingleWith Application.FileDialog(msoFileDialogFolderPicker).Show.AllowMultiSelect = FalsemyFile = .Selec...

如何在VB中编程对EXCEL中数据进行分列
'以空格为分隔符,连续空格只算1个。对所选中的单元格进行处理 Dim m As Range, tmpStr As String, s As String Dim x As Integer, y As Integer, subStr As String If MsgBox("确定要分列处理吗?请确定分列的数据会覆盖它后面的单元格!", _vbYesNoCancel + vbQuestion) <> vbYes Then ...

用VB怎么实现excel中某个数据的替换,例如将"*"替换为"1"
:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False

用VBA的方法从EXCEL中筛选数据导出带分隔符的TXT文件?
For j = 2 To 6 If j = 2 Or j = 6 Then '这里放头尾行 s = s & .Cells(i, j).Text Else s = s & .Cells(i, j).Text & "|"End If Next 希望对你有帮助,请采纳,如下图

如何在VB中编程对EXCEL中数据进行分列
一、对于第1、3两个问题,我写的这个宏可以完美解决,代码如下:Sub 分列() '以空格为分隔符,连续空格只算1个。对所选中的单元格进行处理 Dim m As Range, tmpStr As String, s As String Dim x As Integer, y As Integer, subStr As String If MsgBox("确定要分列处理吗?请...

如何用vb提取excel表格中的数据如何用vb提取excel表格中的数据和...
用vb提取excel表格中的数据方法:1、在汇总表中的A列,将多个工作表的表名依次排列。2、在B1单元格输入以下公式,然后向右向下填充公式=OFFSET(INDIRECT($A1&"!A1"),,COLUMN(A1)-1)&""公式表示:以A1单元格的内容作为表名引用,通过OFFSET函数将表名引用的A1单元格作为基准单元格,每向右移动一列...

如何用VBA导入txt格式文本到Excel
建一个文件夹,把目标txt文件和excel文件放入其中,在excel的宏编辑器中写入以下代码,有些地方可根据你的实际情况做相应改动:Sub import_from_txt()Dim file_name As String, my_path As String Dim lines, cols Dim i As Integer, j As Integer, k As Integer, q As Integer Application....

如何用VB读取某个特定位置的EXCEL文件中的某个工作簿中的某行某列的内...
Private Sub CommandButton1_Click()Workbooks.Open Filename:="E:\\1.xlsx" '打开目标文件Me.TextBox1.Value = ActiveWorkbook.Sheets(2).Range("A3") '给文本框1赋值ActiveWorkbook.Close No '关闭目标文件End Sub根据你的要求,供参考!

求vb代码:批量txt导入,一个文本占一个单元格(某个文件夹里有上万个...
txt")Do Until d = "" i = i + 1 ost.cells(i, 1) = d Open "f:\\test\\" & d For Binary As #1 ost.cells(i, 2) = Input(LOF(1), #1) Close #1 d = DirLoopowb.saveas "f:\\test\\test.xls"owb.Closeoxl.quitMsgBox "ok"End Sub ...

相似回答
大家正在搜