-
Sep 23rd, 2024, 10:58 PM
#1
Thread Starter
New Member
VB Script to format excel sheet
Hi All,
I am new to VBScript and need to format an Excel sheet using it. I found the following code online, but when I try to execute it, I encounter the error:
runmacro.vbs(17, 39) Microsoft VBScript compilation error: Expected statement
I am attempting to add a new cell at G1 and shift all the cells to the right with the code below. Could you please help me resolve this issue? If any additional details are needed, please let me know. Thanks in advance.
Code:
' VBScript to insert a cell and shift cells to the right
Dim objExcel, objWorkbook, objWorksheet
Dim strFilePath
' Get the Excel file path from the command line argument
strFilePath = WScript.Arguments(0)
' Create an Excel application object
Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = False
' Open the Excel file
Set objWorkbook = objExcel.Workbooks.Open(strFilePath)
Set objWorksheet = objWorkbook.Sheets(1) ' Modify if you need a different sheet
' Insert a cell at G1 and shift cells to the right
objWorksheet.Range("G1").Insert Shift:=1 ' xlToRight
' Save and close the workbook
objWorkbook.Save
objWorkbook.Close
' Quit Excel application
objExcel.Quit
' Clean up
Set objWorksheet = Nothing
Set objWorkbook = Nothing
Set objExcel = Nothing
Thanks
Last edited by jmcilhinney; Sep 24th, 2024 at 12:03 AM.
-
Sep 24th, 2024, 12:02 AM
#2
Re: VB Script to format excel sheet
Thanks for using a text colour that can barely be seen. I have reformatted your post for readability.
-
Sep 24th, 2024, 12:04 AM
#3
Re: VB Script to format excel sheet
Which line of code is the error message referring to? Is it this:
Code:
objWorksheet.Range("G1").Insert Shift:=1 ' xlToRight
should that actually be this:
Code:
objWorksheet.Range("G1").Insert(Shift:=1) ' xlToRight
-
Sep 24th, 2024, 07:47 AM
#4
Thread Starter
New Member
Re: VB Script to format excel sheet
Originally Posted by jmcilhinney
Which line of code is the error message referring to? Is it this:
Code:
objWorksheet.Range("G1").Insert Shift:=1 ' xlToRight
should that actually be this:
Code:
objWorksheet.Range("G1").Insert(Shift:=1) ' xlToRight
Thank you very much for doing the needful.
After replacing the code, I am facing below error. Could you please advise me on this?
(17, 38) Microsoft VBScript compilation error: Expected ')'
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|