VBA & Macro Problems

Copy a Cell Value to the Correct Workbook with Excel VBA

Bushra ParveenBushra Parveen Nov 18, 2026 870 views

Question details

Modify a worksheet selection event to copy an item code to the workbook associated with a production line.

Copy a Cell Value to the Correct Workbook with Excel VBA
Product
Microsoft Excel
Device & OS
not provided
Scenario
A user selects an item code on a worksheet, and the production-line value in column J determines the destination workbook.
Observed behavior
The selected item code should be written to the workbook mapped to the production line on the same row.
Before you start

Identify the item-code range, destination workbook paths, destination sheet names, and target cells. The example below assumes item codes are selected in B2:B1000 and writes each code to Sheet1!B2; change these references to match your files.

Solution 1Recommended

Map production lines to workbooks in the worksheet event

Use Select Case to choose a workbook from the production-line value in column J, then write the selected item code to it.

This example reuses a workbook already open at the mapped path or opens it if needed. It leaves the destination workbook open and does not save it automatically.

1
Open the worksheet code module

In Excel, right-click the source worksheet tab and select View Code. Place the event procedure in that worksheet's code module, not in a standard module.

2
Add the selection event

Paste the following code, replacing an existing Worksheet_SelectionChange procedure if necessary: Private Sub Worksheet_SelectionChange(ByVal Target As Range) Dim lineName As String Dim filePath As String Dim destWb As Workbook Dim wb As Workbook Dim eventsWereEnabled As Boolean If Target.CountLarge <> 1 Then Exit Sub If Intersect(Target, Me.Range("B2:B1000")) Is Nothing Then Exit Sub If IsError(Target.Value) Or IsError(Me.Cells(Target.Row, "J").Value) Then Exit Sub If Len(Trim$(CStr(Target.Value))) = 0 Then Exit Sub lineName = Trim$(CStr(Me.Cells(Target.Row, "J").Value)) Select Case UCase$(lineName) Case "LINE 1" filePath = "C:\Production\Line1.xlsx" Case "LINE 2" filePath = "C:\Production\Line2.xlsx" Case Else MsgBox "No workbook is mapped to production line: " & lineName, vbInformation Exit Sub End Select eventsWereEnabled = Application.EnableEvents On Error GoTo HandleError Application.EnableEvents = False For Each wb In Application.Workbooks If StrComp(wb.FullName, filePath, vbTextCompare) = 0 Then Set destWb = wb Exit For End If Next wb If destWb Is Nothing Then Set destWb = Application.Workbooks.Open(filePath) destWb.Worksheets("Sheet1").Range("B2").Value = Target.Value CleanUp: Application.EnableEvents = eventsWereEnabled Exit Sub HandleError: MsgBox "Could not copy the item code: " & Err.Description, vbExclamation Resume CleanUp End Sub

3
Set the mappings and destination

Edit the Case labels and filePath values to match your production lines and workbook paths. Change B2:B1000, Sheet1, and B2 to the intended source range, destination worksheet, and destination cell.

4
Save and test

Save the source file as an Excel Macro-Enabled Workbook (.xlsm). Enable macros if prompted, then select an item-code cell in the configured range and check the mapped destination workbook. Save the destination workbook when you are ready to keep the change.

Map production lines to workbooks in the worksheet event
The destination cell is overwritten: Every qualifying selection writes to the same destination cell in the mapped workbook. The event runs when the selection changes, not when a cell value is edited.
Free Microsoft Office alternative

Work with production spreadsheets in WPS Office

WPS Office is a free, lightweight Office alternative with a familiar spreadsheet interface and support for Excel formats, making it easy to move everyday workbook tasks. VBA support can vary by version and environment, so check compatibility before relying on this Excel event macro.

Edit production-line lists and item-code tables in WPS SpreadsheetsOpen and edit Microsoft Excel formats such as XLSXUse a familiar interface for everyday spreadsheet workMove existing Office documents into a lightweight, free suite
microsoft office alternative - wps office

Frequently Asked Questions

Where should I put the Worksheet_SelectionChange code?

Right-click the source worksheet tab in Excel, select View Code, and place the procedure in that worksheet's code module. A standard module will not automatically handle the worksheet selection event.

How does the macro choose the destination workbook?

It reads column J on the selected cell's row and compares that value with the labels in Select Case. Each matching Case supplies a workbook path; an unmapped line displays a message instead of writing a value.

Will the macro save or close the destination workbook?

No. The example leaves the destination workbook open without saving it automatically. Use File > Save in the destination workbook to keep the copied value.

Why doesn't the macro run when I edit an item code?

Worksheet_SelectionChange runs when the selection changes. If you need to respond to edits instead, use a Worksheet_Change event and adapt its range checks and logic.

Can I map more than two production lines?

Yes. Add another Case label and its filePath assignment inside Select Case for each additional line. Make sure the line labels match the values used in column J.