ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

搜索
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 效率神器,一键搞定繁琐工作
Python自动化办公应用大全 Excel 2021函数公式学习大典 Kutools for Office 套件发布 打造核心竞争力的职场宝典
让更多数据处理,一键完成 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南
楼主: cofe2000

【请教】excel中用vba如何释放内存?

[复制链接]

TA的精华主题

TA的得分主题

发表于 2005-1-2 17:07 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助

偶用了一个VB中的函数

set myobject=nothing

好像起了一点作用,不知能不能真的起作用

TA的精华主题

TA的得分主题

发表于 2005-1-2 18:16 | 显示全部楼层

我这段代码怎么设成对象,然后用set myobject=nothing?

Private Sub OkButton_Click() Application.StatusBar = "正在设置页面,请稍候" Application.ScreenUpdating = False Application.EnableEvents = True ActiveSheet.Pagesetup.LeftMargin = Application.InchesToPoints(TextBox6.Text) ActiveSheet.Pagesetup.RightMargin = Application.InchesToPoints(TextBox7.Text) ActiveSheet.Pagesetup.TopMargin = Application.InchesToPoints(TextBox5.Text) ActiveSheet.Pagesetup.BottomMargin = Application.InchesToPoints(TextBox8.Text) ActiveSheet.Pagesetup.HeaderMargin = Application.InchesToPoints(TextBox9.Text) ActiveSheet.Pagesetup.FooterMargin = Application.InchesToPoints(TextBox10.Text) If OptionButton1.Value = True Then ActiveSheet.Pagesetup.Orientation = xlPortrait Else ActiveSheet.Pagesetup.Orientation = xlLandscape End If If OptionButton3.Value = True Then If ComboBox1.Text = "10%" Then ActiveSheet.Pagesetup.Zoom = 10 ElseIf ComboBox1.Text = "20%" Then ActiveSheet.Pagesetup.Zoom = 20 ElseIf ComboBox1.Text = "30%" Then ActiveSheet.Pagesetup.Zoom = 30 ElseIf ComboBox1.Text = "40%" Then ActiveSheet.Pagesetup.Zoom = 40 ElseIf ComboBox1.Text = "50%" Then ActiveSheet.Pagesetup.Zoom = 50 ElseIf ComboBox1.Text = "60%" Then ActiveSheet.Pagesetup.Zoom = 60 ElseIf ComboBox1.Text = "70%" Then ActiveSheet.Pagesetup.Zoom = 70 ElseIf ComboBox1.Text = "80%" Then ActiveSheet.Pagesetup.Zoom = 80 ElseIf ComboBox1.Text = "90%" Then ActiveSheet.Pagesetup.Zoom = 90 ElseIf ComboBox1.Text = "100%" Then ActiveSheet.Pagesetup.Zoom = 100 ElseIf ComboBox1.Text = "150%" Then ActiveSheet.Pagesetup.Zoom = 150 ElseIf ComboBox1.Text = "200%" Then ActiveSheet.Pagesetup.Zoom = 200 ElseIf ComboBox1.Text = "300%" Then ActiveSheet.Pagesetup.Zoom = 300 ElseIf ComboBox1.Text = "400%" Then ActiveSheet.Pagesetup.Zoom = 400 End If ElseIf ComboBox2.Text = "1页宽1页高" Then ActiveSheet.Pagesetup.Zoom = False ActiveSheet.Pagesetup.FitToPagesWide = 1 ActiveSheet.Pagesetup.FitToPagesTall = 1 ElseIf ComboBox2.Text = "1页宽10页高" Then ActiveSheet.Pagesetup.Zoom = False ActiveSheet.Pagesetup.FitToPagesWide = 1 ActiveSheet.Pagesetup.FitToPagesTall = 10 ElseIf ComboBox2.Text = "1页宽100页高" Then ActiveSheet.Pagesetup.Zoom = False ActiveSheet.Pagesetup.FitToPagesWide = 1 ActiveSheet.Pagesetup.FitToPagesTall = 100 ElseIf ComboBox2.Text = "2页宽100页高" Then ActiveSheet.Pagesetup.Zoom = False ActiveSheet.Pagesetup.FitToPagesWide = 2 ActiveSheet.Pagesetup.FitToPagesTall = 100 End If If ComboBox3.Text = "Page 1" Then ActiveSheet.Pagesetup.LeftHeader = "&""Times New Roman,常规""Page &P" ElseIf ComboBox3.Text = "Page 1 of ?" Then ActiveSheet.Pagesetup.LeftHeader = "&""Times New Roman,常规""Page &P of &N" ElseIf ComboBox3.Text = ActiveSheet.Name Then ActiveSheet.Pagesetup.LeftHeader = "&""Times New Roman,常规""&A" ElseIf ComboBox3.Text = ThisWorkbook.Name Then ActiveSheet.Pagesetup.LeftHeader = "&""Times New Roman,常规""&F" Else ActiveSheet.Pagesetup.LeftHeader = ComboBox3.Text End If If ComboBox4.Text = "Page 1" Then ActiveSheet.Pagesetup.CenterHeader = "&""Times New Roman,常规""Page &P" ElseIf ComboBox4.Text = "Page 1 of ?" Then ActiveSheet.Pagesetup.CenterHeader = "&""Times New Roman,常规""Page &P of &N" ElseIf ComboBox4.Text = ActiveSheet.Name Then ActiveSheet.Pagesetup.CenterHeader = "&""Times New Roman,常规""&A" ElseIf ComboBox4.Text = ThisWorkbook.Name Then ActiveSheet.Pagesetup.CenterHeader = "&""Times New Roman,常规""&F" Else ActiveSheet.Pagesetup.CenterHeader = ComboBox4.Text End If If ComboBox5.Text = "Page 1" Then ActiveSheet.Pagesetup.RightHeader = "&""Times New Roman,常规""Page &P" ElseIf ComboBox5.Text = "Page 1 of ?" Then ActiveSheet.Pagesetup.RightHeader = "&""Times New Roman,常规""Page &P of &N" ElseIf ComboBox5.Text = ActiveSheet.Name Then ActiveSheet.Pagesetup.RightHeader = "&""Times New Roman,常规""&A" ElseIf ComboBox5.Text = ThisWorkbook.Name Then ActiveSheet.Pagesetup.RightHeader = "&""Times New Roman,常规""&F" Else ActiveSheet.Pagesetup.RightHeader = ComboBox5.Text End If If ComboBox6.Text = "Page 1" Then ActiveSheet.Pagesetup.LeftFooter = "&""Times New Roman,常规""Page &P" ElseIf ComboBox6.Text = "Page 1 of ?" Then ActiveSheet.Pagesetup.LeftFooter = "&""Times New Roman,常规""Page &P of &N" ElseIf ComboBox6.Text = ActiveSheet.Name Then ActiveSheet.Pagesetup.LeftFooter = "&""Times New Roman,常规""&A" ElseIf ComboBox6.Text = ThisWorkbook.Name Then ActiveSheet.Pagesetup.LeftFooter = "&""Times New Roman,常规""&F" Else ActiveSheet.Pagesetup.LeftFooter = ComboBox6.Text End If If ComboBox7.Text = "Page 1" Then ActiveSheet.Pagesetup.CenterFooter = "&""Times New Roman,常规""Page &P" ElseIf ComboBox7.Text = "Page 1 of ?" Then ActiveSheet.Pagesetup.CenterFooter = "&""Times New Roman,常规""Page &P of &N" ElseIf ComboBox7.Text = ActiveSheet.Name Then ActiveSheet.Pagesetup.CenterFooter = "&""Times New Roman,常规""&A" ElseIf ComboBox7.Text = ThisWorkbook.Name Then ActiveSheet.Pagesetup.CenterFooter = "&""Times New Roman,常规""&F" Else ActiveSheet.Pagesetup.CenterFooter = ComboBox7.Text End If If ComboBox8.Text = "Page 1" Then ActiveSheet.Pagesetup.RightFooter = "&""Times New Roman,常规""Page &P" ElseIf ComboBox8.Text = "Page 1 of ?" Then ActiveSheet.Pagesetup.RightFooter = "&""Times New Roman,常规""Page &P of &N" ElseIf ComboBox8.Text = ActiveSheet.Name Then ActiveSheet.Pagesetup.RightFooter = "&""Times New Roman,常规""&A" ElseIf ComboBox8.Text = ThisWorkbook.Name Then ActiveSheet.Pagesetup.RightFooter = "&""Times New Roman,常规""&F" Else ActiveSheet.Pagesetup.RightFooter = ComboBox8.Text End If ActiveSheet.Pagesetup.PrintArea = RefEdit1.Value ActiveSheet.Pagesetup.PrintTitleRows = RefEdit2.Value ActiveSheet.Pagesetup.PrintTitleColumns = RefEdit3.Value

Application.ScreenUpdating = True Application.StatusBar = False ms = MsgBox("页面设置完成,您要现在打印吗?", vbYesNo) If ms = vbNo Then Application.EnableEvents = False Exit Sub End If ActiveSheet.PrintOut Application.EnableEvents = False End Sub

TA的精华主题

TA的得分主题

发表于 2005-1-3 10:38 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助

真的很是谢谢button, 敬礼...

TA的精华主题

TA的得分主题

发表于 2005-1-3 10:43 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
还有事相问,有没办法让这段代码执行的更快啊?

TA的精华主题

TA的得分主题

发表于 2005-1-4 12:07 | 显示全部楼层

TA的精华主题

TA的得分主题

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

关键是精简代码,去除不必要的循环和判断等等。

改了一下,不一定合适,因为没有原文件也不知道是否正确。我这里也没有运行,请根据基本思路自行改善。

Private Sub OkButton_Click() Dim xlsht As Worrksheet Dim comCtl As Control Dim i As Long

With Application .StatusBar = "正在设置页面,请稍候" .ScreenUpdating = False .EnableEvents = True End With

Set xlsht = ActiveSheet With xlsht .PageSetup.LeftMargin = Application.InchesToPoints(TextBox6.Text) .PageSetup.RightMargin = Application.InchesToPoints(TextBox7.Text) .PageSetup.TopMargin = Application.InchesToPoints(TextBox5.Text) .PageSetup.BottomMargin = Application.InchesToPoints(TextBox8.Text) .PageSetup.HeaderMargin = Application.InchesToPoints(TextBox9.Text) .PageSetup.FooterMargin = Application.InchesToPoints(TextBox10.Text) End With If OptionButton1.Value = True Then xlsht.PageSetup.Orientation = xlPortrait Else xlsht.PageSetup.Orientation = xlLandscape End If If OptionButton3.Value = True Then xlsht.PageSetup.Zoom = VBA.CLng(VBA.Left(Len(ComboBox1.Text) - 1)) If ComboBox2.Text <> "" Then With xlsht .PageSetup.Zoom = False .PageSetup.FitToPagesWide = VBA.CLng(Left(ComboBox2.Text, 1)) .PageSetup.FitToPagesTall = VBA.CLng(VBA.Mid(ComboBox2.Text, 4, VBA.Len(ComboBox2.Text) - 5)) End With End If For Each comCtl In Me.Controls If VBA.Left(comCtl.Name, 5) = "Combo" And VBA.CLng(VBA.Right(comCtl.Name, 1)) >= 3 Then If comCtl.Text <> "" Then Select Case comCtl.Text Case "Page 1" xlsht.PageSetup.LeftHeader = "&""Times New Roman,常规""Page &P" Case "Page 1 of ?" xlsht.PageSetup.LeftHeader = "&""Times New Roman,常规""Page &P of &N" Case xlsht.Name xlsht.PageSetup.LeftHeader = "&""Times New Roman,常规""&A" Case ThisWorkbook.Name xlsht.PageSetup.LeftHeader = "&""Times New Roman,常规""&F" Case Else xlsht.PageSetup.LeftHeader = comCtl.Text End Select End If End If Next

With xlsht .PageSetup.PrintArea = RefEdit1.Value .PageSetup.PrintTitleRows = RefEdit2.Value .PageSetup.PrintTitleColumns = RefEdit3.Value End With

Application.ScreenUpdating = True Application.StatusBar = False ms = MsgBox("页面设置完成,您要现在打印吗?", vbYesNo) If ms = vbNo Then Application.EnableEvents = False Exit Sub End If ActiveSheet.PrintOut Application.EnableEvents = True End Sub

[此贴子已经被作者于2005-1-3 13:16:14编辑过]

TA的精华主题

TA的得分主题

发表于 2005-1-2 18:45 | 显示全部楼层

大家习惯了用EXCEL录制宏的方式编程,但是最好不要使用这种方式。

可以声明一个变量如

dim xlsht as worksheet

set xlsht=activesheet

...

'最后释放

set xlsht=nothing

'就可以了。

上述代码中的activesheet用声明的变量xlsht替换即可。

TA的精华主题

TA的得分主题

发表于 2005-1-2 17:31 | 显示全部楼层

Sub Macro1() Dim num As Integer, i As Integer Dim arrSht() As Variant Dim sht As Worksheet num = InputBox("", , 200) Application.ScreenUpdating = False ReDim arrSht(1 To num + 1) arrSht(num + 1) = Sheets("aaa").Name For i = 1 To num Set sht = Worksheets.Add sht.Move after:=Worksheets(Worksheets.Count) sht.Name = CStr(i) arrSht(i) = CStr(i) Next i Sheets(arrSht).FillAcrossSheets Range:=Sheets("aaa").UsedRange Application.ScreenUpdating = True Set sht = Nothing End Sub

TA的精华主题

TA的得分主题

发表于 2004-5-10 12:59 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助

簡單來說,內存多對一個極大的檔案沒有幫助,你的 swap file 大小是自動還是自定?

不過你用 P4 2G 只得 256 M 是否過少呢?內存不太貴吧,至小也用 512 M 啊!

[此贴子已经被作者于2004-5-10 13:33:56编辑过]

【请教】excel中用vba如何释放内存?

【请教】excel中用vba如何释放内存?

TA的精华主题

TA的得分主题

发表于 2004-5-10 12:27 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册

Excel and memory (English)

http://www.decisionmodels.com/memlimits.htm

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

本版积分规则

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

GMT+8, 2025-12-21 00:46 , Processed in 0.026936 second(s), 9 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

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

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

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