Results 1 to 4 of 4

Thread: VB Script to format excel sheet

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2024
    Posts
    2

    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.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,689

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,689

    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
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    New Member
    Join Date
    Sep 2024
    Posts
    2

    Re: VB Script to format excel sheet

    Quote Originally Posted by jmcilhinney View Post
    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
  •  



Click Here to Expand Forum to Full Width