Store Table Column in Array

MM91

Board Regular
Joined
Nov 29, 2021
Messages
59
Office Version
  1. 365
Platform
  1. Windows
Hi I am trying to store a table column in an array for use in other module. I have the code that seems like it runs and works fine. But the array returns nothing in the messagebox. I get no errors but am not sure what I am doing wrong. Please help! thanks!

DATA TABLE.PNG



Dim StandardPartCodesArray() As String

Public Sub LoadApp()

'Set Database Workbook File Location
Dim strFilename As String: strFilename = "C:\Users\mattr\Desktop\Standard Parts Macro Test.xlsm"

'Open New Excel Instance
Dim XLapp As New Excel.Application
XLapp.Visible = False

'Open WorkBook
Dim SPwb As Excel.Workbook
Set SPwb = XLapp.Workbooks.Open(Filename:=strFilename, UpdateLinks:=3)
SPwb.Activate



'Activate Worksheet
Dim SPws As Excel.Worksheet
Set SPws = SPwb.Worksheets("Standard Parts")
SPws.Activate




'Set Table Object
Dim SPTbl As ListObject
Set SPTbl = SPws.ListObjects("StandardPartsTable")


'Store Data
Dim LastRowSPTable As Long
Dim i As Long

LastRowSPTable = SPTbl.DataBodyRange.Rows.Count

ReDim StandardPartCodesArray(1 To LastRowSPTable)

For i = 1 To TotalRows
StandardPartCodesArray(i) = SPTbl.DataBodyRange(i, 1).Value
Next

Dim messagetest As String
messagetest = StandardPartCodesArray(1)
'Msgbox Join(StandardPartCodesArray, vbCrLf)
Msgbox messagetest

SPwb.Close SaveChanges:=False
XLapp.Quit
Set XLapp = Nothing







End Sub
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
I don't see the variable TotalRows declared or given a value anywhere in your code, so at best it's value is zero. Try this instead:

VBA Code:
LastRowSPTable = SPTbl.DataBodyRange.Rows.Count
  
   ReDim StandardPartCodesArray(1 To LastRowSPTable)

       For i = 1 To LastRowSPTable
           StandardPartCodesArray(i) = SPTbl.DataBodyRange(i, 1).Value
       Next
Also, you didn't say which column of the table you wanted to place in the array. If its the description column then modify the line immediately above the Next s by changing this:
StandardPartCodesArray(i) = SPTbl.DataBodyRange(i, 1).Value
to this:
StandardPartCodesArray(i) = SPTbl.DataBodyRange(i, 2).Value
 
Last edited:
Upvote 0
Solution
Wow thank you so much I thought I had gone over every variable name a hundred times thank you!!
 
Upvote 0

Forum statistics

Threads
1,223,214
Messages
6,170,774
Members
452,353
Latest member
strainu

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