pctechguide.com

  • Home
  • Guides
  • Tutorials
  • Articles
  • Reviews
  • Glossary
  • Contact

VBA Macro that Makes an Array with Data from Column A

Arrays are a very important aspect of many programs. However, they can be a pain to create if you have a huge list of items that you want to make an array with.

I created a VBA macro that makes an array out of a list of items shown in column A and puts the array code in Cell B1. Here is the code:

Sub CreateArrayCode()
Dim lastRow As Long
Dim i As Long
Dim arrayCode As String

' Find the last row with data in Column A
lastRow = Cells(Rows.Count, 1).End(xlUp).Row

' Start constructing the array code
arrayCode = "Dim boyNames() As Variant" & vbCrLf & "boyNames = Array("

' Loop through each cell in Column A and add the value to the array code
For i = 1 To lastRow
    If i = lastRow Then
        arrayCode = arrayCode & """" & Cells(i, 1).Value & """"
    Else
        arrayCode = arrayCode & """" & Cells(i, 1).Value & """, "
    End If
Next i

' Close the array code
arrayCode = arrayCode & ")"

' Output the array code to the Immediate Window (Ctrl + G to view)
Debug.Print arrayCode

' Optionally, write the array code to a new worksheet
Sheets.Add After:=Sheets(Sheets.Count)
ActiveSheet.Name = "ArrayCode"
Cells(1, 1).Value = arrayCode

End Sub

Here are the steps that you will need to take to run this code:

  1. You will need to open a new Excel document.
  2. You will need to either press the Alt + F11 or click on the Developer Tab and then click “Visual Basic” to open the VBA editor.
  3. You will need to click the “Insert” field in the copy menu.
  4. Then you will need to click “Module” to insert a new module.
  5. Once you have created a new module, you will need to copy and paste the code that we showed above into the module.
  6. You will need to either click “Run” or press the F5 key to run the macro.

Of course, these are just the steps that you will need to take to actually run the macro itself. You will need to create a list of elements that are stored in Column A. It is important to make sure that every element of your list is going to be in a separate cell in Column A. Otherwise, you will accidentally combine elements in a single part of your array.

You also need to make sure that you don’t have any important code in Cell B1. If you have any other data in your spreadsheet, it is a good idea to move anything in Column B into another column. Alternatively, you can just adjust the code to put the array code in a cell in another column that is not getting used. However, it is probably a lot easier to just move the contents of Column B to another column.

For this code, I added the following 15 boy names to the list, which are all in column A as shown:

When you click “Run”, the array is created in the first cell of Column B as shown:

We hope that this code helps you if you need to create large arrays for future VBA macros! Take care!

Filed Under: Articles

Latest Articles

The Flexible PDF is King of Sharing

PDF stands for Portable Document Format, a file format that originated in print publishing as a tool for proofing print jobs before sending them to the printer. Today, it has become the standard for sharing print-ready documents via the Internet. The format was developed by Adobe and was … [Read More...]

Eliminating Common WordPress Vulnerabilities to DDoS Attacks

Distributed Denial of Service (DDoS) attacks are serious threats that every website manager must be prepared for. Unfortunately, some of the most widely publicized DDoS attacks have created a number of misconceptions about the risks associated with them. In 2012, hactivist group Anonymous … [Read More...]

Earning Effortlessly With Passive Income Apps: Reality or Scam?

The internet is full of everything – both good and bad. Therefore, it’s hardly surprising that people are cautious and take every promising idea with a grain of salt at first. Passive income applications are often viewed as especially suspicious since a lot of people believe earning more with little … [Read More...]

Gaming Laptop Security Guide: Protecting Your High-End Hardware Investment in 2025

Since Jacob took over PC Tech Guide, we’ve looked at how tech intersects with personal well-being and digital safety. Gaming laptops are now … [Read More...]

20 Cool Creative Commons Photographs About the Future of AI

AI technology is starting to have a huge impact on our lives. The market value for AI is estimated to have been worth $279.22 billion in 2024 and it … [Read More...]

13 Impressive Stats on the Future of AI

AI technology is starting to become much more important in our everyday lives. Many businesses are using it as well. While he has created a lot of … [Read More...]

Graphic Designers on Reddit Share their Views of AI

There are clearly a lot of positive things about AI. However, it is not a good thing for everyone. One of the things that many people are worried … [Read More...]

Redditors Talk About the Impact of AI on Freelance Writers

AI technology has had a huge impact on our lives. A 2023 survey by Pew Research found that 56% of people use AI at least once a day or once a week. … [Read More...]

11 Most Popular Books on Perl Programming

Perl is not the most popular programming language. It has only one million users, compared to 12 million that use Python. However, it has a lot of … [Read More...]

Guides

  • Computer Communications
  • Mobile Computing
  • PC Components
  • PC Data Storage
  • PC Input-Output
  • PC Multimedia
  • Processors (CPUs)

Recent Posts

Xerox 3635MFPX Phaser Multifunction Black and White Laser Printer

The Xerox 3635MFPX Phaser Multifunction Black and White Laser Printer is an all in one printer that is perfect for just about any business level. … [Read More...]

Object Oriented Programming 6

Now that we know what a class is, and how to derive classes using inheritance, we'll take a quick look at the slightly thorny issue of multiple … [Read More...]

Converting VCR Video to DVD Using Canopus ADVC-55

The specific product we're going to use in this tutorial is effectively a combination of Canopus's high-quality ADVC-55 analogue-to-DV converter … [Read More...]

[footer_backtotop]

Copyright © 2025 About | Privacy | Contact Information | Wrtie For Us | Disclaimer | Copyright License | Authors