EXCEL VBA 实用代码收集(17页).doc
《EXCEL VBA 实用代码收集(17页).doc》由会员分享,可在线阅读,更多相关《EXCEL VBA 实用代码收集(17页).doc(17页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、-EXCEL VBA 实用代码收集-第 17 页图片切换Sub 显示开或关() If ActiveSheet.Shapes(Picture 2).Visible = True Then ActiveSheet.Shapes(Picture 1).Visible = True ActiveSheet.Shapes(Picture 2).Visible = False ElseActiveSheet.Shapes(Picture 2).Visible = TrueActiveSheet.Shapes(Picture 1).Visible = False End IfEnd Sub当前单元格输入数字
2、自动分解Private Sub Worksheet_Change(ByVal Target As Range) If Target.Column 1 Then Exit Sub If Len(Target(1, 1) 1 Then Dim oJs As Objectnguage = JScript Target(1, 2).Resize(1, 254).ClearContents Target.Resize(1, Len(Target) = Split(oJs.eval( & Target & .match(/./g);), ,) End IfEnd Subword批量修改图片大小固定长宽Su
3、b setpicsize() 设置图片大小Dim n图片个数On Error Resume Next 忽略错误For n = 1 To ActiveDocument.InlineShapes.Count InlineShapes类型图片ActiveDocument.InlineShapes(n).Height = 400 设置图片高度为 400pxActiveDocument.InlineShapes(n).Width = 300 设置图片宽度 300pxNext nFor n = 1 To ActiveDocument.Shapes.Count Shapes类型图片ActiveDocumen
4、t.Shapes(n).Height = 400 设置图片高度为 400pxActiveDocument.Shapes(n).Width = 300 设置图片宽度 300pxNext nEnd Sub批量修改图片大小按比例缩放篇Sub setpicsize() 设置图片大小Dim n图片个数Dim picwidthDim picheightOn Error Resume Next 忽略错误For n = 1 Tohapes.Count InlineShapes类型图片picheight = ActiveDocument.InlineShapes(n).Heightpicwidth = Acti
5、veDocument.InlineShapes(n).WidthActiveDocument.InlineShapes(n).Height = picheight * 1.1 设置高度为1.1倍ActiveDocument.InlineShapes(n).Width = picwidth * 1.1 Next nFor n = 1 ToActiveDocument.Shapes.Count Shapes类型图片picheight = ActiveDocument.Shapes(n).Heightpicwidth = ActiveDocument.Shapes(n).WidthActiveDoc
6、ument.Shapes(n).Height = picheight * 1.1 ActiveDocument.Shapes(n).Width = picwidth * 1.1 设置宽度为1.1倍Next nEnd Sub批量给图片加边框Dim i As IntegerFor i = 1 To ActiveDocument.InlineShapes.CountWith ActiveDocument.InlineShapes(i)With .Borders(wdBorderLeft).LineStyle = wdLineStyleSingle.LineWidth = wdLineWidth100
7、pt.Color = wdColorAutomaticEnd WithWith .Borders(wdBorderRight).LineStyle = wdLineStyleSingle.LineWidth = wdLineWidth100pt.Color = wdColorAutomaticEnd WithWith .Borders(wdBorderTop).LineStyle = wdLineStyleSingle.LineWidth = wdLineWidth100pt.Color = wdColorAutomaticEnd WithWith .Borders(wdBorderBotto
8、m).LineStyle = wdLineStyleSingle.LineWidth = wdLineWidth100pt.Color = wdColorAutomaticEnd With.Borders.Shadow = FalseEnd WithWith Options.DefaultBorderLineStyle = wdLineStyleSingle.DefaultBorderLineWidth = wdLineWidth100pt.DefaultBorderColor = wdColorAutomaticEnd WithNext i锁定文件名Private Sub Workbook_
9、Open()If ThisWorkbook.Name 三八节.xls ThenApplication.DisplayAlerts = FalseApplication.QuitEnd IfEnd Sub将数值转换为文本程序扩展 可以将程序代码1和程序代码2略加改动,将一个字符附加到所选单元格的开头。如将cell.Value = & cell.Value换成cell.Value=”I”&cell.Value,则在所选单元格开头添加字符“I”,即可统一单元格开始形式。程序代码1Sub 数值转换为文本1() 通过添加号 Dim cell As Range For Each cell In Selec
10、tion If Not cell.HasFormula Then If Not IsEmpty(cell) Then cell.Value = & cell.Value End If End If NextEnd Sub程序代码2Sub 数值转换成文本2() 只对数字单元格进行操作 Dim cell As Range For Each cell In Selection If Not cell.HasFormula Then If Not IsEmpty(cell) Then If IsNumeric(cell) Then cell.Value = & cell.Value 可根据需要变换字符
11、 End If End If End If NextEnd Sub程序代码3Sub 数值转换为文本3() 通过格式 Dim cell As Range For Each cell In Selection If Not cell.HasFormula Then If Not IsEmpty(cell) Then Selection.NumberFormatLocal = End If End If NextEnd Sub关闭并保存所有工作簿 Option Explicit Sub CloseAllWorkbooks() Dim Book As Workbook For Each Book In
12、 WorkbooksIf Book.NameThisWorkbook.Name Then Book.Close savechanges:=True End If Next Book ThisWorkbook.Close savechanges:=True End Sub 关闭工作簿并将它彻底删除 Option ExplicitSub KillMe() With ThisWorkbook .Saved = True .ChangeFileAccess Mode:=xlReadOnly Kill .FullName .Close False End WithEnd SubA列输出排列组合Sub p
13、ailie()Dim s As String, x() As StringDim starttime As Single, endtime As SingleDim i As Long, j As Integer, k As Integer, Num As Long, n As IntegerDim ALL(), TEMP1 As Long, TEMP2 As Long, arr() As Strings = InputBox(请输入不重复的字母或数字)n = Len(s) 元素个数ReDim x(n - 1)For i = 1 To nx(i - 1) = Mid(s, i, 1)Nexts
14、tarttime = Timer 开始计时Num = 1For i = 1 To nNum = Num * i递归计算n!NextReDim arr(1 To Num, 1 To 1)For i = 1 To NumReDim ALL(1 To n) 初始化数组allALL(1) = x(0)TEMP1 = iFor j = 2 To nTEMP2 = TEMP1 Mod jTEMP1 = TEMP1 jIf TEMP2 = 0 ThenALL(j) = x(j - 1) temp2为 0则放在最后ElseFor k = j To TEMP2 + 1 Step -1ALL(k) = ALL(k
15、 - 1) temp2之后的元素后移一位NextALL(TEMP2) = x(j - 1) temp2不为 0 则置于第temp2个元素前End IfNextarr(i, 1) = Join(ALL, ) 输出Nextendtime = TimerApplication.ScreenUpdating = FalseRange(a1).Resize(Num, 1) = arrApplication.ScreenUpdating = TrueMsgBox 共 & Num & 种排列!用时 & endtime - starttime & 秒!End Sub同薄汇总工作表Sub mysub()Appl
16、ication.ScreenUpdating = FalseDim sh As Worksheet, aa As Long, bb As Long, cc As Long, dd As Longdd = Sheets(汇总).IV1.End(1).ColumnSheets(汇总).Range(Cells(2, 2), Cells(65536, dd).ClearContentsFor Each sh In Worksheets If sh.Name 汇总 Then bb = Sheets(汇总).b65536.End(xlUp).Row + 1 aa = sh.b65536.End(xlUp)
17、.Row cc = sh.IV1.End(1).Column sh.Range(sh.Cells(2, 2), sh.Cells(aa, cc).Copy Sheets(汇总).Cells(bb, 2).PasteSpecial xlPasteValues End If Next shApplication.ScreenUpdating = TrueEnd Sub异薄SHEET1汇总Private Sub CommandButton2_Click() Application.ScreenUpdating = False Dim i&, LastRow&, Path$, FileName$, T
18、WB$, WB As Workbook Path = ThisWorkbook.Path & FileName = Dir(Path & *.xls) Range(A1:X65536).ClearContents Do While Len(FileName) If FileName TWB Then Set WB = Workbooks.Open(Path & FileName) With WB.Worksheets(1) LastRow = .Range(A65536).End(xlUp).Row If LastRow 1 Then .Range(A8:x8).Copyheets(汇总).R
19、ange(A65536).End(xlUp)(2).PasteSpecial Paste:=xlValue End If End With Application.CutCopyMode = False WB.Close True End If FileName = Dir() Loop Range(A1).Select Set WB = Nothing Application.ScreenUpdating = TrueEnd Sub异薄汇总工作表Private Sub CommandButton2_Click() Application.ScreenUpdating = False Dim
20、i&, LastRow&, Path$, FileName$, TWB$, WS As Worksheet, WB As Workbook Path = ThisWorkbook.Path & FileName = Dir(Path & *.xls) Range(A1:X65536).ClearContents Do While Len(FileName) If FileName TWB Then Set WB = Workbooks.Open(Path & FileName) LastRow = WS.Range(A65536).End(xlUp).Row If LastRow 1 Then
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- EXCEL VBA 实用代码收集17页 实用 代码 收集 17
限制150内