Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8630599
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T09:05:46+00:00 2026-06-12T09:05:46+00:00

I am getting this error while running this vba code. its kind of syntax

  • 0

I am getting this error while running this vba code. its kind of syntax error. Please help me with that. here is the script.
What script doing is , its copying data from one column to another and selecting the copied column.

Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = True

Set objWorkbook = objExcel.Workbooks.Open("C:\Users\pc\Report\report_20120912.xls")
Set ws = objWorkbook.Worksheets("Data")
Set SelRange = objExcel.Selection
ws.Range("F2:F1000").AdvancedFilter 2,,ws.Range("M2"),True

ws.Range("N1") = ws.Range("A1")
ws.Range("O1") = ws.Range("B1")


//Below line has the bug

ws.Range("M2").Select             
selection=objExcel.Selection
col1=selection.End(xlDown)         //showing error on this line      
ws.Range(selection,col1).Select
n = ws.Range.Count
ws.Range("S2").Select
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T09:05:48+00:00Added an answer on June 12, 2026 at 9:05 am

    I see that you have tagged the question as Excel/Excel-VBA

    If you are doing this in VBA Excel then you do not need to use CreateObject to create another instance of Excel. You can work with the current instance as well

    See this code

    Sub Sample()
        Dim objWorkbook As Workbook, ws As Worksheet
    
        Set objWorkbook = Workbooks.Open("C:\Users\pc\Report\report_20120912.xls")
        Set ws = objWorkbook.Worksheets("Data")
    
        With ws
            '
            '~~> Rest of the code
            '
        End With
    End Sub
    

    As mentioned in my comment, throw the Selection / Select out of your code. It not only slows down your code but can also give you runtime errors. See this link. I have commented the code below so that you will not have any problem understanding it. 🙂

    Your above code can also be written as (I have not tested the code)

    Option Explicit
    
    Sub Sample()
        Dim objWorkbook As Workbook, ws As Worksheet
        Dim lRow As Long, n As Long
        Dim mRange As Range
    
        '~~> Open the workbook
        Set objWorkbook = Workbooks.Open("C:\Users\pc\Report\report_20120912.xls")
        '~~> Set the sheet that you want to work with
        Set ws = objWorkbook.Worksheets("Data")
    
        With ws
            .Range("F2:F1000").AdvancedFilter 2, , .Range("M2"), True
    
            .Range("N1").Value = .Range("A1").Value
            .Range("O1").Value = .Range("B1").Value
    
            '~~> Find the last row
            lRow = .Range("M" & .Rows.Count).End(xlUp).Row
    
            '~~> Set your range
            Set mRange = .Range("M2:M" & lRow)
    
            n = mRange.Count
        End With
    End Sub
    

    If you notice that you actually do not need to use .Select or work with the Selection

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I keep getting this error while trying to modify some tables. Here's my code:
I keep getting this error: Uncaught Syntax Error: Unexpected Token ILLEGAL while running Ext
I am getting following error while running local script/server of my Rails project: This
I am getting this error while running my code: Error using ==> mldivide Matrix
I am getting this error While running this LoadError: Expected /home/user/Desktop/Tripurari/myapp/app/models/host.rb to define Host##
I am getting this weird error while running cucumber test: ERROR Mongo::OperationFailure: Database command
I am getting this error while i am trying to upload a database that
I'm getting this error while running the login application (Using GWT-RPC). How can I
I recently started getting this error while running gems or bundler. The only thing
I am getting this error while running a hadoop pipes program. The program compiles

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.