Getting a range data of a column from Excel 2016 through VBA

LeilaMansouri

New Member
Joined
Jul 20, 2017
Messages
2
Hi,

I am trying to get a range of data from a column of my Excel sheet on VBA, and then process each cell of the column one by one.

By there is a run time error "Subscript out of range" while getting the ListObject.

Could you please let me know what this wrong with the code and who can I resolve it?

Code:
Dim sourceWS As WorksheetDim databaseTable As ListObject
Dim pinNumberColumn As Range
    
Set sourceWS = Sheets("Hello")
Set databaseTable = sourceWS.ListObjects(1)

Set pinNumberColumn = databaseTable.ListColumns("Pin-Nr.").DataBodyRange
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Hello Leila,

I use the following code for looping through the cells in column A. It puts "Hello" in the used range.

Code:
Dim lr As Long
lr = Cells(Rows.Count, 1).End(xlUp).Row 
For i = lr To 1 Step -1
cells(I,1).Value = "hello" 'adjust the range to your needs. 
Next

Hope this helps. :)
 
Upvote 0
Hello Leila,

I use the following code for looping through the cells in column A. It puts "Hello" in the used range.

Code:
Dim lr As Long
lr = Cells(Rows.Count, 1).End(xlUp).Row 
For i = lr To 1 Step -1
cells(I,1).Value = "hello" 'adjust the range to your needs. 
Next

Hope this helps. :)

Hi [FONT=&quot]MrJoosten,

[/FONT]
Thanks for the response. "hello" is the name of excel sheet used to read data from. Also I would like to read data of a column by the name of its header. (here "Pin-Nr.")

How could I specify which excel sheet it should read from? and how to read a range of column data just by the header name?

Thanks.
 
Upvote 0

Forum statistics

Threads
1,224,814
Messages
6,181,121
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top