We help IT Professionals succeed at work.
Get Started

VBA - Getting Data from an Excel Worksheet and Creating a Collection of Custom (Book) objects

267 Views
Last Modified: 2017-10-29
In a simple VBA project for Word I have Class Module named Book which has just two properties - Title and Author - both strings.
I would like to be pull data from Sheet1 of a simple Excel Workbook to create a Collection of Books items ( named myBooks). Every book to be added to this Collection  (myBooks) will be created using a record from Sheet1 of the Excel Workbook, such that the value in Column 1 of each record will be the title of the Book and the value in Column 2 of each record will be the Author of the Book. I include a screen shot of the Sheet1 of my Excel Workbook.
Thank you for your help.

Screenshot of the Sheet1 of the Excel Workbook
Dim objExcel As New Excel.Application
Dim exWb As Excel.Workbook

Dim bookTitle As String
Dim bookAutor As String


Dim myBooks As Collection
Set myBooks = New Collection


Set exWb = objExcel.Workbooks.Open("G:\MyFolder\BookShelf.xlsx")


Set excelWorkBook = objExcel.Workbooks.Open("G:\Dr Ahmad\Handover\QuickWord.xlsx")

' I need code in this region which could run some kind of Loop on excelWorkBook  and give me a collection of book objects
' creating a book from all the records in Sheet1 of excelWorkBook such that the value in column 1 of each row will be the title of 
'  the book and the value in column 2 of each row will be the author of the book. 


exWb.Close

Set exWb = Nothing

Open in new window

Comment
Watch Question
Keep everyone healthy; Wear a mask - Social distance - Don't touch your face - Wash your hands for 20 seconds
CERTIFIED EXPERT
Most Valuable Expert 2017
Distinguished Expert 2020
Commented:
This problem has been solved!
Unlock 1 Answer and 21 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE