日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【原创】VBA学习笔记(21) VBA函数,appliacation函数,工作表函数,三种同名函数对比举例(3个例子)

發布時間:2023/12/9 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【原创】VBA学习笔记(21) VBA函数,appliacation函数,工作表函数,三种同名函数对比举例(3个例子) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

?

1 VBA里可以使用的3類函數,都是獨立的

  • VBA函數
  • application函數
  • application.worksheetfunction函數

?

  • VBA函數,appliacation函數,工作表函數,三種同名函數完全獨立
  • EXCEL工作表函數(默認都是只能針對一個cell做作用的! 并且一般是cell.value)

?

?

1.1 比如 match函數

  • vba.match? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??好像是沒有
  • application.match()? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? '運行時如果查不到,會返回錯誤值
  • application.worksheetfunction.match()? ? '運行時如果查不到會直接報錯跳出
Sub test1()Dim dict1 As New DictionaryDim dict2 As Object Set dict2 = CreateObject("scripting.dictionary")arr1 = Array(1, 2, 3, 4, 5) arr2 = [{11,22,33,44,55}] '下標默認從1開始,因為是EXCEL格式For i = LBound(arr1) To UBound(arr1)dict1(arr1(i)) = arr2(i + 1) NextFor Each i In dict1.Keys()Debug.Print i & "," & dict1(i) Nexttarget1 = 55 Debug.Print dict1.Keys(Application.Match(target1, dict1.Items, 0) - 1) Debug.Print dict1.Keys(WorksheetFunction.Match(target1, dict1.Items, 0) - 1) 'Debug.Print dict1.Keys(Match(target1, dict1.Items, 0) - 1) '沒這個VBA函數End Sub

?

?

1.2 比如 trim函數

? ? trim() 是不是可以不只針對一個cell? 多個?可以針對字符串中間的空格不處理

  • trim()? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?' 只處理首位的空格
  • application.trim()? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ????'處理首位的空格,中間的空格也會被縮為1個
  • application.worksheetfunction.trim()? ? ? ? ?? '處理首位的空格,中間的空格也會被縮為1個
Sub test1()str1 = " a b c d e "Debug.Print "str1="; str1 Debug.Print "Trim(str1)=" & Trim(str1) Debug.Print "Application.Trim(str1)=" & Application.Trim(str1) Debug.Print "Application.WorksheetFunction.Trim(str1)=" & Application.WorksheetFunction.Trim(str1)End Sub

?

?

?1.3 比如replace函數(VBA函數和 worksheetfunction 的replace語法完全不同)

  • replace() / VBA.Replace(目標我在你這字符串, "替換后的新文本", "要被替換的文本")

?

  • worksheetfunction.replace() / Application.worksheetfunction.replace()
  • worksheetfunction.Replace(old_text,start_num,num_chars,new_text)
  • worksheetfunction.replace(目標完整字符串,開始位置,從開始位置開始的替換個數,新的文本)

?

Sub test1()str1 = "hello,xxx,xxx,xxx"Debug.Print str1 Debug.Print Replace(str1, "xxx", "yyy") Debug.Print Application.Replace(str1, 7, 3, "y") Debug.Print WorksheetFunction.Replace(str1, 11, 2, "yy")End Sub

?

1.4? VBA.iserror() 函數 和 application.iferror() 函數

Sub test100()Debug.Print Application.Match(3, Array(1, 3, 5), 0) Debug.Print IsError(Application.Match(3, Array(1, 3, 5), 0)) Debug.Print Application.Match(2, Array(1, 3, 5), 0) Debug.Print IsError(Application.Match(2, Array(1, 3, 5), 0)) Debug.PrintDebug.Print Application.IfError(Application.Match(3, Array(1, 3, 5), 0), "999") Debug.Print Application.IfError(Application.Match(2, Array(1, 3, 5), 0), "888") Debug.PrintEnd Sub

總結

以上是生活随笔為你收集整理的【原创】VBA学习笔记(21) VBA函数,appliacation函数,工作表函数,三种同名函数对比举例(3个例子)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。