VBA to find Value, then Name Cell

OldDogNewTricks

New Member
Joined
Jun 27, 2014
Messages
21
I want my code to do two things.
1) Locate the cells in Column A that have a certain value
2) Name the cells with that certain value

All the values will be in Column A. The word I am searching for will appear three different times in the column. I need a code that will find those cells and name them. I have tried multiple different angles, and I am not sure where I am going wrong. I greatly appreciate any help you can provide!

I have Excel 2007.

Example:
The following is supposed to represent Column A in my document. I am searching for cells that have the word "Bacon" in it. The code should name the first cell containing the word, "Bacon1." The next cell containing the word should be named "Bacon2." This continues until no more cells containing "Bacon" can be found and labeled.

Toppings
Bacon
Hamburger
Chicken
Peppers
Cheese
Meats

Bacon
Hamburger
Chicken
Extras
Sauce
Cheese
Bacon
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
I am using Excel 2007. I want to erase the names in every row in column A of my worksheet, so I do not run into issues where a row has a name from a previous time I ran the code. I can only figure out how to erase all the names in the workbook.
 
Upvote 0
I am using Excel 2007. I want to erase the names in every row in column A of my worksheet, so I do not run into issues where a row has a name from a previous time I ran the code. I can only figure out how to erase all the names in the workbook.
You can use this macro to delete all the named rows on the worksheet...
Code:
Sub DeleteNames()
  Dim N As Name
  On Error Resume Next
  For Each N In ActiveWorkbook.Names
    If Range(Mid(N.RefersTo, 2)).Columns.Count = Columns.Count Then N.Delete
  Next N
  On Error GoTo 0
End Sub
 
Upvote 0
The previous code for deleting the row names does not work like I thought it had. However, I found this in the Mr. Excel archives, and it works for deleting name ranges for a single sheet:

Dim n As Name
Dim Sht As String

' Put in name of sheet where the range is located
Sht = "Sheet2"

For Each n In ActiveWorkbook.Names
If n.RefersToRange.Worksheet.Name = Sht Then
n.Delete
End If
Next n
 
Upvote 0
The previous code for deleting the row names does not work like I thought it had. However, I found this in the Mr. Excel archives, and it works for deleting name ranges for a single sheet:

Dim n As Name
Dim Sht As String

' Put in name of sheet where the range is located
Sht = "Sheet2"

For Each n In ActiveWorkbook.Names
If n.RefersToRange.Worksheet.Name = Sht Then
n.Delete
End If
Next n

Let's combine the relevant part of the above code with the code I posted to make sure the only defined names that get deleted are the ones that refer to whole rows on the sheet. And note that I changed the hard-coded sheet name to the active worksheet so that you can run this code on any sheet without having to change the name assigned to a variable each time... just activate the sheet whose whole-row defined names you want to delete and run the code.
Code:
Sub DeleteNames()
  Dim N As Name
  On Error Resume Next
  For Each N In ActiveWorkbook.Names
    If N.RefersToRange.Worksheet.Name = ActiveSheet.Name Then
      If Range(Mid(N.RefersTo, 2)).Columns.Count = Columns.Count Then N.Delete
    End If
  Next N
  On Error GoTo 0
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,239
Messages
6,170,947
Members
452,368
Latest member
jayp2104

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