Excel精英培训网

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

[已解决]合并代码(2)遍历工作表

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

    遍历一个工作簿中所有有公式工作表中的公式,然后存入文本文件,并且可以实现还原。该怎么修改或者重写代码?当然要在实现下面代码效果的基础上遍历哦!
  • Sub 读取公式并存入文本文件()
  •     Dim FormulaCells As Range, Cell As Range
  •     Dim FormulaSheet As Worksheet
  •     Dim Row As Integer, myfile$, objFolder
  •     Dim fso As Scripting.FileSystemObject
  •     Dim mt As Scripting.TextStream
  •     '创建Range对象
  •     On Error Resume Next
  •     Set objShell = CreateObject("Shell.Application")
  •     Set FormulaCells = Range("A1").SpecialCells(xlFormulas, 23)
  •     '没有找到公式
  •     If FormulaCells Is Nothing Then
  •         MsgBox "当前工作表中没有公式!"
  •         Exit Sub
  •     End If
  •     Set objFolder = objShell.BrowseForFolder(0, "请选择文件存放位置", 0, 0)
  •     If Not objFolder Is Nothing Then
  •         mypath = objFolder.self.Path
  •     Else
  •         mypath = ""
  •         Exit Sub
  •     End If
  •     myfile = mypath & "\" & Replace(ThisWorkbook.Name, ".xlsm", "") & Replace(Replace(ThisWorkbook.Path, "\", " "), ":", " ") & ".txt"
  •     Set fso = New Scripting.FileSystemObject
  •     Set mt = fso.CreateTextFile(Filename:=myfile, overwrite:=True)
  •     '读取公式,同时在状态栏中显示进度。
  •     For Each Cell In FormulaCells
  •         mt.Write Cell.Address(RowAbsolute:=False, ColumnAbsolute:=False) & vbCrLf
  •         mt.Write Cell.Formula & vbCrLf
  •     Next
  •     mt.Close
  •     MsgBox "公式保存成功!"
  • End Sub


  • Sub 读取文本文件并放入指定工作表()
  •     Dim fso As Scripting.filesystemobject
  •     Dim mt As Scripting.textstream
  •     Dim myfile$, i&, sht$, b As Boolean, rng$, fm$
  •     myfile = Application.GetOpenFilename("TXT Files (*.txt), *.txt", 0, "选定文件", , False)
  •     If myfile = "" Then Exit Sub
  •     Set fso = New Scripting.filesystemobject
  •     Set mt = fso.OpenTextFile(Filename:=myfile, IOMode:=ForReading)
  •     sht = "测试"  '公式插入那个工作表
  •     With mt
  •         Do Until .AtEndOfStream
  •             i = i + 1
  •             If i Mod 2 = 1 Then
  •                 b = False
  •                 rng = .ReadLine
  •             Else
  •                 b = True
  •                 fm = .ReadLine
  •             End If
  •             If b = True Then Sheets(sht).Range(rng).Formula = fm
  •         Loop
  •         .Close
  •     End With
  •     MsgBox "公式还原成功!"
  • End Sub

最佳答案
2016-6-15 09:07
代码如下,但是这里要注意还原是按照工作表的名称进行还原的,即导出之前是按照每个工作表的名称进行存储的,读取还原的时候也是按照工作表名称进行:
  1. Sub 读取公式并存入文本文件()
  2.     Dim FormulaCells As Range, Cell As Range
  3.     Dim sht As Worksheet
  4.     Dim Row As Integer, myfile$, objFolder As Object
  5.     Dim fso As Scripting.filesystemobject
  6.     Dim mt As Scripting.textstream
  7.     '创建Range对象
  8.     On Error Resume Next
  9.     Set objShell = CreateObject("Shell.Application")
  10.     Set objFolder = objShell.BrowseForFolder(0, "请选择文件存放位置", 0, 0)
  11.     If Not objFolder Is Nothing Then
  12.         mypath = objFolder.self.Path
  13.     Else
  14.         mypath = ""
  15.         Exit Sub
  16.     End If
  17.     myfile = mypath & "" & Replace(ThisWorkbook.Name, ".xlsm", "") & Replace(Replace(ThisWorkbook.Path, "", " "), ":", " ") & ".txt"
  18.     Set fso = New Scripting.filesystemobject
  19.     Set mt = fso.CreateTextFile(Filename:=myfile, overwrite:=True)
  20.     '读取公式,同时在状态栏中显示进度。
  21.     Set objShell = CreateObject("Shell.Application")
  22.     For Each sht In ThisWorkbook.Sheets
  23.         Set FormulaCells = sht.Range("A1").SpecialCells(xlFormulas, 23)
  24.         If FormulaCells <> Null Then
  25.         For Each Cell In FormulaCells
  26.             mt.Write sht.Name & vbCrLf
  27.             mt.Write Cell.Address(RowAbsolute:=False, ColumnAbsolute:=False) & vbCrLf
  28.             mt.Write Cell.Formula & vbCrLf
  29.         Next
  30.         End If
  31.     Next
  32.     mt.Close
  33.     MsgBox "公式保存成功!"
  34. End Sub
  35. Sub 读取文本文件并放入指定工作表()
  36.     Dim fso As Scripting.filesystemobject
  37.     Dim mt As Scripting.textstream
  38.     Dim myfile$, i&, sht$, b As Boolean, rng$, fm$
  39.     myfile = Application.GetOpenFilename("TXT Files (*.txt), *.txt", 0, "选定文件", , False)
  40.     If myfile = "" Then Exit Sub
  41.     Set fso = New Scripting.filesystemobject
  42.     Set mt = fso.OpenTextFile(Filename:=myfile, IOMode:=ForReading)
  43.     sht = "测试"  '公式插入那个工作表
  44.     With mt
  45.         Do Until .AtEndOfStream
  46.             i = i + 1
  47.             If i Mod 3 = 1 Then
  48.                 b = False
  49.                 sht = .ReadLine
  50.             End If
  51.             If i Mod 3 = 2 Then
  52.                 b = False
  53.                 rng = .ReadLine
  54.             End If
  55.             If i Mod 3 = 0 Then
  56.                 b = True
  57.                 fm = .ReadLine
  58.             End If
  59.             If b = True Then Sheets(sht).Range(rng).Formula = fm
  60.         Loop
  61.         .Close
  62.     End With
  63.     MsgBox "公式还原成功!"
  64. End Sub
复制代码
发表于 2016-6-15 09:07 | 显示全部楼层    本楼为最佳答案   
代码如下,但是这里要注意还原是按照工作表的名称进行还原的,即导出之前是按照每个工作表的名称进行存储的,读取还原的时候也是按照工作表名称进行:
  1. Sub 读取公式并存入文本文件()
  2.     Dim FormulaCells As Range, Cell As Range
  3.     Dim sht As Worksheet
  4.     Dim Row As Integer, myfile$, objFolder As Object
  5.     Dim fso As Scripting.filesystemobject
  6.     Dim mt As Scripting.textstream
  7.     '创建Range对象
  8.     On Error Resume Next
  9.     Set objShell = CreateObject("Shell.Application")
  10.     Set objFolder = objShell.BrowseForFolder(0, "请选择文件存放位置", 0, 0)
  11.     If Not objFolder Is Nothing Then
  12.         mypath = objFolder.self.Path
  13.     Else
  14.         mypath = ""
  15.         Exit Sub
  16.     End If
  17.     myfile = mypath & "" & Replace(ThisWorkbook.Name, ".xlsm", "") & Replace(Replace(ThisWorkbook.Path, "", " "), ":", " ") & ".txt"
  18.     Set fso = New Scripting.filesystemobject
  19.     Set mt = fso.CreateTextFile(Filename:=myfile, overwrite:=True)
  20.     '读取公式,同时在状态栏中显示进度。
  21.     Set objShell = CreateObject("Shell.Application")
  22.     For Each sht In ThisWorkbook.Sheets
  23.         Set FormulaCells = sht.Range("A1").SpecialCells(xlFormulas, 23)
  24.         If FormulaCells <> Null Then
  25.         For Each Cell In FormulaCells
  26.             mt.Write sht.Name & vbCrLf
  27.             mt.Write Cell.Address(RowAbsolute:=False, ColumnAbsolute:=False) & vbCrLf
  28.             mt.Write Cell.Formula & vbCrLf
  29.         Next
  30.         End If
  31.     Next
  32.     mt.Close
  33.     MsgBox "公式保存成功!"
  34. End Sub
  35. Sub 读取文本文件并放入指定工作表()
  36.     Dim fso As Scripting.filesystemobject
  37.     Dim mt As Scripting.textstream
  38.     Dim myfile$, i&, sht$, b As Boolean, rng$, fm$
  39.     myfile = Application.GetOpenFilename("TXT Files (*.txt), *.txt", 0, "选定文件", , False)
  40.     If myfile = "" Then Exit Sub
  41.     Set fso = New Scripting.filesystemobject
  42.     Set mt = fso.OpenTextFile(Filename:=myfile, IOMode:=ForReading)
  43.     sht = "测试"  '公式插入那个工作表
  44.     With mt
  45.         Do Until .AtEndOfStream
  46.             i = i + 1
  47.             If i Mod 3 = 1 Then
  48.                 b = False
  49.                 sht = .ReadLine
  50.             End If
  51.             If i Mod 3 = 2 Then
  52.                 b = False
  53.                 rng = .ReadLine
  54.             End If
  55.             If i Mod 3 = 0 Then
  56.                 b = True
  57.                 fm = .ReadLine
  58.             End If
  59.             If b = True Then Sheets(sht).Range(rng).Formula = fm
  60.         Loop
  61.         .Close
  62.     End With
  63.     MsgBox "公式还原成功!"
  64. End Sub
复制代码
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-27 09:58 , Processed in 0.274443 second(s), 8 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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