VBA Loop code to replace text

jd80

New Member
Joined
Nov 15, 2012
Messages
2
I have a worksheet with several hundred thousand rows. First column has numbers, in ranges of 25 numbered and 50 numbered sequences. Second column has names that the numbers are assigned to.
When I did the vlookup to assign names to each number, I was only able to put a name next to the first number in a range because the worksheet I was pulling from only had the first number.
So I have the start of a range with a name, then #N/A next to the following 24 or 49 numbers, then a name next to the first number for the next range and #N/A following for the rest of the range.

What would the code be to go through and replace the the #N/A with the name above until the next instance of a name?
Below is a sample of the data I have.
1726 Smith,Joe
1727#N/A
1728#N/A
1729#N/A
1730#N/A
1731#N/A
1732#N/A
1733#N/A
1734#N/A
1735#N/A
1736#N/A
1737#N/A
1738#N/A
1739#N/A
1740#N/A
1741#N/A
1742#N/A
1743#N/A
1744#N/A
1745#N/A
1746#N/A
1747#N/A
1748#N/A
1749#N/A
1750#N/A
1751#N/A
1752#N/A
1753#N/A
1754#N/A
1755#N/A
1756#N/A
1757#N/A
1758#N/A
1759#N/A
1760#N/A
1761#N/A
1762#N/A
1763#N/A
1764#N/A
1765#N/A
1766#N/A
1767#N/A
1768#N/A
1769#N/A
1770#N/A
1771#N/A
1772#N/A
1773#N/A
1774#N/A
1775#N/A
1776Doe, John
1777#N/A
1778#N/A
1779#N/A
1780#N/A
1781#N/A
1782#N/A
1783#N/A
1784#N/A
1785#N/A
1786#N/A
1787#N/A
1788#N/A
1789#N/A
1790#N/A
1791#N/A
1792#N/A
1793#N/A
1794#N/A
1795#N/A
1796#N/A
1797#N/A
1798#N/A
1799#N/A
1800#N/A
1801#N/A
1802#N/A
1803#N/A
1804#N/A
1805#N/A
1806#N/A
1807#N/A
1808#N/A
1809#N/A
1810#N/A
1811#N/A
1812#N/A
1813#N/A
1814#N/A
1815#N/A
1816#N/A
1817#N/A
1818#N/A
1819#N/A
1820#N/A
1821#N/A
1822#N/A
1823#N/A
1824#N/A
1825#N/A
1826Doe, Jane
1827#N/A
1828#N/A
1829#N/A
1830#N/A
1831#N/A
1832#N/A
1833#N/A
1834#N/A
1835#N/A
1836#N/A
1837#N/A
1838#N/A
1839#N/A
1840#N/A
1841#N/A
1842#N/A
1843#N/A
1844#N/A
1845#N/A
1846#N/A
1847#N/A
1848#N/A
1849#N/A
1850#N/A
1851#N/A
1852#N/A
1853#N/A
1854#N/A
1855#N/A
1856#N/A
1857#N/A
1858#N/A
1859#N/A
1860#N/A
1861#N/A
1862#N/A
1863#N/A
1864#N/A
1865#N/A
1866#N/A
1867#N/A
1868#N/A
1869#N/A
1870#N/A
1871#N/A
1872#N/A
1873#N/A
1874#N/A
1875#N/A

<tbody>
</tbody>
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
You don’t really need code for this.

First off, delete all the #N/As.

Now highlight the range beginning with the first name to the bottom

Select F5 >> Special >> check Blanks

Press the equal sign and then the up arrow one time. Ctrl + Enter
 
Upvote 0
Maybe something like:
Rich (BB code):
Sub ReplaceMe()

Dim i as Long
Dim Temp as String

Application.ScreenUpdating = False

For i = 2 to Range("B" & Rows.Count).End(xlUp).Row
  If Left(Range("B" & i), 1) <> "#" Then
     Temp = Range("B" & i).Value
  Else
      Range("B" & i).Value = Temp
  End If
Next i

Application.ScreenUpdating = True

End Sub
The above assumes you have header values in row 1 (i.e. your data starts on row 2) and that B2 contains a name and not an error
 
Upvote 0
Perfect, thank you!

You don’t really need code for this.

First off, delete all the #N/As.

Now highlight the range beginning with the first name to the bottom

Select F5 >> Special >> check Blanks

Press the equal sign and then the up arrow one time. Ctrl + Enter
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,327
Messages
6,124,280
Members
449,149
Latest member
mwdbActuary

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