Excel Guides

Making AutoComplete Work for an Entire Column in Excel

When you have a lot of data in an Excel spreadsheet, it can be difficult to find the information you need. The AutoComplete feature can help by suggesting possible entries as you type in a cell. But what if you want to use AutoComplete for an entire column?

To do this, you'll need to use a little bit of code. First, open the Visual Basic editor (in Excel, press Alt+F11). In the editor, double-click on the sheet where you want to enable AutoComplete for an entire column. This will open the code window for that sheet.

In the code window, paste the following code:

Private Sub Worksheet_Change(ByVal Target As Range)

    Dim rng As Range
    Dim str As String

    'Only run this code if a single cell is changed
    If Target.Cells.Count = 1 Then

        'Check if the cell is in column A
        If Target.Column = 1 Then

            'Turn off events so this code doesn't run again unnecessarily
            Application.EnableEvents = False

            'Set the range to be the entire column
            Set rng = Range("A:A")

            'Get the value of the changed cell
            str = Target.Value

            'Loop through each cell in the range
            For Each cell In rng

                'If the value of the cell matches the changed cell, then set its value to be empty
                If cell.Value = str Then
                    cell.Value = ""
                End If

            Next cell

            'Turn events back on
            Application.EnableEvents = True

        End If

    End If    
End Sub

How does this code work?

This code uses a few different features of Excel and Visual Basic. Let's take a look at each part:

  1. Private Sub Worksheet_Change(ByVal Target As Range): This line of code declares a subroutine, which is a set of instructions that can be run together. The Worksheet_Change subroutine will run automatically any time a cell on the worksheet is changed.
  2. Dim rng As Range: This line declares a variable called rng. A variable is like a container that can store a value. In this case, we'll use it to store a range of cells.
  3. Dim str As String: This line declares another variable, called str. This time, we'll use it to store text values.
  4. If Target.Cells.Count = 1 Then: This line checks whether more than one cell was changed before running the rest of the code. We only want to run the code if a single cell was changed.
  5. If Target.Column = 1 Then: This line checks whether the changed cell is in column A.
  6. Application.EnableEvents = False: This line turns off events so that our code doesn't run again unnecessarily.
  7. Set rng = Range("A:A"): This line sets our rng variable to be equal to all cells in column A.
  8. str = Target.Value: This line sets our str variable equal to the value of the changed cell.
>

For Each

>
For Each >  cell >  In >  rng >  >>  >>  >>  >>  >>      >>      >>          >>              >>                  >>                      >>>                          >>>                              >>>                                  >>>                                      >>>                                          >>>                                              >>>                                                  >>>                                                      >>>  
> > > > > > > >> >> >> >>

Move beyond 

Excel

Get started with Causal today.
Build models effortlessly, connect them directly to your data, and share them with interactive dashboards and beautiful visuals.