need to fill multiple blank cells in 6 columns with unique numbers

skyport

Active Member
Joined
Aug 3, 2014
Messages
374
On this one, I am working with data in columns A-F, G is not currently used however, H - AZ are occupied with important info.

Referring only to the data in columns A-F, with a few thousand lines of names and addresses etc, several of the cells contain nothing and are blank.

I need a quick way to assign and fill in each blank cell in that range with a unique number beginning with 1 and then 2, 3, etc etc.

I am hoping this can be done.
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Actually, regarding the above, the numbers do not have to be 1,2,3 etc. they could be even the value of the cell as long as each blank cell becomes populated with a unique letter,number or combo
 
Upvote 0
Actually, regarding the above, the numbers do not have to be 1,2,3 etc. they could be even the value of the cell as long as each blank cell becomes populated with a unique letter,number or combo
Give this a try (it fills them with address)...
Code:
Sub FillBlanksWithCellAddress()
  Dim Cell As Range
  For Each Cell In Intersect(Columns("A:G"), ActiveSheet.UsedRange).SpecialCells(xlBlanks)
    Cell.Value = Cell.Address(0, 0)
  Next
End Sub
 
Upvote 0
Hi again Rick. Beginning to feel like we are family. By the way on that last one earlier this morning, another great part about what you did was making it work by deleting the last entries and working downward and not the first or earlier ones which would have been a problem. Very very well done.

Will try this one now and give immediate feedback
 
Upvote 0
I think I know what is preventing it.

We had this same problem before with a previous code you did and the problem was on this end in the sense that further down the sheet, there will always be data floating around here and there that can distract the code.

You had brilliantly solved this on a previous code, that now works great, by simply having a window come up and prompt for the number of the last row with data to be processed under the code. I bet that would work the same on this one and eliminate the problem if you could write it in that way.
 
Upvote 0
skyport,

Here is another macro for you to consider.

Sample raw data in worksheet Sheet1 (you can change the sheet name in the macro):


Excel 2007
ABCDEF
1ABCDEF
2abcdef
3abcde
4abcdef
5abc
6ab
7a
8
9abcdef
10bdf
11ce
12abcdef
13
Sheet1


After the macro:


Excel 2007
ABCDEF
1ABCDEF
2abcdef
3abcde1
4abcdef
5abc222
6ab3333
7a44444
8555555
9abcdef
106b6d6f
1177c7e7
12abcdef
13
Sheet1


Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).

Code:
Sub ReplaceBlanksWithUniqueNumbers()
' hiker95, 04/22/2015, ME850525
Dim lr As Long, c As Range, n As Long
Application.ScreenUpdating = False
With Sheets("Sheet1")   '<-- you can change the sheet name here
  lr = .Cells.Find("*", , xlValues, xlWhole, xlByRows, xlPrevious, False).Row
  For Each c In .Range("A2:A" & lr)
    If Application.CountBlank(.Range("A" & c.Row & ":F" & c.Row)) > 0 Then
      n = n + 1
      With .Range("A" & c.Row & ":F" & c.Row).SpecialCells(xlCellTypeBlanks)
        .Value = n
      End With
    End If
  Next c
End With
Application.ScreenUpdating = True
End Sub

Before you use the macro with Excel 2007 or newer, save your workbook, Save As, a macro enabled workbook with the file extension .xlsm, and, answer the "do you want to enable macros" question as "yes" or "OK" (depending on the button label for your version of Excel) the next time you open your workbook.

Then run the ReplaceBlanksWithUniqueNumbers macro.
 
Last edited:
Upvote 0
Hiker

Good to see another old friend here.

I am getting a runtime error 1004 with a debug pointing to this line: With .Range("A" & c.Row & ":F" & c.Row).SpecialCells(xlCell
 
Upvote 0
skyport,

having a window come up and prompt for the number of the last row with data to be processed under the code.

Let me update the macro to include an InputBox to ask for the last used row in columns A-F.

Be back shortly.
 
Upvote 0
skyport,

Here is another macro solution for you to consider that asks for the last used row in columns A thru F:

Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).

Code:
Sub ReplaceBlanksWithUniqueNumbersV2()
' hiker95, 04/22/2015, ME850525
Dim lr As Long, c As Range, n As Long
Application.ScreenUpdating = False
With Sheets("Sheet1")   '<-- you can change the sheet name here
  lr = InputBox("What is the last used row in columns A thru F?")
  For Each c In .Range("A2:A" & lr)
    If Application.CountBlank(.Range("A" & c.Row & ":F" & c.Row)) > 0 Then
      n = n + 1
      With .Range("A" & c.Row & ":F" & c.Row).SpecialCells(xlCellTypeBlanks)
        .Value = n
      End With
    End If
  Next c
End With
Application.ScreenUpdating = True
End Sub

Before you use the macro with Excel 2007 or newer, save your workbook, Save As, a macro enabled workbook with the file extension .xlsm, and, answer the "do you want to enable macros" question as "yes" or "OK" (depending on the button label for your version of Excel) the next time you open your workbook.

Then run the ReplaceBlanksWithUniqueNumbersV2 macro.
 
Upvote 0

Forum statistics

Threads
1,215,219
Messages
6,123,687
Members
449,117
Latest member
Aaagu

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