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

  • Home
  • SEARCH
  • 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 8470957
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T16:39:45+00:00 2026-06-10T16:39:45+00:00

I am reading some cells of excel using VBA. Function getData(currentWorksheet as Worksheet, dataStartRow

  • 0

I am reading some cells of excel using VBA.

Function getData(currentWorksheet as Worksheet, dataStartRow as Integer, _
dataEndRow as Integer, DataStartCol as Integer, dataEndCol as Integer)

    Dim dataTable as Range
    dataTable = currentWorksheet.Range(currentWorksheet.Cells(dataStartRow, _
    dataStartCol), currentWorksheet.Cells(dataEndRow, dataEndCol))

    getData = dataTable

EndFunction

It throws an error, object variable or with block variable not set. How take this range in a variable? Please guide me.

  • 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-10T16:39:46+00:00Added an answer on June 10, 2026 at 4:39 pm

    When you use a Range object, you cannot simply use the following syntax:

    Dim myRange as Range
    myRange = Range("A1")  
    

    You must use the set keyword to assign Range objects:

    Function getData(currentWorksheet As Worksheet, dataStartRow As Integer, dataEndRow As Integer, DataStartCol As Integer, dataEndCol As Integer)
    
        Dim dataTable As Range
        Set dataTable = currentWorksheet.Range(currentWorksheet.Cells(dataStartRow, DataStartCol), currentWorksheet.Cells(dataEndRow, dataEndCol))
    
        Set getData = dataTable
    
    End Function
    
    Sub main()
        Dim test As Range
    
        Set test = getData(ActiveSheet, 1, 3, 2, 5)
        test.select
    
    End Sub
    

    Note that every time a range is declared I use the Set keyword.


    You can also allow your getData function to return a Range object instead of a Variant although this is unrelated to the problem you are having.

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

Sidebar

Related Questions

I am reading Excel worksheet data using C# and Microsoft.Office.Interop. The sheet contains some
I'm reading a date from an Excel cell in Python (using .Value on the
I am reading the Excel sheet in c# using interop services. My excel sheet
I'm having some trouble reading from an Excel spreadsheet in C#. I have this
I've been reading that some devs/dbas recommend using transactions in all database calls, even
I have a VSTO addin and I'm reading data from an Excel worksheet. It
Reading a raw 3d (256x256x256 cells) texture (8bit) using the following: f = fopen(file,
I'm reading a csv file, using DictReader(). The function returns a dictionary, where the
I have an Excel spreadsheet that I'm reading in that contains some £ signs.
I have an excel sheet with some text in numerous cells and need to

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.