ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

搜索
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 Excel Home精品图文教程库
HR薪酬管理数字化实战 Excel 2021函数公式学习大典 Excel数据透视表实战秘技 打造核心竞争力的职场宝典
300集Office 2010微视频教程 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南
查看: 3249|回复: 15

[已解决] 把EXCEL数据导入到txt文档中的指定位置。谢谢liulang0808和一把小刀闯天下老师

[复制链接]

TA的精华主题

TA的得分主题

发表于 2019-1-17 23:26 | 显示全部楼层 |阅读模式
本帖最后由 lhj323323 于 2019-1-18 20:43 编辑

老师:
我想用VBA实现如下需求
把EXCEL的sheet1的A、B两列的内容,导入到同路径的mk.txt文档中的指定位置
       同路径里,只有一个txt文档,且它的文件名永不变,即mk
       同路径里,也只有这一个xls文档
mk.txt 里的原有内容,请保留,同时,原有内容中,[TRD]之下,是永远没有数据的。
具体的需求,请看附件。先谢谢老师了。


求助3.rar (6.83 KB, 下载次数: 14)

TA的精华主题

TA的得分主题

发表于 2019-1-18 08:20 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
  1. Private Sub CommandButton1_Click()
  2.     Dim crr()
  3.     Application.ScreenUpdating = False
  4.    
  5.     arr = ActiveSheet.[a1].CurrentRegion
  6.     Open ThisWorkbook.Path & "\mk.txt" For Input As #1
  7.     s = StrConv(InputB(LOF(1), 1), vbUnicode)
  8.     brr = Split(s, vbCrLf)
  9.     Close #1
  10.     ReDim crr(1 To UBound(brr) - 1 + UBound(arr) * 2)
  11.     For j = 0 To UBound(brr)
  12.         If brr(j) <> "[TIP]" Then
  13.             crr(j + 1) = brr(j)
  14.         Else
  15.             lb = j
  16.             lc = lb + 1
  17.             Exit For
  18.         End If
  19.     Next j
  20.     For j = 2 To UBound(arr)
  21.         If Len(arr(j, 1)) > 0 Then
  22.             crr(lc) = Format(arr(j, 1), "0000000") & "=7"
  23.             lc = lc + 1
  24.         End If
  25.     Next j
  26.     For j = lb To UBound(brr)
  27.         If Len(brr(j)) > 0 Then
  28.             crr(lc) = brr(j)
  29.             lc = lc + 1
  30.         End If
  31.     Next j
  32.     For j = 2 To UBound(arr)
  33.         If Len(arr(j, 1)) > 0 Then
  34.             crr(lc) = Format(arr(j, 1), "0000000") & "=" & arr(j, 2)
  35.             lc = lc + 1
  36.         End If
  37.     Next j
  38.     str1 = Join(crr, vbCrLf)
  39.     Open ThisWorkbook.Path & "\mk.txt" For Output As #1
  40.     Print #1, str1
  41.     Close #1
  42.     Application.ScreenUpdating = True
  43. End Sub
复制代码

评分

1

查看全部评分

TA的精华主题

TA的得分主题

发表于 2019-1-18 08:21 | 显示全部楼层
附件内容供参考。。。。。。

求助3.zip

15.67 KB, 下载次数: 15

评分

1

查看全部评分

TA的精华主题

TA的得分主题

发表于 2019-1-18 08:31 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
Option Explicit

Sub test()
    Dim filename, i, j, arr, brr(), crr, cnt
    filename = ThisWorkbook.Path & "\mk.txt"
    If Dir(filename) = vbNullString Then MsgBox filename: Exit Sub
    Open filename For Input As #1
    arr = Split(StrConv(InputB(LOF(1), 1), vbUnicode), vbNewLine)
    Close #1
    ReDim brr(10 * UBound(arr))
    crr = [a1].CurrentRegion
    For i = 0 To UBound(arr)
      If Len(arr(i)) = 0 Then Exit For
      If arr(i) = "[TIP]" Then
        For j = 2 To UBound(crr, 1)
          cnt = cnt + 1
          brr(cnt) = String(7 - Len(crr(j, 1)), "0") & crr(j, 1) & "=7"
        Next
      End If
      cnt = cnt + 1: brr(cnt) = arr(i)
    Next
    For i = 2 To UBound(crr, 1)
      cnt = cnt + 1
      brr(cnt) = String(7 - Len(crr(i, 1)), "0") & crr(i, 1) & "=" & crr(i, 2)
    Next
    ReDim Preserve brr(cnt)
    arr = Mid(Join(brr, vbNewLine), 3)
    Open filename For Output As #1
    Print #1, arr
    Close #1
End Sub

评分

2

查看全部评分

TA的精华主题

TA的得分主题

发表于 2019-1-18 09:24 | 显示全部楼层
API读写INI文件的方法:
    Private Declare Function GetPrivateProfileInt Lib "kernel32" Alias "GetPrivateProfileIntA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal nDefault As Long, ByVal lpFileName As String) As Long
    Private Declare Function GetPrivateProfileSection Lib "kernel32" Alias "GetPrivateProfileSectionA" (ByVal lpAppName As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
    Private Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
    Private Declare Function WritePrivateProfileSection Lib "kernel32" Alias "WritePrivateProfileSectionA" (ByVal lpAppName As String, ByVal lpString As String, ByVal lpFileName As String) As Long
    Private Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Long
    Dim Ret As Long
    Dim Start As Long
    Public FileName As String
    Const BufSize = 10240
    Dim buf As String * BufSize
    Public Sub SetValue(ByVal clsName As String, ByVal key As String, ByVal V As String)
     Ret = WritePrivateProfileString(clsName, key, V, FileName)
    End Sub
    Public Function GetValue(ByVal clsName As String, ByVal key As String) As String
     Ret = GetPrivateProfileString(clsName, key, "", buf, BufSize, FileName)
     Start = 1
     GetValue = RetStr()
    End Function
Sub wrini()
    Dim arr
    FileName = ThisWorkbook.Path & "\mk.txt"
    arr = Range("a1").CurrentRegion
    For i = 2 To UBound(arr)
        s = "0000000" + CStr(arr(i, 1))
        s = Right(s, 7)
        SetValue "MAK", s, "7"
        SetValue "TRD", s, CStr(arr(i, 2))
    Next
End Sub

TA的精华主题

TA的得分主题

 楼主| 发表于 2019-1-18 20:30 | 显示全部楼层
xd3210 发表于 2019-1-18 09:24
API读写INI文件的方法:
    Private Declare Function GetPrivateProfileInt Lib "kernel32" Alias "GetP ...

xd3210老师:
是如下图这般写入代码吗?
但运行时,要报错
快照2.png
快照1.png

TA的精华主题

TA的得分主题

 楼主| 发表于 2019-1-18 20:39 | 显示全部楼层

TA的精华主题

TA的得分主题

 楼主| 发表于 2019-1-18 20:40 | 显示全部楼层

谢谢“一把小刀闯天下”老师,测试完全正确。

TA的精华主题

TA的得分主题

 楼主| 发表于 2019-1-18 20:52 | 显示全部楼层

TA的精华主题

TA的得分主题

 楼主| 发表于 2019-1-18 21:27 | 显示全部楼层
“liulang0808”和“一把小刀闯天下”老师
非常感谢两位老师的热心帮助,一楼的求助需求均是正确的!

但由于我在提出需求时不细致,在验证老师的代码时,才发现“原有内容”竟然还有两种新情况,请看下图
快照1.png

给两位老师带来不必要的麻烦,很不好意思。
如有时间,请帮忙看看本楼的如下附件:
求助4.rar (15.25 KB, 下载次数: 5)
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

关闭

最新热点上一条 /1 下一条

手机版|关于我们|联系我们|ExcelHome

GMT+8, 2024-4-24 23:24 , Processed in 0.038163 second(s), 9 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

沪公网安备 31011702000001号 沪ICP备11019229号-2

本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任!     本站特聘法律顾问:李志群律师

快速回复 返回顶部 返回列表