[Excel VBA] Macro to Open a CSV File When a Cell is Selected | Linking Column G Selection to Filenames in Column E

目次

Overview

When working in Excel, there are times when you want to automatically open a related file just by selecting a specific cell.

In this article, I will introduce a VBA macro that “automatically opens a corresponding CSV file when you place your cursor on cells G2 to G100.”

  • CSV files are saved in a specified folder.
  • Column E contains the file name (without extension).
  • Rows where the cell in Column G contains “Open” act as the trigger.

With this specification, you can implement a convenient macro that allows you to open CSV files immediately using only mouse operations.

Specifications

  1. Trigger: Operates when a cell in the range G2 to G100 is selected.
  2. File Lookup: Searches for a CSV file based on the file name listed in Column E of the same row.
  3. Confirmation: Displays a message box to confirm before opening the CSV file.
  4. Error Handling: Displays a warning if the file does not exist.

Prerequisites

  • Prepare a folder where CSV files are saved.
  • Enter the “File Name (without extension)” in Column E of Excel.
  • Enter the text “Open” in any row of Column G (This text serves as the trigger).

Full VBA Code (Worksheet Module)

Paste the following code into the specific Worksheet Module (e.g., Sheet1) in the VBA Editor.

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    ' Check if a specific cell in Column G is selected
    If Not Intersect(Target, Me.Range("G2:G100")) Is Nothing Then
        ' Check if the cell value is "Open"
        If Target.Value = "Open" Then
            Dim code As String
            code = Me.Cells(Target.Row, "E").Value
            
            ' Set the folder path where CSV files are stored
            Dim csvFolderPath As String
            ' Note: Don't forget the backslash at the end
            csvFolderPath = "C:\Users\YourName\Documents\CSV_Folder\" ' <- Change as needed
            
            Dim csvFilePath As String
            csvFilePath = csvFolderPath & code & ".csv"
            
            ' Check if the file exists
            If Len(Dir(csvFilePath)) > 0 Then
                Dim MsgResponse As VbMsgBoxResult
                MsgResponse = MsgBox("Do you want to open " & code & ".csv?", vbQuestion + vbYesNo, "Confirmation")
                
                If MsgResponse = vbYes Then
                    Workbooks.Open Filename:=csvFilePath
                End If
            Else
                MsgBox code & ".csv not found.", vbExclamation
            End If
        End If
    End If
End Sub

Important Points

  • Folder Path: Do not forget to include the backslash (\) at the end of the csvFolderPath.
  • Absolute Path: It is recommended to specify the full (absolute) path rather than a relative path.
  • File Extension: Since the code adds .csv automatically (& ".csv"), please enter only the file name without the extension in Column E.

Summary

  • You can open the corresponding CSV file simply by selecting Column G.
  • The process is safe: Check file existence -> Display message -> User confirmation -> Open.
  • This is an excellent way to use VBA for business efficiency and labor-saving.

Why not automate the repetitive task of “searching for files” and “opening them” into a single click?

よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!

この記事を書いた人

私が勉強したこと、実践したこと、してることを書いているブログです。
主に資産運用について書いていたのですが、
最近はプログラミングに興味があるので、今はそればっかりです。

目次