Sub 分解して展開する() Dim ws As Worksheet Dim lastRow As Long Dim i As Long Dim num As String Dim startCol As Long Dim j As Long Dim digit As String Dim integerPart As String Dim decimalPart As String Dim numLength As Long Dim decimalLength As Long ' ワークシートを指定 Set ws = ThisWorkbook.Sheets("Sheet1") ' シート名を…
Sub 分解して展開する() ’条件:A列の数値⇒1億はG列~展開とする 'lastRow → lastR: 最終行の変数名を lastR に短縮。'num → n: 数値を格納する変数を n に短縮。'startCol → sCol: 開始列を指す変数名を sCol に短縮。'digit → dgt: 各桁を格納する変数名を dgt に短縮。'integerPart → intP: 整数部分の変数名を intP に短縮。'decimalPart → decP: 小数部分の変数名を decP に短縮。'numLength → nLen: 整数部分の桁数を表す変数名を nLen に短縮。'dec…
'//自作関数版 =MID(SplitText1(A1), 1, 1) Function SplitText1(inputStr As String) As String Dim cleanStr As String ' 数字の場合は小数点を除去 If IsNumeric(inputStr) Then cleanStr = Replace(inputStr, ".", "") ' 小数点を除去 Else cleanStr = inputStr ' 文字列の場合、そのまま End If ' 文字列を1文字ずつ分解して返す SplitTextWithoutDecimal = cleanStrEnd…
Sub セル値の分解() Dim str As String Dim i As Integer Dim lastRow As Long Dim rowNum As Long Dim cleanStr As String ' A列の最終行を取得 lastRow = Cells(Rows.Count, 1).End(xlUp).Row ' A列の各セルを処理 For rowNum = 1 To lastRow ' A列のセルの内容を取得 str = Cells(rowNum, 1).Value ' 数字の場合、小数点を除去 If IsNumeric(str) Then ' 小数点を除去する cle…
Sub Oracle ODBCドライバの接続() ' 変数の宣言 Dim conn As Object Dim connectionString As String Dim userID As String Dim password As String Dim tnsService As String Dim odbcDriver As String ' シートからユーザーID とパスワードを読み取る(例: シート1のA1とB1セル) userID = ThisWorkbook.Sheets("Sheet1").Range("A1").Value password = ThisWorkbook…
Sub日付による条件判断() Dim cell As Range Dim referenceDate As Date Dim oneWeekBefore As Date Dim targetRange As Range Dim searchRange As Range Dim foundCell As Range ' 検索する範囲(例:B1:B10)を指定 Set searchRange = Range("B1:B10") ' 今日の日付を基準日として設定 referenceDate = Date ' 今日の日付を基準に設定 ' 検索範囲内で基準日を検索 Set foundCell = sea…
Sub ExcelからAccess抽出() Dim accessApp As Object Dim accessDbPath As String Dim queryName As String Dim conn As Object Dim connectionString As String Dim userName As String Dim password As String Dim odbcDSN As String ' Excelシートからユーザー名とパスワードを取得 userName = ThisWorkbook.Sheets("Sheet1").Range("A1").Value…
Sub text復号化() Dim fs As Object Dim textFile As Object Dim encryptedText As String Dim plainText As String ' 暗号化されたファイルを読み込む Set fs = CreateObject("Scripting.FileSystemObject") Set textFile = fs.OpenTextFile("C:\path\to\your\encrypted_credentials.txt", 1) encryptedText = textFile.ReadAll textFile.Clo…
Sub text暗号化() Dim fs As Object Dim textFile As Object Dim plainText As String Dim encryptedText As String ' ユーザー名とパスワード Dim OracleUsername As String Dim OraclePassword As String OracleUsername = "yourUsername" OraclePassword = "yourPassword" ' ユーザー名とパスワードを1つの文字列にまとめる plainText = OracleUsername & vbC…
Sub Z列に各行の最後の非空セルの値を転記() Dim rng As Range Dim C, cell As Range Dim lastNonEmptyCell As Range Dim inputRange As Range ' ユーザーにセル範囲を指定させるための InputBox を表示 On Error Resume Next Set inputRange = Application.InputBox("セル範囲を指定してください。例:A1:E10", "セル範囲選択", Type:=8) On Error GoTo 0 If inputRange Is Nothing Then…
Sub 検索結果を指定したひな形に転記してデスクトップに保存2() ' Zフォルダにある対象のExcelファイル(ブック)を開く Dim sourceFolder As String sourceFolder = "C:\Users\YourUsername\Documents\Zフォルダ\" ' Zフォルダのパスを指定 Dim sourceWorkbook As Workbook Dim targetSheet As Worksheet Set sourceWorkbook = Workbooks.Open(sourceFolder & "ひな形ファイル.xlsx") ' ひな形のファイル名…
Sub 検索結果を指定のひな形に転記して保存() '配列バージョン ' Zフォルダにある対象のExcelファイル(ブック)を開く Dim sourceFolder As String sourceFolder = "C:\Users\YourUsername\Documents\Zフォルダ\" ' Zフォルダのパスを指定 Dim sourceWorkbook As Workbook Dim targetSheet As Worksheet Set sourceWorkbook = Workbooks.Open(sourceFolder & "対象のファイル.xlsx") ' 対象のファイル名を…
For j = 2 To lastRowTarget ' 指定ブックのA列を検索(ヘッダー行を除く) If wsTarget.Rows(j).Hidden = False Then ' 非表示の行を無視 If wsTarget.Cells(j, 1).Value = searchKey Then foundRow = j Exit For ' 一致した行が見つかったのでループを抜ける End If End IfNext j
Sub 罫線() Dim ws As Worksheet Dim LastRow As Long Dim LastCol As Long Dim i As Long Dim RangeToFormat As Range ' シートを指定(例: シート1) Set ws = ThisWorkbook.Sheets("Sheet1") ' 最終行(データがある最後の行)を取得 LastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row ' 最終列(データがある最後の列)を取得 LastCol = ws.Cells(1, ws.Columns.Count…
Sub リストの罫線() Dim ws As Worksheet Dim LastRow As Long Dim LastCol As Long Dim i As Long Dim RangeToFormat As Range ' シートを指定(例: シート1) Set ws = ThisWorkbook.Sheets("Sheet1") ' 最終行(データがある最後の行)を取得 LastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row ' 最終列(データがある最後の列)を取得 LastCol = ws.Cells(1, ws.Columns.C…
Sub SkipErrorCells() Dim ws As Worksheet Dim cell As Range Dim result As Variant ' シートの指定 (ここではActiveSheetを使っています) Set ws = ActiveSheet ' 範囲を指定 (例えばA1からA10まで) For Each cell In ws.Range("A1:A10") On Error Resume Next ' エラーが発生しても次の行に進む result = cell.Value * 2 ' 例: 数値に対して計算を行う (エラーが発生する可能性あり) If Err.Nu…
VBAの用途別、適切なデータ型種類ガイドライン 参考に。。。
VBAの用途別、適切なデータ型種類ガイドライン 参考に。。。 整数型: Integer: 小さな範囲の整数(-32,768 ~ 32,767)。 Long: 大きな整数(-2,147,483,648 ~ 2,147,483,647)。 浮動小数点型: Single: 浮動小数点数(精度が少し低い)。 Double: 高精度の浮動小数点数。 文字列型: String: 文字列データ(名前、住所など)。 論理型: Boolean: 真偽値(True または False)。 日付型: Date: 日付や時刻。 汎用型: Variant: あらゆる型のデータを格納可能。ただし、使用時には注意のこと。 …
Sub 転記() Dim folderPath As String Dim fileName As String Dim wb As Workbook Dim isOpen As Boolean Dim tempWb As Workbook Dim wsSource As Worksheet Dim wsTarget As Worksheet Dim lastRowSource As Long Dim lastRowTarget As Long Dim key As String Dim i As Long Dim matchRow As Long Dim cell As Range ' チェ…
Sub 転記() Dim folderPath As String Dim fileName As String Dim wb As Workbook Dim isOpen As Boolean Dim tempWb As Workbook Dim wsSource As Worksheet Dim wsTarget As Worksheet Dim lastRowSource As Long Dim lastRowTarget As Long Dim key As String Dim i As Long Dim matchRow As Long Dim cell As Range ' チェ…
Sub 転記() Dim folderPath As String Dim fileName As String Dim wbSource As Workbook ' アクティブブック Dim wsSource As Worksheet ' アクティブブックのシート Dim wbTarget As Workbook ' 指定したブック Dim wsTarget As Worksheet ' 指定したシート Dim lastRowSource As Long ' アクティブブックの最終行 Dim lastRowTarget As Long ' 指定ブックの最終行 Dim i As Long, j…
Sub 配列による転記() Dim folderPath As String Dim fileName As String Dim wbSource As Workbook ' アクティブブック Dim wsSource As Worksheet ' アクティブブックのシート Dim wbTarget As Workbook ' 指定したブック Dim wsTarget As Worksheet ' 指定したシート Dim lastRowSource As Long ' アクティブブックの最終行 Dim lastRowTarget As Long ' 指定ブックの最終行 Dim i As Lo…
Sub データ更新() Dim folderPath As String Dim fileName As String Dim wb As Workbook Dim ws As Worksheet Dim targetWs As Worksheet Dim tempWs As Worksheet Dim lastRow As Long Dim targetRow As Long Dim i As Long Dim j As Long Dim conditionColumn As Long Dim conditionValue As Variant Dim foundRow As Long ' …
’~実例コード~ ' フィルターがかかっている場合、フィルターを解除 If wsData.AutoFilterMode Then wsData.ShowAllData ' フィルター解除 End If
#M:: { if (IsUpDisplayPrimary) { ; 下ディスプレイをメインに設定 Run, C:\_cmd\multimonitortool-x64\MultiMonitorTool.exe /SetPrimary 2 IsUpDisplayPrimary := false } else { ; 上ディスプレイをメインに設定 Run, C:\_cmd\multimonitortool-x64\MultiMonitorTool.exe /SetPrimary 1 IsUpDisplayPrimary := true } Sleep, 1000 ; 1秒待機 return}
' Excelアプリケーションを起動Set objExcel = CreateObject("Excel.Application")objExcel.Visible = True ' ドラッグ&ドロップで渡された引数(2つのファイルパス)を取得Set objArgs = WScript.Arguments ' 引数が2つであることを確認If objArgs.Count <> 2 Then MsgBox "2つのExcelファイルをドラッグ&ドロップしてください。", vbExclamation WScript.QuitEnd If ' ドラッグ&ドロップされた2つのファイルパスを取得strFi…
特定の送信者からのメールtxt保存 csvはExcel保存に変換
Sub 特定の送信者からのメールtxt保存() Dim olApp As Outlook.Application Dim olNs As Outlook.Namespace Dim olFolder As Outlook.MAPIFolder Dim senderEmail As String Dim savePath As String Dim count As Integer Dim specifiedDate As Date Dim dateInput As String Dim errorLogFile As String ' 特定の送信者のメールアドレスを設定 senderEmail…
Sub 日付指定してDataから対象を抽出2() Dim wsSearch As Worksheet Dim wsResult As Worksheet Dim searchDate As Date Dim resultArray() As Variant Dim matchCount As Long Dim folderPath1 As String Dim folderPath2 As String Dim fileName As String Dim sourceBook As Workbook Dim wsData As Worksheet Dim dataArray As Varia…
Sub S日付指定してDataから対象を抽出() Dim wsData As Worksheet Dim wsSearch As Worksheet Dim wsResult As Worksheet Dim searchDate As Date Dim dataArray As Variant Dim resultArray() As Variant Dim i As Long, resultRow As Long Dim lastRow As Long Dim matchCount As Long ' シートの設定 Set wsData = ThisWorkbook.Sheets("Dat…
Sub ExtractDataToSheet1AndCompare() Dim folderPath As String Dim wb As Workbook Dim ws1 As Worksheet Dim sampleWs As Worksheet Dim compatibilityWs As Worksheet Dim lastRow As Long Dim i As Long Dim regex As Object Dim compatLastRow As Long Dim outputRow As Long Dim sampleWorkbookPath As String Dim d…
Sub Outlookメールと添付資料を保存2() Dim olApp As Outlook.Application Dim olNs As Outlook.NameSpace Dim olSentFolder As Outlook.folder Dim olInboxFolder As Outlook.folder Dim textSaveFolder As String Dim attachmentSaveFolder As String Dim errorLog As String Dim startDate As Date Dim endDate As Date ' Outlookオブ…
Sub Outlookメールと添付資料を保存() Dim olApp As Outlook.Application Dim olNs As Outlook.NameSpace Dim olSentFolder As Outlook.folder Dim olInboxFolder As Outlook.folder Dim textSaveFolder As String Dim attachmentSaveFolder As String Dim errorLog As String Dim startDate As Date Dim endDate As Date ' Outlookオブジ…
Sub 特定の送信者からのメールtxt保存() Dim olApp As Outlook.Application Dim olNs As Outlook.Namespace Dim olFolder As Outlook.MAPIFolder Dim senderEmail As String Dim savePath As String Dim count As Integer Dim specifiedDate As Date Dim dateInput As String Dim errorLogFile As String ' 特定の送信者のメールアドレスを設定 senderEmail…
'//フォルダー内のテキストファイルを検索 Dim folderPath, fileSystem, folder, file, searchTerm, outputFilefolderPath = "Z:\Work" ' フォルダーのパスを指定outputFile = "Z:\Work\output.html" ' 出力ファイルのパスを指定 ' メッセージボックスで検索パラメータを入力searchTerm = InputBox("検索したいテキストを入力してください:", "テキスト検索") ' 入力がキャンセルされた場合は処理を終了If searchTerm = "" Then WScrip…
Outlookメールをtext保存(作成されてる受信フォルダごと)
Sub Outlookメールをtext保存2() Dim olApp As Outlook.Application Dim olNs As Outlook.NameSpace Dim olSentFolder As Outlook.folder Dim olInboxFolder As Outlook.folder Dim olFolder As Outlook.folder Dim olItem As Object Dim saveFolder As String Dim fileNum As Integer Dim sendDate As String Dim receivedDate A…
Option Explicit Sub Saoutlookの添付ファイルを保存() Dim olApp As Outlook.Application Dim olNs As Outlook.NameSpace Dim olSentFolder As Outlook.Folder Dim olInboxFolder As Outlook.Folder Dim olItem As Object Dim saveFolder As String Dim attachment As Outlook.attachment Dim filePath As String Dim dateSuffix As …
Sub Outlookメールをtext保存() Dim olApp As Outlook.Application Dim olNs As Outlook.NameSpace Dim olSentFolder As Outlook.Folder Dim olInboxFolder As Outlook.Folder Dim olItem As Object Dim saveFolder As String Dim fileNum As Integer Dim sendDate As String Dim receivedDate As String Dim i As Integer Dim at…
Sub Sa送信時間取得() Dim olApp As Outlook.Application Dim olNs As Outlook.Namespace Dim olFolder As Outlook.Folder Dim olItem As Object Dim lastSentTimes As Object Dim sendDate As Variant Dim sentTime As Date Dim filePath As String Dim fileNum As Integer Dim msg As String Set olApp = New Outlook.Applicati…
Sub TransferDataToOracleAndExportToExcel() Dim conn As Object Dim accessDb As DAO.Database Dim accessRs As DAO.Recordset Dim excelApp As Object Dim excelWorkbook As Object Dim excelWorksheet As Object Dim savePath As String Dim fileName As String Dim currentDate As String Dim accessDbPath As String …
' CommonModuleOption Explicit Public folderPath As StringPublic saveFileName As StringPublic dateTimeStamp As String Sub フォルダ内条件による抽出() Dim fileName As String Dim wb As Workbook Dim ws As Worksheet Dim newWb As Workbook Dim newWs As Worksheet Dim lastRow As Long Dim newRow As Long Dim i As Long ' フォ…
Sub フォルダ内bookシートの条件抽出() Dim folderPath As String Dim fileName As String Dim wb As Workbook Dim ws As Worksheet Dim newWb As Workbook Dim newWs As Worksheet Dim lastRow As Long Dim newRow As Long Dim i As Long Dim searchTextC As String Dim searchTextZ As String Dim targetSheet As String Dim saveFileN…
' 今日の日付を取得し、_yyyymmdd_hhmmss形式にフォーマット todayDate = Format(Now, "yyyymmdd_hhmmss")
指定フォルダのbook集計 その2(500件以上はADODB利用)
'//指定フォルダのbook集計 その2(500件以上はADODB利用) Sub 指定フォルダのbook集計2() Dim folderPath As String Dim fileName As String Dim newWb As Workbook Dim newWs As Worksheet Dim newRow As Long Dim todayDate As String Dim conn As Object Dim rs As Object Dim sql As String Dim fullPath As String Dim wb As Workbook Dim ws As …
Sub 指定フォルダのbook集計() Dim folderPath As String Dim fileName As String Dim wb As Workbook Dim ws As Worksheet Dim newWb As Workbook Dim newWs As Worksheet Dim lastRow As Long Dim newRow As Long Dim i As Long Dim dataArray As Variant Dim desktopPath As String Dim saveFileName As String Dim todayDate As …
Option Explicit ' PDFtkのパスを指定Const PDFTK_PATH = "C:\Program Files (x86)\PDFtk\bin\pdftk.exe" ' 分割対象のPDFファイルが格納されているフォルダのパスを指定Dim inputFolderinputFolder = "Z:\Work\【信頼できる場所_Excel】\分割前" ' ここを適切なフォルダのパスに変更 ' 分割後のファイルを保存するフォルダのパスを指定Dim outputFolderoutputFolder = "Z:\Work\【信頼できる場所_Excel】\分割後" ' ここを適切な出力フ…
'//-------------------- ' AccessのVBAモジュールに追加Function GetDataFromOracle(searchCondition As String) As Variant Dim conn As Object Dim rs As Object Dim data As Variant Dim i As Integer, j As Integer Dim sql As String ' ADO接続を作成 Set conn = CreateObject("ADODB.Connection") ' 接続文字列を設定(適切に変更してください) conn.Co…
Sub ExportDataFromOracle() Dim conn As Object Dim rs As Object Dim data As Variant Dim i As Integer, j As Integer Dim searchValue As String Dim sql As String ' 検索条件をExcelのセルから取得(例: A1セルの値) searchValue = ThisWorkbook.Sheets("Sheet1").Cells(1, 1).Value ' シート名とセルを指定 ' ADO接続を作成 Set conn = CreateObject("…
Sub ExportDataFromOracleViaAccess() Dim accessApp As Object Dim conn As Object Dim rs As Object Dim data As Variant Dim i As Integer, j As Integer Dim searchValue1 As String Dim searchValue2 As String Dim searchValue3 As String Dim sql As String ' 検索条件をExcelのセルから取得(例: A1, A2, A3セルの値) searchValue1 = …
'//AccessVBA----------------------------------------- Function GetDataFromOracle() As Variant Dim conn As Object Dim rs As Object Dim data As Variant Dim i As Integer, j As Integer Dim queryName As String ' ADO接続を作成 Set conn = CreateObject("ADODB.Connection") ' 接続文字列を設定(適切に変更してください) conn.ConnectionS…
Sub SaveActiveSheetAsCSV() Dim filePath As String Dim fileName As String ' デスクトップのパスを取得 filePath = CreateObject("WScript.Shell").SpecialFolders("Desktop") & "\" fileName = "ActiveSheet_" & Format(Now, "yyyy-mm-dd_hh-mm-ss") & ".csv" ' ファイル名を設定 ' アクティブシートをCSV形式で保存 With ThisWorkbook .SaveCopyAs filePa…
Sub SendMail() Dim OutlookApp As Object Dim OutlookMail As Object Dim mailBody As String Dim var1 As String Dim var2 As String Dim var3 As String Dim var4 As String Dim var5 As String Dim var6 As String ' セルC1から値を取得 var1 = Range("C1").Value var2 = Range("C2").Value var3 = Range("C3").Value var4 = Ra…
' VBS Script to convert Access Query to SQLDim accessApp, db, query, sqlQuery ' Create Access Application objectSet accessApp = CreateObject("Access.Application") ' Open the Access database (指定するデータベースのパスに変更してください)accessApp.OpenCurrentDatabase "C:\path\to\your\database.accdb" ' Get the query name (ク…
Dim excelApp, workbook, link, ws, hyperLinkDim filePath, newFilePathDim totalLinksRemoved, remainingLinks, totalHyperlinksRemoved ' ドロップされたファイルのパスを取得If WScript.Arguments.Count = 0 Then WScript.Echo "Excelファイルをドロップしてください。" WScript.QuitEnd If filePath = WScript.Arguments(0) ' 新しいファイル名を設定(例: "_no_links…
Sub ハイパーリンク先削除チェック() Dim ws As Worksheet Dim hyperlink As hyperlink Dim response As VbMsgBoxResult Dim recordSheet As Worksheet Dim lastRow As Long Dim hyperlinkFound As Boolean ' リンク記録用のシートを確認、なければ作成 On Error Resume Next Set recordSheet = ThisWorkbook.Worksheets("リンク記録") On Error GoTo 0 If recordSh…
Sub S外部リンク削除() Dim wb As Workbook Dim ws As Worksheet Dim cell As Range Dim externalLinks As String Dim filePath As String Dim fileNum As Integer Dim linkCell As Range ' アクティブなブックを取得 Set wb = ActiveWorkbook externalLinks = "" ' 各ワークシートをループ For Each ws In wb.Worksheets ' 各セルをループ For Each cell In ws.U…
このブックには、安全でない可能性のある外部ソースへのリンクが1つ以上含まれていますの表示が出るExcelを、ブック立ち上げないでリンク先を削除する方法
方法1: ZIPファイルとして解凍する ファイル拡張子を変更: .xlsxや.xlsmの拡張子を.zipに変更します。 ZIPファイルを解凍: 解凍して中のファイルを表示します。 xlフォルダを探す: 解凍したフォルダ内にあるxlフォルダを開きます。 externalLinksフォルダを確認: externalLinksフォルダに外部リンクの情報が保存されている場合があります。このフォルダを削除します。 再圧縮: フォルダを再度ZIP圧縮し、拡張子を元の形式(.xlsxや.xlsm)に戻します。 方法2: XMLエディタを使う ZIPファイルとして解凍した後、xl/workbook.xmlファ…
Sub リンク先確認() Dim ws As Worksheet Dim link As Variant Dim externalLinks As Collection Dim msg As String Dim totalLinks As Long Set externalLinks = New Collection totalLinks = 0 ' アクティブなブックのすべてのシートをループ For Each ws In ActiveWorkbook.Worksheets ' 外部リンクを取得 On Error Resume Next ' エラーを無視する(リンクがない場合など) For …
Sub リンク先削除() Dim ws As Worksheet Dim link As Variant Dim totalLinksRemoved As Long totalLinksRemoved = 0 ' アクティブなブックに対してループ For Each ws In ActiveWorkbook.Worksheets ' 外部リンクを取得 On Error Resume Next ' エラーを無視する(リンクがない場合など) For Each link In ws.LinkSources(xlExcelLinks) ' リンクされたセルの値を保持してリンクを削除 ws.Cells.R…
Dim excelApp, workbook, linkDim filePathDim totalLinksRemoved, currentFileLinksRemoved ' ドロップされたファイルのパスを取得If WScript.Arguments.Count = 0 Then WScript.Echo "Excelファイルをドロップしてください。" WScript.QuitEnd If filePath = WScript.Arguments(0) ' Excelアプリケーションを作成Set excelApp = CreateObject("Excel.Application")exce…
Sub 列飛ばし転記作業2() Dim wsS As Worksheet, wsT As Worksheet Dim lRS As Long, lRT As Long Dim i As Long, j As Long Dim fnd As Range Dim k1 As String, k2 As String Dim tCol As Long ' シートの設定 Set wsS = ThisWorkbook.Sheets("Sheet1") Set wsT = ThisWorkbook.Sheets("Sheet2") ' 最終行の取得 lRS = wsS.Cells(wsS.Rows.Cou…
Sub 重複メール削除() Dim olApp As Outlook.Application Dim olNamespace As Outlook.Namespace Dim olFolder As Outlook.Folder Dim olTargetFolder As Outlook.Folder Dim olItem As Object Dim emailDict As Object Dim emailKey As String Dim duplicates As Integer ' Outlookオブジェクトの取得 Set olApp = Outlook.Application Set…
Sub 1列飛ばし転記作業() Dim wsS As Worksheet, wsT As Worksheet Dim lRS As Long, lRT As Long Dim i As Long, j As Long Dim fnd As Range Dim k1 As String, k2 As String Dim tCol As Long ' シートの設定 Set wsS = ThisWorkbook.Sheets("Sheet1") Set wsT = ThisWorkbook.Sheets("Sheet2") ' 最終行の取得 lRS = wsS.Cells(wsS.Rows.Cou…
DAO (Data Access Objects) ライブラリ参照設定のリストから「Microsoft DAO xx.x Object Library」を探し、チェック ADODB (ActiveX Data Objects) ライブラリ参照設定のリストから「Microsoft ActiveX Data Objects x.x Library」を探し、チェック
Oracle ODBCドライバー接続のポップアップをさせないで実行
Sub ExportAccessDataToExcel() Dim db As Object Dim rs As Object Dim dbPath As String Dim query As String Dim ws As Worksheet Dim i As Integer Dim j As Integer ' Accessデータベースのパス dbPath = "D:\サンプルData.accdb" ' Accessデータベースへのクエリ query = "SELECT SomeField1, SomeField2 FROM SomeTable" ' ExcelのSheet1を指定 S…
Function GetOracleConnectionString(uid As String, pwd As String) As String On Error GoTo ErrorHandler Dim connStr As String connStr = "Provider=MSDORA.1;User ID=" & uid & ";Password=" & pwd & ";Data Source=YourDataSourceName" GetOracleConnectionString = connStr Exit Function ErrorHandler: MsgBox "Ge…
"コンパイルエラー ユーザ定義は定義されていません” の対応方法
参照設定のMicrosoft DAO 3.6 Object Libraryを外し Microsoft Office 14.0 Access database engine Object Libraryに変更
1.リンクテーブルを右クリック ⇒ 「リンクテーブルマネージャー」をクリック 2.該当のデータソース名のものをレ点チェックして、「編集」をクリック 3.接続文字列内にコードを追記DSN=***;APP=Microsoft Office;DATABASE=*****;UID=ユーザ名;PWD=パスワード;
Sub RemoveOldItemsByExecutionDate() Dim pt As PivotTable Dim pf As PivotField Dim item As PivotItem Dim executionDate As Date Dim cutoffDate As Date ' ピボットテーブルを指定します(例: シート1のピボットテーブル1) Set pt = ThisWorkbook.Sheets("Sheet1").PivotTables("PivotTable1") ' フィールドを指定します(例: 日付フィールド) Set pf = pt.PivotFields…
Sub ProtectAllSheets() Dim ws As Worksheet Dim password As String ' 保護に使用するパスワードを設定します password = "1234" ' ワークブック内のすべてのシートをループします For Each ws In ActiveWorkbook.Worksheets ws.Protect Password:=password Next ws MsgBox "すべてのシートに保護を設定しました。", vbInformationEnd Sub Sub UnprotectAllSheets() Dim ws As Worksh…
VBS スクリプトを使用して、10分間隔で30分間継続するタスクをタスクスケジューラに設定
’//cscript create_task.vbs Dim service, taskDefinition, triggers, trigger, settings, repetition ' タスクスケジューラのサービスを取得Set service = CreateObject("Schedule.Service")service.Connect ' タスクの定義を作成Set taskDefinition = service.NewTask(0) ' トリガーを作成Set triggers = taskDefinition.TriggersSet trigger = triggers.Cr…
@echo off 12:00~スタート 10分間隔 30分間schtasks /create /tn "YourTaskName" /tr "C:\path\to\your\script.bat" /sc daily /st 12:00 /ri 10 /du 00:30 /f
'//Access側のvba code Function GetOracleConnectionString(dsn As String, uid As String, pwd As String) As String Dim connStr As String connStr = "ODBC;DSN=" & dsn & ";UID=" & uid & ";PWD=" & pwd GetOracleConnectionString = connStrEnd Function '//Excel側ののvba code Sub FetchDataUsingAccessVBA() Dim access…
Excel VBAからAccessを操作してOracleに接続3
Sub FetchDataUsingAccessVBA() Dim accessApp As Object Dim dbPath As String Dim connStr As String Dim conn As Object Dim rs As Object Dim sqlStr As String Dim ws As Worksheet Dim i As Integer Dim j As Integer Dim vbaCode As String Dim dsn As String Dim uid As String Dim pwd As String On Error GoTo Er…
Excel VBAからAccessを操作してOracleに接続2
Sub FetchDataUsingAccessVBA() Dim accessApp As Object Dim dbPath As String Dim connStr As String Dim conn As Object Dim rs As Object Dim sqlStr As String Dim ws As Worksheet Dim i As Integer Dim j As Integer Dim vbaCode As String ' Accessデータベースのパス dbPath = "D:\サンプルData.accdb" ' ExcelのSheet1を指定 Set w…
Excel VBAからAccessを操作してOracleに接続
1.Access VBAでOracleに接続するコードの準備 '// Access内のVBAモジュールFunction ★GetOracleConnectionString() As String ' Oracleへの接続文字列 (DSN使用) Dim connStr As String connStr = "ODBC;DSN=YourDSN;UID=YourUsername;PWD=YourPassword" ' 接続文字列を返す GetOracleConnectionString = connStrEnd Function 2. Excel VBAからAccessを操作してOracleに接…
「Sheet1」の印刷範囲だけを別のフォルダに .xlsx ファイルとして保存する
Sub SavePrintAreaAsXlsx() Dim ws As Worksheet Dim newWorkbook As Workbook Dim savePath As String Dim printArea As Range ' 保存先のフォルダパスを指定 savePath = "C:\Your\Desired\Folder\Path\Sheet1_PrintArea.xlsx" ' 保存先のパスとファイル名を指定 ' 対象のワークシート「Sheet1」を取得 Set ws = ThisWorkbook.Sheets("Sheet1") ' 印刷範囲を取得 Set printAr…
WORKDAY関数 と DateSerial関数 利用サンプル
Sub 日付計算8日前() ’C列の日付をYYYYMMDD形式からDateSerial関数を使って日付型に変換 Dim ws As Worksheet Dim lastRow As Long Dim i As Long Dim dateStr As String Dim dateValue As Date Dim workdayDate As Date ' 対象のワークシートを設定 Set ws = ThisWorkbook.Sheets("Sheet2") ' 最終行を取得 lastRow = ws.Cells(ws.Rows.Count, "C").End(xlUp).Row ' C列の値…
Sub 複数のシートを印刷() Dim sheetNames As Variant Dim i As Integer ' 印刷したいシート名を配列にリストアップ sheetNames = Array("Sheet1", "Sheet2", "Sheet3") ' シート名を指定 ' 各シートを順番に印刷 For i = LBound(sheetNames) To UBound(sheetNames) Sheets(sheetNames(i)).PrintOut Next i ' 印刷完了のポップアップメッセージ MsgBox "すべてのシートの印刷が完了しました。", vbInformatio…
Sub GroupAndCollapseColumns() ' C列からF列までをグループ化 Columns("C:F").Select Selection.Columns.Group ' グループを閉じた状態に設定 Columns("C:F").EntireColumn.Hidden = TrueEnd Sub Sub GroupAndCollapseRows() ' 3行目から6行目までをグループ化 Rows("3:6").Select Selection.Rows.Group ' グループを閉じた状態に設定 Rows("3:6").EntireRow.Hidden = TrueEnd S…
CStr関数を使用して、クエリ内で数字を文字列に変換⇒Excel側での対応
Sub FormatAsText() ' シート名と列を指定 With Worksheets("シート名").Columns("A:A") .NumberFormat = "@" End WithEnd Sub
SELECT CStr(フィールド名) AS 文字列フィールド名 FROM テーブル名;
Sub ConnectToOracle() Dim conn As Object Dim rs As Object Dim connectionString As String Dim sqlQuery As String ' 接続文字列の設定 connectionString = "DSN=YourDSNName;UID=YourUsername;PWD=YourPassword;" ' Oracleへの接続を作成 Set conn = CreateObject("ADODB.Connection") conn.Open connectionString ' SQLクエリの実行 sqlQue…
Excel VBAで、マクロを起動した際にバージョンが違う場合に最新版を利用するよう促す
Sub CheckMacroVersion() ' 現在のマクロのバージョンを設定 Dim currentVersion As String currentVersion = "1.0.0" ' ここに現在のバージョンを設定 ' 最新バージョンを設定(通常は外部から取得する、もしくはハードコーディング) Dim latestVersion As String latestVersion = "1.1.0" ' ここに最新のバージョンを設定 ' バージョンの比較 If currentVersion < latestVersion Then MsgBox "新しいバージョンがあります!現在のバージ…
Sub KEYを作って最新のもの抽出() Dim ws1 As Worksheet, ws2 As Worksheet Dim lastRow As Long, i As Long, j As Long Dim dict As Object Dim dictKey As Variant ' ディクショナリのキーを格納する変数 ' シート1とシート2を設定 Set ws1 = ThisWorkbook.Sheets("Sheet1") Set ws2 = ThisWorkbook.Sheets("Sheet2") ' シート2を初期化 ws2.Cells.Clear ' シート1の最終行を取得 …
Sub 表示3桁() Dim ws As Worksheet Dim lastRow As Long Dim i As Long ' 対象となるシートを設定する Set ws = ThisWorkbook.Sheets("Sheet2") ' シート名を適宜変更 ' B列とD列の最終行を取得する lastRow = ws.Cells(ws.Rows.Count, "B").End(xlUp).Row ' B列の数値を三桁の文字列にフォーマットする For i = 1 To lastRow If IsNumeric(ws.Cells(i, "B").Value) Then ws.Cells(i,…
発注先毎の連番: DCount("*","クエリ2","[項目] < '" & [項目] & "' and [発注先] =" & [発注先])+1
ODBCの接続文字列でDSN(データソース名)を使用するように変更 サンプルコード
ODBCの接続文字列 : strSQL = "SELECT * FROM [ODBC;Driver={Oracle in OraClient11g_home1};" & _ "Dbq=" & serverName & "/" & serviceName & ";" & _ "Uid=" & userID & ";" & _ "Pwd=" & password & ";].YourOracleTableName" DSNを使用する場合の接続文字列: strSQL = "SELECT * FROM [ODBC;DSN=YourDSNName;].YourOracleTableName" DSNを使…
Option Explicit ' 出力フォルダをVBSスクリプトが存在する場所に指定しますDim objFSO, objArgs, objFile, fileName, newName, scriptPath, outputFolderDim Filenumber ' File System Object を作成しますSet objFSO = CreateObject("Scripting.FileSystemObject") ' スクリプトのパスを取得しますscriptPath = WScript.ScriptFullNameoutputFolder = objFSO.GetParentF…
Sub GetDataFromOracleViaAccess() Dim conn As Object Dim rs As Object Dim strConn As String Dim strSQL As String Dim ws As Worksheet Dim i As Integer ' Accessデータベースのパス Dim dbPath As String dbPath = "C:\Path\To\Your\Access\Database.accdb" ' Oracleデータベースの接続情報 Dim serverName As String Dim userID As Stri…
Sub OracleADOConnection() Dim conn As Object Dim rs As Object Dim connectionString As String Dim sql As String Dim i As Integer ' 接続文字列の設定 connectionString = "Provider=OraOLEDB.Oracle;Data Source=YourDataSource;User Id=YourUsername;Password=YourPassword;" ' SQLクエリの設定 sql = "SELECT * FROM YourTable" …
Dim connDim rsDim connectionString ' 接続文字列の設定connectionString = "Provider=OraOLEDB.Oracle;Data Source=YourDataSource;User Id=YourUsername;Password=YourPassword;" ' 接続オブジェクトの作成Set conn = CreateObject("ADODB.Connection")Set rs = CreateObject("ADODB.Recordset") ' 接続を開くconn.Open connectionString ' SQLクエ…
Declare PtrSafe Function FindWindow Lib "user32" Alias "FindWindowA" _ (ByVal lpClassName As String, ByVal lpWindowName As String) As Long Declare PtrSafe Function FindWindowEx Lib "user32" Alias "FindWindowExA" _ (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As Strin…
Sub RunAccessQueryAndLoginToOracle() Dim accessApp As Object Dim dbPath As String Dim queryName As String Dim odbcDriverName As String Dim oracleUsername As String Dim oraclePassword As String Dim hWnd As Long Dim startTime As Double ' Accessデータベースのパスとクエリ名を指定します dbPath = "C:\path\to\your\database.ac…
Sub GetDataFromAccess() Dim conn As Object Dim rs As Object Dim strConnection As String Dim strSQL As String Dim ws As Worksheet ' Create a new ADODB connection object Set conn = CreateObject("ADODB.Connection") ' Create a new ADODB recordset object Set rs = CreateObject("ADODB.Recordset") ' Connect…
Sub GetDataFromAccessUsingDSN() Dim conn As Object Dim rs As Object Dim strConnection As String Dim strSQL As String Dim ws As Worksheet ' Create a new ADODB connection object Set conn = CreateObject("ADODB.Connection") ' Create a new ADODB recordset object Set rs = CreateObject("ADODB.Recordset") '…
Sub SnapShapesToGrid() Dim shp As Shape For Each shp In ActiveSheet.Shapes With shp .Top = (.TopLeftCell.Row - 1) * Rows(1).Height .Left = (.TopLeftCell.Column - 1) * Columns(1).Width .Width = Columns(.TopLeftCell.Column).Width .Height = Rows(.TopLeftCell.Row).Height End With Next shpEnd Sub
Sub GetDataFromAccess() '「Microsoft ActiveX Data Objects 2.8 Library」をチェック Dim conn As Object Dim rs As Object Dim strConnection As String Dim strSQL As String ' Create a new ADODB connection object Set conn = CreateObject("ADODB.Connection") ' Create a new ADODB recordset object Set rs = CreateObje…
--------------------------------------------------------------------------------------------- ' ユーザー名とパスワードを設定 Dim userid As String Dim password As String userid = "ユーザー名" ' 適切なユーザー名に置き換えてください password = "パスワード" ' 適切なパスワードに置き換えてください ' コネクションをセットし、Oracle ODBCドライバ接続//Accessを開く Set cnn = New ADODB.Conn…
Excel→Outlookメール送信(メール文面をテキストボックスver.)
Sub メール文面をテキストボックスver.() Dim OutlookApp As Object Dim OutlookMail As Object Dim ws As Worksheet Dim Response As VbMsgBoxResult Dim AttachFiles As Variant Dim i As Integer Dim ToRecipients As String Dim CCRecipients As String Dim BCCRecipients As String Dim MailBody As String Dim MailBody2 As String …
Sub 選択セルをセミコロン結合() Dim rng As Range Dim cell As Range Dim result As String ' アクティブセル範囲を取得 Set rng = Selection ' セルの内容を結合 For Each cell In rng If cell.Value <> "" Then If result = "" Then result = cell.Value Else result = result & ";" & cell.Value End If End If Next cell ' B1セルに出力 Range("B1").Value =…
Sub ファイル選択() ' シート"Sheet1"をアクティブにしてA1セルを選択 Sheets("Sheet1").Select Range("A1").Select ' ファイル選択ダイアログを表示してユーザーにファイルを選ばせる Dim filePath As String filePath = Application.GetOpenFilename("All Files (*.*), *.*," & _ "Excel Files (*.xls; *.xlsx), *.xls; *.xlsx," & _ "CSV Files (*.csv), *.csv," & _ "Text Fil…
「ブログリーダー」を活用して、ヒコさんをフォローしませんか?