I need a VB Script to copy values from Excel1 - sheet1 - column 1 to Excel2 - sheet2 - column 2
Loading
I need a VB Script to copy values from Excel1 - sheet1 - column 1 to Excel2 - sheet2 - column 2
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Uday DodiyaRank #239 · 10.3K reputationPosted Jun 21, 2024, 7:24 AM EDT
Hello Nidhi Kowalli,
To create a VBScript that copies values from
Excel1(Sheet1, Column 1) toExcel2(Sheet2, Column 2), you can follow the steps below. This script uses theExcel.Applicationobject to automate Excel operations.Here is a sample VBScript:
Option Explicit ' Define constants for the Excel file paths Const Excel1Path = "C:\Path\To\Excel1.xlsx" Const Excel2Path = "C:\Path\To\Excel2.xlsx" ' Create Excel application object Dim objExcel Set objExcel = CreateObject("Excel.Application") ' Open Excel1 and Excel2 Dim wb1, wb2 Set wb1 = objExcel.Workbooks.Open(Excel1Path) Set wb2 = objExcel.Workbooks.Open(Excel2Path) ' Get the relevant worksheets Dim ws1, ws2 Set ws1 = wb1.Sheets("Sheet1") Set ws2 = wb2.Sheets("Sheet2") ' Get the range in Column 1 of Sheet1 Dim range1 Set range1 = ws1.Columns(1).SpecialCells(2) ' 2 stands for xlCellTypeConstants, which means non-empty cells ' Copy values from Column 1 of Sheet1 to Column 2 of Sheet2 Dim cell, targetCell For Each cell In range1 Set targetCell = ws2.Cells(cell.Row, 2) targetCell.Value = cell.Value Next ' Save and close the workbooks wb2.Save wb1.Close False wb2.Close False ' Quit Excel application objExcel.Quit ' Clean up Set range1 = Nothing Set ws1 = Nothing Set ws2 = Nothing Set wb1 = Nothing Set wb2 = Nothing Set objExcel = Nothing WScript.Echo "Values copied successfully from Excel1 - Sheet1 - Column 1 to Excel2 - Sheet2 - Column 2."Explanation:
Excel1andExcel2.CreateObject("Excel.Application").Sheet1ofExcel1andSheet2ofExcel2.Sheet1.Sheet2.Excel2and close both workbooks.Make sure to replace
C:\Path\To\Excel1.xlsxandC:\Path\To\Excel2.xlsxwith the actual file paths of your Excel files. Save this script with a.vbsextension and run it. Ensure that Excel is installed on your system and the paths to the files are correct.