九色国产,午夜在线视频,新黄色网址,九九色综合,天天做夜夜做久久做狠狠,天天躁夜夜躁狠狠躁2021a,久久不卡一区二区三区

打開APP
userphoto
未登錄

開通VIP,暢享免費(fèi)電子書等14項(xiàng)超值服

開通VIP
OFFICE Excel表格中常用的vba代碼集錦

1.用于批量刪除圖片的VBA代碼

  1. sub del_pic()
  2. For Each a In ActiveSheet.Shapes
  3. If a.Type <> 8 Then
  4. a.Delete
  5. End If
  6. Next a
  7. end sub()

2.批量插入圖片的VBA代碼

假設(shè)要插入的列為B列,A列是對(duì)應(yīng)的圖片名稱,圖片路徑和工作表路徑需要一致,插入的圖片從B2開始

無邊距:

  1. '無邊距
  2. Sub aaimg()
  3. Dim a As Shape
  4. Dim rg As Range
  5. r_num = [a65536].End(xlUp).Row
  6. '先刪除已經(jīng)存在的
  7. For Each a In ActiveSheet.Shapes
  8. If a.Type <> 8 Then
  9. a.Delete
  10. End If
  11. Next a
  12. '寬度
  13. Columns('B:B').ColumnWidth = 11
  14. '高度
  15. Rows('2:' & r_num).RowHeight = 92
  16. '設(shè)置范圍
  17. For Each rg In Range('b2:b' & r_num)
  18. ActiveSheet.Shapes.AddShape(msoShapeRectangle, rg.Left, rg.Top, rg.Width, rg.Height).Select
  19. '報(bào)錯(cuò)就繼續(xù)
  20. On Error Resume Next
  21. '無邊框
  22. Selection.ShapeRange.Line.Visible = msoFalse
  23. Rem 設(shè)置偏移
  24. Selection.ShapeRange.Fill.UserPicture ThisWorkbook.Path & '\' & rg.Offset(0, -1) & '.png'
  25. Next rg
  26. End Sub

有邊距:

  1. '有邊距
  2. Sub aaimg()
  3. Dim a As Shape
  4. Dim rg As Range
  5. r_num = [a65536].End(xlUp).Row
  6. '先刪除已經(jīng)存在的
  7. For Each a In ActiveSheet.Shapes
  8. If a.Type <> 8 Then
  9. a.Delete
  10. End If
  11. Next a
  12. '寬度
  13. Columns('B:B').ColumnWidth = 11
  14. '高度
  15. Rows('2:' & r_num).RowHeight = 92
  16. '設(shè)置范圍
  17. For Each rg In Range('b2:b' & r_num)
  18. ActiveSheet.Shapes.AddShape(msoShapeRectangle, rg.Left + 4, rg.Top + 4, rg.Width - 8, rg.Height - 8).Select
  19. '報(bào)錯(cuò)就繼續(xù)
  20. On Error Resume Next
  21. '無邊框
  22. Selection.ShapeRange.Line.Visible = msoFalse
  23. Rem 設(shè)置偏移
  24. Selection.ShapeRange.Fill.UserPicture ThisWorkbook.Path & '\' & rg.Offset(0, -1) & '.png'
  25. Next rg
  26. End Sub

3.批量下載文件或圖片的VBA代碼

假設(shè)下載文件的url在A列,文件名稱在B列,要保存的文件類型在C列

  1. Sub downloadimg()
  2. '給定網(wǎng)址下載圖片或視頻
  3. Dim H, S, f_type, name, filename
  4. 'f_type 文件類型 name 文件名稱 filename 路徑名稱
  5. r_num = [a65536].End(xlUp).Row
  6. filename = ThisWorkbook.Path & '\' & 'img'
  7. If Dir(filename, vbDirectory) = '' Then '如果文件不存在
  8. MkDir filename
  9. GoTo main
  10. End If
  11. main:
  12. Set H = CreateObject('Microsoft.XMLHTTP')
  13. For i = 1 To r_num
  14. name = Range('b' & i).Value
  15. If name = '' Then name = i '為空則默認(rèn)為數(shù)字
  16. f_type = Range('c' & i).Value
  17. On Error Resume Next
  18. H.Open 'GET', Range('A' & i), False '網(wǎng)絡(luò)中的文件URL
  19. H.send
  20. Set S = CreateObject('ADODB.Stream')
  21. S.Type = 1
  22. S.Open
  23. S.write H.Responsebody
  24. S.savetofile filename & '\' & name & '.' & f_type, 2 '本地保存文件名
  25. S.Close
  26. Next i
  27. End Sub

4.查找本周的周一以及周日

  1. Sub ff()
  2. d = '2019/8/12'
  3. MsgBox DateAdd('d', -(Weekday(d, 0) - 1), d) '周一
  4. MsgBox DateAdd('d', (7 - Weekday(d, 0)), d) '周日
  5. 'MsgBox Weekday(d, 0)
  6. End Sub

5.獲取最后一列的行號(hào)的VBA代碼

  1. Function get_col(col_num)
  2. '輸出字母形式的列名稱
  3. If col_num <= 26 Then
  4. col_str = Chr(64 + col_num)
  5. Else
  6. b_num = col_num \ 26
  7. e_num = col_num Mod 26
  8. If e_num = 0 Then
  9. col_str = Chr(64 + b_num - 1) + Chr(64 + 26)
  10. Else
  11. col_str = Chr(64 + b_num) + Chr(64 + e_num)
  12. End If
  13. End If
  14. get_col = col_str
  15. End Function

6.二維數(shù)組與一維數(shù)組,獲取某行與某列,并轉(zhuǎn)為一維數(shù)組

  1. Sub ar()
  2. Dim arr1(1 To 6, 1 To 3), arr2()
  3. Dim i, j As Integer
  4. For i = 1 To 6
  5. For j = 1 To 3
  6. arr1(i, j) = i * j
  7. Next j
  8. Next i
  9. '獲取第三列
  10. arr2 = Application.Transpose(Application.Index(arr1, 0, 3))
  11. '獲取第三行
  12. arr2 = Application.Index(arr1, 3, 0)
  13. For i = 1 To UBound(arr2)
  14. Debug.Print arr2(i)
  15. Next i
  16. End Sub

7.打開工作簿,并將該工作表的某個(gè)工作表放入數(shù)組的VBA代碼

  1. Function get_arr(file, sh_name)
  2. '打開一個(gè)工作簿,并返回一個(gè)數(shù)組,第一個(gè)為路徑,第二個(gè)參數(shù)為工作表的序號(hào)(工作表名稱)
  3. Dim wb As Workbook
  4. Set wb = Workbooks.Open(file)
  5. wb.Sheets(sh_name).Select
  6. row_num = [b65536].End(xlUp).Row
  7. col_num = ActiveSheet.UsedRange.Columns.Count
  8. col_str = get_col(col_num) '獲取行名稱
  9. arr = Sheets(sh_name).Range('a1:' & col_str & row_num)
  10. wb.Close False
  11. Set wb = Nothing
  12. get_arr = arr
  13. End Function

8.循環(huán)當(dāng)前工作簿,對(duì)每個(gè)工作表進(jìn)行操作的VBA代碼

  1. Sub type_sum()
  2. For Each sht In Sheets
  3. sh_name = sht.Name
  4. Sheets(sh_name).Select
  5. 'Call tianchong '執(zhí)行某個(gè)操作
  6. Next sht
  7. End Sub

9.在當(dāng)前工作簿增加工作表,如果名字相同會(huì)刪除

  1. Function add_sheet(sh_name)
  2. '添加工作表
  3. '刪除舊數(shù)據(jù)
  4. Application.DisplayAlerts = False
  5. For Each sht In Sheets
  6. If sht.Name = sh_name Then sht.Delete
  7. Next sht
  8. Application.DisplayAlerts = True
  9. '添加新工作表
  10. Sheets.add After:=Sheets(Sheets.Count)
  11. Sheets(Sheets.Count).Name = sh_name
  12. End Function

10.打開word并對(duì)其中內(nèi)容進(jìn)行替換

  1. Sub open_wd2(arr)
  2. '循環(huán)體
  3. Application.EnableCancelKey = xlDisabled
  4. Dim wd, w_doc As Object
  5. Dim common_path
  6. common_path = 'D:\new\' '輸出的文檔位置
  7. doc_path='D:\analyse\20191118\計(jì)算\模板.docx' 'word模板路徑
  8. Set wd = CreateObject('Word.application')
  9. 'wd.Visible = True '設(shè)置窗體可見
  10. Set w_doc = wd.Documents.Open(doc_path)
  11. Set myRange = w_doc.Content
  12. '替換內(nèi)容
  13. For i = UBound(arr) To 2 Step -1
  14. myRange.Find.Execute FindText:=arr(i, 2), _
  15. ReplaceWith:=arr(i, 3), Replace:=wdReplaceAll
  16. Next i
  17. w_doc.SaveAs2 common_path & arr(2, 3) & '結(jié)果.docx'
  18. w_doc.Close
  19. '退出word程序
  20. wd.Quit
  21. Set wd = Nothing
  22. Set w_doc = Nothing
  23. End Sub
  24. Sub main_func()
  25. Application.EnableCancelKey = xlDisabled
  26. With CreateObject('Wscript.Shell')
  27. Call .RegWrite('HKEY_CURRENT_USER\Control Panel\International\iLZero', '1')
  28. '設(shè)置小數(shù)點(diǎn)前導(dǎo)0顯示即 [0.7]格式
  29. End With
  30. ex_path='D:\analyse\模板.xlsx' 'excel模板位置
  31. arr1 = get_arr(ex_path, 1) '替換的格式,get_arr為上方的函數(shù)
  32. arr2 = get_arr(ex_path, 2) '替換的數(shù)據(jù)
  33. For j = 2 To UBound(arr2)
  34. For k = 2 To UBound(arr1)
  35. arr1(k, 3) = CStr(arr2(j, k))
  36. Next k
  37. open_wd2 (arr1)
  38. Next j
  39. 'With CreateObject('Wscript.Shell')
  40. 'Call .RegWrite('HKEY_CURRENT_USER\Control Panel\International\iLZero', '0')
  41. '恢復(fù)到默認(rèn) 小數(shù)點(diǎn)前導(dǎo)0不顯示 [.7]狀態(tài)
  42. 'End With
  43. End Sub

運(yùn)行時(shí),需要復(fù)制本文中的5、7函數(shù),同時(shí)設(shè)置好word模板路徑、excel模板路徑和輸出位置。

11.將當(dāng)前工作表中的公式轉(zhuǎn)換成數(shù)值

  1. Sub shuzhi()
  2. '公式轉(zhuǎn)為數(shù)值
  3. row_num = [a65536].End(xlUp).Row
  4. col_num = ActiveSheet.UsedRange.Columns.Count
  5. col_str = get_col(col_num)
  6. Range('A1:' & col_str & row_num).Copy
  7. Range('A1').Select
  8. Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
  9. :=False, Transpose:=False
  10. End Sub

12.刪除特定行

在原有數(shù)據(jù)上直接修改

  1. Sub ffa()
  2. '刪除對(duì)應(yīng)行
  3. row_num = [a65536].End(xlUp).Row
  4. For i = row_num To 1 Step -1
  5. If Cells(1, i) = '同比' Or Cells(1, i) = '' Then '此處填寫條件
  6. Columns(i).Delete
  7. End If
  8. Next i
  9. End Sub

先備份再進(jìn)行刪除

  1. Function add_sheet(sh_name)
  2. '添加工作表
  3. '刪除舊數(shù)據(jù)
  4. Application.DisplayAlerts = False
  5. For Each sht In Sheets
  6. If sht.Name = sh_name Then sht.Delete
  7. Next sht
  8. Application.DisplayAlerts = True
  9. '添加新工作表
  10. Sheets.add After:=Sheets(Sheets.Count)
  11. Sheets(Sheets.Count).Name = sh_name
  12. End Function
  13. Sub ffa()
  14. '刪除對(duì)應(yīng)行
  15. row_num = [a65536].End(xlUp).Row
  16. col_num = ActiveSheet.UsedRange.Columns.Count
  17. col_str = get_col(col_num)
  18. arr=Range('A1:' & col_str & row_num)
  19. add_sheet('刪除后')
  20. Sheets('刪除后').Range('a1').Resize(row_num, UBound(arr, 2)) = arr
  21. For i = row_num To 1 Step -1
  22. If Cells(1, i) = '同比' Or Cells(1, i) = '' Then '此處填寫條件
  23. Columns(i).Delete
  24. End If
  25. Next i
  26. End Sub

13.判斷文件夾和文件是否存在的VBA代碼

  1. Sub fe()
  2. testfile = 'D:\analyse\20191118\計(jì)算\new\'
  3. If Dir(testfile, vbDirectory) = '' Then
  4. MsgBox '不存在'
  5. Else
  6. MsgBox '存在'
  7. End If
  8. End Sub

14.添加引用

  1. 'Name: Excel Major: 1 Minor: 7 GUID: {00020813-0000-0000-C000-000000000046}
  2. 'Name: DAO Major: 5 Minor: 0 GUID: {00025E01-0000-0000-C000-000000000046}
  3. 'Name: WMPLib Major: 1 Minor: 0 GUID: {6BF52A50-394A-11D3-B153-00C04F79FAA6}
  4. 'Name: VBIDE Major: 5 Minor: 3 GUID: {0002E157-0000-0000-C000-000000000046}
  5. 'Name: Office Major: 2 Minor: 5 GUID: {2DF8D04C-5BFA-101B-BDE5-00AA0044DE52}
  6. 'Name: stdole Major: 2 Minor: 0 GUID: {00020430-0000-0000-C000-000000000046}
  7. 'Name: Word Major: 8 Minor: 5 GUID: {00020905-0000-0000-C000-000000000046}
  8. 'Name: VBA Major: 4 Minor: 1 GUID: {000204EF-0000-0000-C000-000000000046}
  9. Sub AutoAddRef()
  10. Dim strGUID As String
  11. strGUID = '{00020905-0000-0000-C000-000000000046}' 'Microsoft Windows Media Player Marjor=1 Minor=0
  12. ThisDocument.VBProject.References.AddFromGuid GUID:=strGUID, Major:=8, Minor:=5
  13. End Sub
本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊舉報(bào)。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
ExcelVBA字典用法之按列拆分工作表
16,匯總至多工作簿(Move/SaveAs)
合并報(bào)表太難了!我,看了30秒,放棄……
VBA代碼翻譯成Python調(diào)用示例
取得工作表名稱、單元格賦值個(gè)工作表名稱
“VBA”學(xué)習(xí)筆記
更多類似文章 >>
生活服務(wù)
熱點(diǎn)新聞
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服