Skip to content

Instantly share code, notes, and snippets.

View imihito's full-sized avatar

imihito imihito

View GitHub Profile
@imihito
imihito / file0.txt
Created January 3, 2019 11:12
Powershellのコードの字句解析 ref: https://qiita.com/nukie_53/items/c20cab099db5dde35ba7
# 字句解析したいスクリプト文字列
[string]$code = {Get-ChildItem -LiteralPath $PSHOME -Filter *.exe}.ToString()
<# $code =>
Get-ChildItem -LiteralPath $PSHOME -Filter *.exe
#>
# 解析エラーを入れる変数。中身は`Tokenize`内で設定されるため$nullでOK
[Collections.ObjectModel.Collection[Management.Automation.PSParseError]]$parseErrors = $null
# トークン化
'配列の次元数を取得する。初期化していない動的配列の場合は0
Public Function DimensionsOf(anyArray As Variant) As Long
If Not VBA.IsArray(anyArray) Then Err().Raise 13
'VBAの多次元配列の次元の上限
Const MAX_ARRAY_DIMENSION = 60
Dim d As Long, no_mean_var As Long
On Error Resume Next
For d = 1 To MAX_ARRAY_DIMENSION
@imihito
imihito / file0.txt
Created October 11, 2018 14:18
Excelのコメントに「本文のフォント」をまとめて設定する(むりやり) ref: https://qiita.com/nukie_53/items/27b88adcca1ad3db4746
'ワークシート内の全てのコメントのフォントを「本文のフォント」に変更する
'COM操作では難しそうなため、SendKeysを使用して強引に変更する
Public Sub ChangeAllCommentFontToThemeMinorFont(ws As Excel.Worksheet)
'VBAのSendKeysのバグ対策のため
Dim wsShell As Object
Set wsShell = VBA.CreateObject("WScript.Shell")
'SendKeysで操作するため、対象のシートを最前面にする
Dim wb As Excel.Workbook
Set wb = ws.Parent
@imihito
imihito / DisableExcelGlobal.bas
Last active June 17, 2019 13:08
グローバルなRangeプロパティへの参照を機械的に検知するアイディア ref: https://qiita.com/nukie_53/items/4e9226ac8f747e772afd
Option Explicit
Option Private Module
'Method override
Public Sub Calculate(a): End Sub
Public Sub DDEExecute(): End Sub
Public Sub DDEInitiate(): End Sub
Public Sub DDEPoke(): End Sub
Public Sub DDERequest(): End Sub
Public Sub DDETerminate(): End Sub
@imihito
imihito / #REF!エラー判定サンプル
Last active December 16, 2019 10:08
VBAでセルのエラーの種類を判定する(CVErr + XlCVError 列挙型) ref: https://qiita.com/nukie_53/items/6c2d9d4699149bebe863
Dim refErr As Variant
refErr = CVErr(Excel.XlCVError.xlErrRef)
'適当な#REF!を作成
ActiveCell.Formula = "=INDIRECT(""定義されていない名前"")"
Debug.Print ActiveCell.Value '->エラー 2023
Debug.Print ActiveCell.Text '->#REF!
Debug.Print ActiveCell.Value = refErr '->True
@imihito
imihito / RunningObjectTableManager.cs
Last active June 9, 2022 12:26
Running Object Tableからオブジェクトを取得する(例:開かれているExcelのブックの一覧を取得する) ref: https://qiita.com/nukie_53/items/5d681a2f49a137204c14
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;
using Excel = Microsoft.Office.Interop.Excel;
/// <summary>
/// Running Object Table関連操作のラッパークラス
@imihito
imihito / EarlyBinding
Last active March 31, 2022 06:13
事前バインディングと実行時バインディングでWithの挙動が変わる事例 ref: https://qiita.com/nukie_53/items/d2e541ce1ea215afda1b
'require Microsoft Scripting Runtime
Private Sub SampleEarlyBinding()
Dim fso As Scripting.FileSystemObject
Set fso = VBA.CreateObject("Scripting.FileSystemObject")
Dim tempFilePath As String
tempFilePath = fso.BuildPath(VBA.Environ$("TEMP"), fso.GetTempName())
With fso.CreateTextFile(tempFilePath)
'ファイルを開いているので削除できない
@imihito
imihito / file0.txt
Last active August 26, 2018 02:08
VBA標準機能で作成できる要素数0の配列 ref: https://qiita.com/nukie_53/items/9a7a1eb07eff50ae1e8b
'Option Base 0
Dim myArray() As Variant
ReDim myArray(0) ' 要素数0ではなく添え字が0~0の要素数1個の配列
@imihito
imihito / file0.txt
Created August 16, 2018 11:24
Get-ChildItemの結果を元にファイルをリネームするときの注意(重複操作) ref: https://qiita.com/nukie_53/items/5c46d992a2c754fc61a0
Name Value
---- -----
PSVersion 4.0
WSManStackVersion 3.0
SerializationVersion 1.1.0.1
CLRVersion 4.0.30319.42000
BuildVersion 6.3.9600.18968
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0}
PSRemotingProtocolVersion 2.2
@imihito
imihito / WindowsPowerShell
Created August 12, 2018 02:05
VBAでUTC時刻・オフセット付き時刻を取得する(WMI使用) ref: https://qiita.com/nukie_53/items/ca89faede67c905912eb
Get-ChildItem -LiteralPath 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time Zones'