| ページ一覧 | ブログ | twitter |  書式 | 書式(表) |

MyMemoWiki

「Excel VBA Utility」の版間の差分

提供: MyMemoWiki
ナビゲーションに移動 検索に移動
(ページの作成:「==Excel VBA Utility== [Excel VBA]{{category VBAソース片}} ===basUtil=== Option Explicit ' Win32 API Private Declare Function GetPrivateProfileString…」)
 
1行目: 1行目:
 
==Excel VBA Utility==
 
==Excel VBA Utility==
[Excel VBA]{{category VBAソース片}}
+
[[Excel VBA]]{{category VBAソース片}}
  
 
===basUtil===
 
===basUtil===
97行目: 97行目:
 
       End If
 
       End If
 
      
 
      
       If Right$(result, 1) <> DIR_SEP Then
+
       If Right$(result, 1) &lt;&gt; DIR_SEP Then
 
           result = result & DIR_SEP
 
           result = result & DIR_SEP
 
       End If
 
       End If
119行目: 119行目:
 
        
 
        
 
       If Not isBlank(Trim$(rootDir)) _
 
       If Not isBlank(Trim$(rootDir)) _
       And ("A" <= drv And drv <= "Z") _
+
       And ("A" &lt;= drv And drv &lt;= "Z") _
 
       And (Mid$(rootDir, 2, 1) = ":") _
 
       And (Mid$(rootDir, 2, 1) = ":") _
 
       Then
 
       Then
208行目: 208行目:
 
       For i = LBound(values) To UBound(values)
 
       For i = LBound(values) To UBound(values)
 
           For j = i + 1 To UBound(values)
 
           For j = i + 1 To UBound(values)
               If StrComp(values(i), values(j), vbTextCompare) > 0 Then
+
               If StrComp(values(i), values(j), vbTextCompare) &gt; 0 Then
 
                   tmp = values(i)
 
                   tmp = values(i)
 
                   values(i) = values(j)
 
                   values(i) = values(j)
261行目: 261行目:
 
       Dim curVal As String
 
       Dim curVal As String
 
       For i = LBound(tmp) To UBound(tmp)
 
       For i = LBound(tmp) To UBound(tmp)
           If i > LBound(tmp) Then
+
           If i &gt; LBound(tmp) Then
 
               If curVal = tmp(i) Then
 
               If curVal = tmp(i) Then
 
                   ReDim Preserve duplicatedItems(dupIdx)
 
                   ReDim Preserve duplicatedItems(dupIdx)

2020年2月15日 (土) 08:02時点における版

Excel VBA Utility

Excel VBAテンプレート:Category VBAソース片

basUtil

  1. Option Explicit
  2. ' Win32 API
  3. Private Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
  4. Private Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpString As String, ByVal lpFileName As String) As Long
  5. Public Const MAX_INI_LEN As Long = 256
  6. Public Const INI_EXTENT As String = ".ini"
  7. Public Const EXCEL_EXTENT As String = ".xls"
  8. Public Const DIR_SEP As String = "\"
  9. '
  10. ' 設定を保存する
  11. '
  12. ' @param book Excelブック
  13. ' @param strKey 設定キー
  14. ' @param strVal 設定値
  15. '
  16. Public Function setAppSetting(ByRef book As Excel.Workbook, _
  17. ByVal strKey As String, _
  18. ByVal strVal As String) As Long
  19. setAppSetting = WritePrivateProfileString( _
  20. APP_NAME, strKey, strVal, getIniFilename(book))
  21. End Function
  22. '
  23. ' 設定を取得する
  24. '
  25. ' @param book Excelブック
  26. ' @param strKey 設定キー
  27. ' @param strDefault 対応する設定値が存在しない場合のデフォルト値
  28. '
  29. Public Function getAppSetting(ByRef book As Excel.Workbook, _
  30. ByVal strKey As String, _
  31. ByVal strDefault As String) As String
  32. Dim strBuf As String * MAX_INI_LEN
  33. Dim result As Long
  34. result = GetPrivateProfileString( _
  35. APP_NAME, strKey, strDefault, strBuf, MAX_INI_LEN, _
  36. getIniFilename(book))
  37. getAppSetting = Left(strBuf, InStr(strBuf, vbNullChar) - 1)
  38. End Function
  39. '
  40. ' 初期化ファイル名を取得する
  41. '
  42. ' @param book Excelブック
  43. '
  44. Private Function getIniFilename(ByRef book As Excel.Workbook) As String
  45. getIniFilename = getWorkbookRerativeFilename(book, INI_EXTENT)
  46. End Function
  47. '
  48. ' EXCEL ファイルのプルパス、拡張子変更文字列を返す
  49. '
  50. ' @param book Excelブック
  51. ' @param extents 拡張子
  52. ' @return Excelのファイル名の拡張子を変更した文字列
  53. '
  54. Private Function getWorkbookRerativeFilename(ByRef book As Excel.Workbook, extents As String) As String
  55. Dim chk As String
  56. Dim tmp As String
  57. tmp = book.Name
  58. chk = String(Len(EXCEL_EXTENT), " ") & tmp
  59. If Right$(chk, Len(EXCEL_EXTENT)) = EXCEL_EXTENT Then
  60. tmp = Left$(tmp, Len(tmp) - Len(EXCEL_EXTENT))
  61. End If
  62. getWorkbookRerativeFilename = getPath(book.path) & tmp & extents
  63. End Function
  64. '
  65. ' パス名を作成(最後が'\'で終わるように整形する)
  66. '
  67. ' @param pathName 整形するパス名
  68. ' @return 整形されたパス名
  69. '
  70. Public Function getPath(ByVal pathName) As String
  71. Dim result As String
  72. result = pathName
  73. If Trim$(result) = "" Then
  74. result = "."
  75. End If
  76. If Right$(result, 1) <> DIR_SEP Then
  77. result = result & DIR_SEP
  78. End If
  79. getPath = result
  80. End Function
  81. '
  82. ' ファイルを選択する
  83. ' fileFilter eg-"Microsoft Excelブック,*.xls,テキストファイル,*.txt"
  84. '
  85. Public Function chooseFile(Optional fileFilter As String, _
  86. Optional rootDir As String, _
  87. Optional filterIndex As Integer, _
  88. Optional title As String, _
  89. Optional buttonText As String, _
  90. Optional MultiSelect As Boolean) As Variant
  91. Dim drv As String
  92. drv = UCase$(Left$(rootDir, 1))
  93. If Not isBlank(Trim$(rootDir)) _
  94. And ("A" <= drv And drv <= "Z") _
  95. And (Mid$(rootDir, 2, 1) = ":") _
  96. Then
  97. Call ChDrive(drv)
  98. Call ChDir(rootDir)
  99. End If
  100. chooseFile = Application.GetOpenFilename(fileFilter, filterIndex, title, buttonText, MultiSelect)
  101. End Function
  102. '
  103. ' フォルダ取得ダイアログを表示する
  104. '
  105. '
  106. ' @return 選択されたフォルダ名
  107. ' @param title タイトルの文字列
  108. ' options 選択オプションの値
  109. ' rootFolder 既定フォルダの文字列
  110. '
  111. Public Function browseForFolder(title As String, _
  112. options, _
  113. Optional rootFolder As String = "") As String
  114. Dim cmdShell As Object
  115. Dim folder As Object
  116. On Error GoTo errHandler
  117. Set cmdShell = CreateObject("Shell.Application")
  118. 'syntax
  119. ' object.BrowseForFolder Hwnd, Title, Options, [RootFolder]
  120. '
  121. Set folder = cmdShell.browseForFolder(0, title, options, rootFolder)
  122. If Not folder Is Nothing Then
  123. browseForFolder = folder.Items.Item.path
  124. Else
  125. browseForFolder = ""
  126. End If
  127. closer:
  128. Set folder = Nothing
  129. Set cmdShell = Nothing
  130. Exit Function
  131. errHandler:
  132. MsgBox Err.Description, vbCritical
  133. GoTo closer
  134. End Function
  135. '
  136. ' 文字列配列を、区切文字で区切られたテキストに展開する
  137. '
  138. ' @param fields 展開する文字列配列
  139. ' @param separator 区切文字
  140. ' @return 区切文字で区切られたテキスト
  141. '
  142. Public Function arrayToSeparetedValueText(ByRef fields() As String, separator As String) As String
  143. Dim ret As String
  144. Dim i As Integer
  145. For i = 0 To UBound(fields)
  146. If i = 0 Then
  147. ret = fields(i)
  148. Else
  149. ret = ret & separator & fields(i)
  150. End If
  151. Next
  152. arrayToSeparetedValueText = ret
  153. End Function
  154. '
  155. ' 文字列に対する簡易ソート
  156. '
  157. ' @param values ソート対象の文字列配列
  158. '
  159. Public Sub bubbleSort(ByRef values() As String)
  160. Dim tmp As String
  161. Dim i As Integer
  162. Dim j As Integer
  163. tmp = ""
  164. For i = LBound(values) To UBound(values)
  165. For j = i + 1 To UBound(values)
  166. If StrComp(values(i), values(j), vbTextCompare) > 0 Then
  167. tmp = values(i)
  168. values(i) = values(j)
  169. values(j) = tmp
  170. End If
  171. Next
  172. Next
  173. End Sub
  174. '
  175. ' 指定された文字列が配列に含まれるか検査する
  176. '
  177. ' @param str 検査文字列
  178. ' @param strAry 検査対象配列
  179. ' @return 配列に値が含まれていればTrue
  180. '
  181. Public Function isContainArray(str As String, strAry() As String) As Boolean
  182. Dim i As Integer
  183. For i = LBound(strAry) To UBound(strAry)
  184. If str = strAry(i) Then
  185. isContainArray = True
  186. Exit Function
  187. End If
  188. Next
  189. isContainArray = False
  190. End Function
  191. '
  192. ' 文字列配列に、重複項目があるかをチェックする
  193. '
  194. ' @param values 検査対象配列
  195. ' @param duplicatedItems 重複項目を文字列配列にセットする
  196. ' @return 重複がある場合に true を返す
  197. '
  198. Public Function duplicatedCheck(ByRef values() As String, ByRef duplicatedItems() As String) As Boolean
  199. Dim result As Boolean
  200. Dim i As Integer
  201. Dim tmp() As String
  202. Dim dupIdx As Integer
  203. result = False
  204. dupIdx = 0
  205. ReDim tmp(UBound(values))
  206. For i = LBound(values) To UBound(values)
  207. tmp(i) = values(i)
  208. Next
  209. Call Util.bubbleSort(tmp)
  210. Dim curVal As String
  211. For i = LBound(tmp) To UBound(tmp)
  212. If i > LBound(tmp) Then
  213. If curVal = tmp(i) Then
  214. ReDim Preserve duplicatedItems(dupIdx)
  215. duplicatedItems(dupIdx) = curVal
  216. dupIdx = dupIdx + 1
  217. result = True
  218. End If
  219. End If
  220. curVal = tmp(i)
  221. Next
  222. duplicatedCheck = result
  223. End Function
  224. '
  225. 'ブランク判定
  226. '
  227. ' @param str 判定する文字列
  228. ' @return 文字列が空ならTrue
  229. '
  230. Public Function isBlank(str As String) As Boolean
  231. isBlank = ((Trim$(str)) = "")
  232. End Function
  233. '
  234. ' Boolean "1":True"0":False に変換
  235. '
  236. ' @param blv 真偽値(Boolean)
  237. ' @return Trueなら"1"、Falseなら"0"
  238. '
  239. Public Function booleanToFlag(blv As Boolean) As String
  240. booleanToFlag = IIf(blv, "1", "0")
  241. End Function
  242. '
  243. ' Boolean "1":True" ":False に変換
  244. '
  245. ' @param blv 真偽値(Boolean)
  246. ' @return Trueなら"1"、Falseなら" "
  247. '
  248. Public Function booleanToFlag2(blv As Boolean) As String
  249. booleanToFlag2 = IIf(blv, "1", " ")
  250. End Function
  251. '
  252. ' フラグ "1":True"0":False に変換
  253. '
  254. ' @param flag フラグ
  255. ' @return "1"ならTrue、それ以外はFalse
  256. '
  257. Public Function flagToBoolean(flag As String) As String
  258. flagToBoolean = IIf((Trim$(flag) = "1"), True, False)
  259. End Function

テンプレート:Ref Util.bas