Excel精英培训网

 找回密码
 注册
数据透视表40+个常用小技巧,让你一次学会!
查看: 2903|回复: 1

[已解决]保存、还原代码

[复制链接]
发表于 2016-6-15 22:26 | 显示全部楼层 |阅读模式
本帖最后由 乐乐2006201506 于 2016-6-16 13:34 编辑

1.怎样提取工作簿中宏代码并保存在文本文件中
2.然后通过提取出来的含宏代码的文本文件,实现还原到原来的工作簿中。
谢谢!
最佳答案
2016-6-16 10:48
代码如下,这个代码共参考!
  1. Sub 导出代码()
  2.     Dim h&, j&
  3.     Dim fso As Scripting.filesystemobject
  4.     Dim na$
  5.     Dim mt As Scripting.textstream
  6.     Set objShell = CreateObject("Shell.Application")
  7.     Set objFolder = objShell.BrowseForFolder(0, "请选择文件存放位置", 0, 0)
  8.     If Not objFolder Is Nothing Then
  9.         mypath = objFolder.self.Path
  10.     Else
  11.         mypath = ""
  12.         Exit Sub
  13.     End If
  14.     Set fso = New Scripting.filesystemobject
  15.     myfile = mypath & "" & "测试.txt"
  16.     Set mt = fso.CreateTextFile(Filename:=myfile, overwrite:=True)
  17.     On Error Resume Next
  18.     With ThisWorkbook.VBProject
  19.         For j = 1 To .VBComponents.Count
  20.             MsgBox .VBComponents(j).Type & .VBComponents(j).Name
  21.             If .VBComponents(j).Type = 1 And .VBComponents(j).Name <> "导入导出代码" Then
  22.                 na = .VBComponents(j).Name
  23.                 With .VBComponents(na).CodeModule
  24.                     h = .CountOfLines
  25.                     mt.Write "模块名称:" & na & vbCrLf
  26.                     For i = 1 To h
  27.                         mt.Write .Lines(i, 1) & vbCrLf
  28.                     Next
  29.                 End With
  30.                 .VBComponents.Remove .VBComponents(j)
  31.             End If
  32.         Next
  33.     End With
  34.     mt.Close
  35.     MsgBox "公式保存成功!"
  36. End Sub
  37. Sub 读取文本文件并放入指定工作表遍历()
  38.     Dim fso As Scripting.filesystemobject
  39.     Dim mt As Scripting.textstream, cN$
  40.     Dim myfile$, i&, str$, b As Boolean, rng$, fm$
  41.     myfile = Application.GetOpenFilename("TXT Files (*.txt), *.txt", 0, "选定文件", , False)
  42.     If myfile = "" Then Exit Sub
  43.     Set fso = New Scripting.filesystemobject
  44.     Set mt = fso.OpenTextFile(Filename:=myfile, IOMode:=ForReading)
  45.     With mt
  46.         Do Until .AtEndOfStream
  47.            str = .ReadLine
  48.             If Left(str, 5) = "模块名称:" Then
  49.                 cN = Replace(str, "模块名称:", "")
  50.                 MsgBox cN
  51.                 ThisWorkbook.VBProject.VBComponents.Add(1).Name = cN
  52.                 i = 0
  53.             Else
  54.                 i = i + 1
  55.                 With ThisWorkbook.VBProject.VBComponents(cN).CodeModule
  56.                     .InsertLines i, str
  57.                 End With
  58.             End If
  59.         Loop
  60.         .Close
  61.     End With
  62.     MsgBox "公式还原成功!"
  63. End Sub
复制代码

保护公式求助(保存 导入).rar

88.48 KB, 下载次数: 13

发表于 2016-6-16 10:48 | 显示全部楼层    本楼为最佳答案   
代码如下,这个代码共参考!
  1. Sub 导出代码()
  2.     Dim h&, j&
  3.     Dim fso As Scripting.filesystemobject
  4.     Dim na$
  5.     Dim mt As Scripting.textstream
  6.     Set objShell = CreateObject("Shell.Application")
  7.     Set objFolder = objShell.BrowseForFolder(0, "请选择文件存放位置", 0, 0)
  8.     If Not objFolder Is Nothing Then
  9.         mypath = objFolder.self.Path
  10.     Else
  11.         mypath = ""
  12.         Exit Sub
  13.     End If
  14.     Set fso = New Scripting.filesystemobject
  15.     myfile = mypath & "" & "测试.txt"
  16.     Set mt = fso.CreateTextFile(Filename:=myfile, overwrite:=True)
  17.     On Error Resume Next
  18.     With ThisWorkbook.VBProject
  19.         For j = 1 To .VBComponents.Count
  20.             MsgBox .VBComponents(j).Type & .VBComponents(j).Name
  21.             If .VBComponents(j).Type = 1 And .VBComponents(j).Name <> "导入导出代码" Then
  22.                 na = .VBComponents(j).Name
  23.                 With .VBComponents(na).CodeModule
  24.                     h = .CountOfLines
  25.                     mt.Write "模块名称:" & na & vbCrLf
  26.                     For i = 1 To h
  27.                         mt.Write .Lines(i, 1) & vbCrLf
  28.                     Next
  29.                 End With
  30.                 .VBComponents.Remove .VBComponents(j)
  31.             End If
  32.         Next
  33.     End With
  34.     mt.Close
  35.     MsgBox "公式保存成功!"
  36. End Sub
  37. Sub 读取文本文件并放入指定工作表遍历()
  38.     Dim fso As Scripting.filesystemobject
  39.     Dim mt As Scripting.textstream, cN$
  40.     Dim myfile$, i&, str$, b As Boolean, rng$, fm$
  41.     myfile = Application.GetOpenFilename("TXT Files (*.txt), *.txt", 0, "选定文件", , False)
  42.     If myfile = "" Then Exit Sub
  43.     Set fso = New Scripting.filesystemobject
  44.     Set mt = fso.OpenTextFile(Filename:=myfile, IOMode:=ForReading)
  45.     With mt
  46.         Do Until .AtEndOfStream
  47.            str = .ReadLine
  48.             If Left(str, 5) = "模块名称:" Then
  49.                 cN = Replace(str, "模块名称:", "")
  50.                 MsgBox cN
  51.                 ThisWorkbook.VBProject.VBComponents.Add(1).Name = cN
  52.                 i = 0
  53.             Else
  54.                 i = i + 1
  55.                 With ThisWorkbook.VBProject.VBComponents(cN).CodeModule
  56.                     .InsertLines i, str
  57.                 End With
  58.             End If
  59.         Loop
  60.         .Close
  61.     End With
  62.     MsgBox "公式还原成功!"
  63. End Sub
复制代码

保护公式求助(保存 导入).rar

90.64 KB, 下载次数: 12

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|Archiver|Excel精英培训 ( 豫ICP备11015029号 )

GMT+8, 2024-4-23 17:45 , Processed in 0.294023 second(s), 9 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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