ExcelVBA
Sub Sample2() Dim buf As String Open "C:\Sample\Data.txt" For Input As #1 Do Until EOF(1) Line Input #1, buf セル = buf Loop Close #1 End Sub ネタ元 http://officetanaka.net/excel/vba/file/file08b.htm
Dir()でも確認できるけど、ファイル検索中など困るときにはこっち使うと吉。 'Dir()を使わないファイル確認 Function FileExist(path) Dim objFileSys Dim strScriptPath Dim strCopyFrom Dim strCopyTo Set objFileSys = CreateObject("Scripting.FileSyste…
▼日付データをYYYYMMDD形式の文字列にするユーザー定義関数 Function YYYYMMDD(日付 As Date) As Variant If 日付 >= #3/1/1900# Then YYYYMMDD = Format(日付, "yyyymmdd") Else YYYYMMDD = CVErr(xlErrValue) End If End Functionネタ元 http://www.relief…
マイナス1とか減算指定なら、今日の日付から引いて日付/時間にする例 Function FUNC(aDate As String,aTime As String) Dim dDate As Date Dim dTime As Date If IsDate(aDate) Then dDate = CDate(aDate) ElseIf CInt(aDate) <= 0 Then dDate = DateAdd("d"…
APIを使う Private Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" _ (ByVal lpstrCommand As String, ByVal lpstrReturnString As String, _ ByVal uReturnLength As Long, ByVal hwndCallback As Long) As Long Sub Sample2() Di…
WorksheetFunction.RTD()がうまくいく時とうまくいかない時があるExcelが忙しいらしいので、Application.Ready = Trueをチェックする。 'Excel のバージョンが 2002 以降の時 If Val(Application.Version) >= 10 Then 'アプリケーションの状態を確認し、準備…
何度も Nothing -> CreateObject しようとするとエラーになることがある。 Set oIE = Nothing での処理がすぐ完結しないときがあるようで、その後に実行される Set oIE = CreateObject("InternetExplorer.Application") のところでこのエラーが出るといった…
こんな感じ。 Dim userAddress As Variant userAddress = Array("東京都", "北海道", "愛知県")ネタ元 http://www.officepro.jp/excelvba/array/index6.html
VBAにもあるのね。条件一致しなかったらブレークで止まってくれます。一般リリースするもので使うなら、テスト関数とか本番環境では呼ばれないところで使うこと。 ネタ元 http://note.phyllo.net/?eid=1106218
Option Explicit Dim WSH,sc Set WSH=CreateObject("WScript.Shell") Set sc = WSH.CreateShortcut("C:\Documents and Settings\xxxx\デスクトップ\メモ帳.lnk") sc.TargetPath = "C:\WINDOWS\notepad.exe" sc.save Set sc = Nothing Set WSH = nothingネタ元…
Shell("cmd.exe /c taskkill /F /IM excel.exe")ネタ元 http://oshiete.goo.ne.jp/qa/5490461.html
MsgBox ActiveSheet.Shapes(Application.Caller).TopLeftCell.Address MsgBox ActiveSheet.Shapes(Application.Caller).TopLeftCell.Row MsgBox ActiveSheet.Shapes(Application.Caller).TopLeftCell.Column ネタ元 http://oshiete.goo.ne.jp/qa/4129092.html
Name / Kill Option Explicit Private Const cnsSOUR = "C:\AAA\SAMPLE1.txt" ' 元ファイル Private Const cnsDEST = "C:\BBB\SAMPLE2.txt" ' 先ファイル ' ファイル移動のサンプル① Sub MOVE_SAMPLE1() ' ファイルを移動(この方法は同一ドライブのみ) Name …
Sub Test() Dim myFol As String myFol = ThisWorkbook.Path & "¥hoge¥" Shell "C:¥Windows¥Explorer.exe " & myFol, vbNormalFocus End Sub ネタ元 http://hinekure.vba-soft.com/?eid=270237 すぐわかるExcelマクロ&VBAマネして使えるサンプル集 Excel 20…
Val()関数使うネタ元 http://www.geocities.jp/cbc_vbnet/function/mojireyu_function.html#val
Sub ユーザーにフォルダーを選択してもらう() Dim dlg As FileDialog Dim fold_path As String Set dlg = Application.FileDialog(msoFileDialogFolderPicker) 'キャンセルボタンクリック時にマクロを終了 If dlg.Show = False Then Exit Sub 'フォルダーの…
参照の追加でMicrosoft Scripting Runtimeを追加すること 'X=列=aStartCols Y=行=aStartRows Sub CSV入力(aInputCsvPath, aStartCols, aStartRows) Dim varFileName As Variant Dim objFSO As New Scripting.FileSystemObject Dim inTS As TextStream Dim st…
Win32_Process クラスを使う。 '指定のプロセスがいたらTrue いないならFalse Function CheckProcess(aExename) As Boolean 'WMIにて使用する各種オブジェクトを定義・生成する。 Dim oClassSet Dim oClass Dim oLocator Dim oService Dim sMesStr 'ローカル…
折り返して全体を表示するを解除するVBAのコード Cells.WrapText = Falseネタ元 http://www.relief.jp/itnote/archives/excel-vba-off-wrap-text.php
URLDownloadToFile APIを使用してWebからファイルをダウンロードする方法 #If Win64 Then Private Declare PtrSafe Function DeleteUrlCacheEntry Lib "wininet" Alias _ "DeleteUrlCacheEntryA" (ByVal lpszUrlName As String) As Long Private Declare Ptr…
フォームを作って vbModelessで表示 UserFormを強制表示させる効果があります。念のために書くおまじない。【標準モジュール】 Public flag As Boolean Sub Sample5() Dim TotalSize As Long, buf As String UserForm1.Show vbModeless UserForm1.Repaint fl…
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer) If CloseMode = 0 Then MsgBox "[×]ボタンでは閉じられません。", 48 Cancel = True End If End Subネタ元 http://officetanaka.net/excel/vba/tips/tips18.htm
10分毎に復帰用のファイルが作られるが、それを止める Private Sub Workbook_Open() ActiveWorkbook.EnableAutoRecover = False End Subネタ元 http://blog.livedoor.jp/katsuyausami/archives/52107517.html
消えると困るのでコピペ■ボタンクリック部分のコード Private Sub CommandButton1_Click() Dim obj As JSON Set obj = GetJSON("test.json") Do While obj.HasNext MsgBox obj.getValue("id") & ":" & obj.getValue("name") Loop End Sub ■ConnectModule '接…
書き込み SaveSetting appname, section, key, data読込 GetSetting(appname, section, key, default)ネタ元 http://officetanaka.net/excel/vba/tips/tips43.htm
3つあるInternetExplorerオブジェクト (WebBrowserコントロール) XMLHTTPオブジェクト HTMLDocumentオブジェクトどれでもけっきょくHTMLDocumentオブジェクトを取り出していろいろいじれる。ネタ元 http://so-zou.jp/software/tech/programming/vba/sample/…
意外なことにどのバージョンでもOKな標準コントロールがないみたい。以下で配布しているモジュールをインポートして使うといい感じ。 http://www.asahi-net.or.jp/~ef2o-inue/download/sub09_020_025.html
ESCキーを押した時にCancel=Trueのボタンを押したこととして処理するみたいネタ元 http://vba.doorblog.jp/archives/51316717.html
ダイアログを後でも出したり、情報を取得する用事があるならMe.Hide もう用事なしならUnload Me ネタ元 http://www.asahi-net.or.jp/~ef2o-inue/vba_k/sub04_080.html
Public Sub ie_quit() Dim objShell Dim objWindow Set objShell = CreateObject("Shell.Application") For Each objWindow In objShell.Windows If TypeName(objWindow.Document) = "HTMLDocument" Then objWindow.Quit Sleep 100 End If Next Set objShell…