adding end array

Public Schedule Face-to-Face & Online Instructor-Led Training - View dates & book

Forum home » Delegate support and help forum » Microsoft Excel VBA Training and help » Adding to the end of an array

Adding to the end of an array

resolvedResolved · Medium Priority · Version 2016

Diane has attended:
Excel VBA Introduction course
Excel VBA Intermediate course
Excel VBA Advanced course

Adding to the end of an array

I should know this, but mind has gone a blank. I want to put the content of 4 sheets into one array. Writing the first one is no problem, but how do I write to the end of the array for the next 3 please. Here is the context I am using it in

Thanks, Diane

Sub RunConsolidateNames()
For bCounter = 1 To 4
Select Case bCounter
Case Is = 1
Sheets(6).Activate
iGetLAstRow = Cells(Rows.Count, 1).End(xlUp).row
Case Is = 2
Sheets(7).Activate
iGetLAstRow = Cells(Rows.Count, 1).End(xlUp).row
Case Is = 3
Sheets(8).Activate
iGetLAstRow = Cells(Rows.Count, 1).End(xlUp).row
Case Is = 4
Sheets(9).Activate
iGetLAstRow = Cells(Rows.Count, 1).End(xlUp).row
End Select
iGetLAstRow = Cells(Rows.Count, 1).End(xlUp).row
sRange = "a2" & ":" & "a" & iGetLAstRow
NamesArray = Range(sRange)
Next bCounter
End Sub

RE: Adding to the end of an array

Hi Diane,

I hope you are fine.

You will need a different approach to do what you want.

I am not sure this is the most efficient solution, but it is the only one came to my mind. I will come back to you, if I come up with another solution.

Sub RunConsolidateNames()

Dim Sheet6Array as variant
Dim Sheet7Array as variant
Dim Sheet8Array as variant
Dim Sheet9Array as variant
Dim AllArray as Variant
Dim longCount as Long

Sheets(6).Select
Sheet6Array=range("A2",range("a2").end(xlDown))
Sheets(7).Select
Sheet7Array=range("A2",range("a2").end(xlDown))
Sheets(8).select
Sheet8Array=range("A2",range("a2").end(xlDown))
Sheets(9).Select
Sheet9Array=range("A2",range("a2").end(xlDown))

Redim AllArray(1 to ubound(Sheet6Array,1)+1 to ubound(Sheet7Array,1)+1 to ubound(Sheet8Array,1)+1 to ubound(Sheet9Array,1),1)

For longCount=1 to ubound(Sheet6Array,1)
AllArray(longCount,1)= Sheet6Array(longCount,1)
Next longCount

For longCount=1 to ubound(Sheet7Array,1)
AllArray(longCount+ubound(Sheet6Array,1),1)= Sheet7Array(longCount,1)
Next longCount

For longCount=1 to ubound(Sheet8Array,1)
AllArray(longCount+ubound(Sheet6Array,1)+ubound(Sheet7Array,1),1)= Sheet8Array(longCount,1)
Next longCount

For longCount=1 to ubound(Sheet9Array,1)
AllArray(longCount+ubound(Sheet6Array,1)+ubound(Sheet7Array,1)+ubound(Sheet8Array,1),1)= Sheet9Array(longCount,1)
Next longCount

End Sub



Please let me know if you get an error.



Kind regards

Jens Bonde
Microsoft Office Specialist Trainer

Tel: 0207 987 3777
STL - https://www.stl-training.co.uk
98%+ recommend us

London's leader with UK wide delivery in Microsoft Office training and management training to global brands, FTSE 100, SME's and the public sector

RE: Adding to the end of an array

Thanks Jens. I will implement that.

 

Training courses

 

Training information:

Welcome. Please choose your application (eg. Excel) and then post your question.

Our Microsoft Qualified trainers will then respond within 24 hours (working days).

Frequently Asked Questions
What does 'Resolved' mean?

Any suggestions, questions or comments? Please post in the Improve the forum thread.


 

Excel tip:

Freeze Rows and Columns to keep lables displayed

You can freeze rows and columns in your worksheet so they don't move.

This allows you to keep row and column labels displayed on your screen as you move through a large worksheet.

Click below and/or to the right of the cell(s) you want to freeze. (NB. Excel freezes ALL the rows above and ALL the columns to left of the selected cell)

Click on the 'Windows' menu and selct 'Freeze Panes'.

Lines appear in your worksheet. The required rows and columns are frozen and remain on your screen as you move through your worksheet.

To unfreeze rows and columns, click on 'Window' menu and select 'Unfreeze Panes'.

View all Excel hints and tips


Server loaded in 0.09 secs.