macro help column A to column B based on list

PhoneBoy

New Member
Joined
Oct 2, 2006
Messages
12
I'm sure that the answer is here, but I don't know enough about it to search intelligently.

Problem: I get a huge spreadsheet (10,000 lines or so) each month that I have to divide up and pay people. I have been using a method that I saw in this post

http://www.mrexcel.com/board2/viewtopic.php?t=167633&highlight=macro+replace+text

which Erik Van Geit proposed. I rename my worksheet as data and create a second worksheet called list where I put my replacement text. It works beatifully especially because I have to do this each month and my list changes as we open new stores.

What I am wanting to do is instead of replacing the text, I would like to create a new column with the info. For example, I am currently replacing 8857 with NC. I would like to keep 8857 in column B and add NC in column C. The if/then statements will not work due to the number of replacements that I have (I think).

Any help would be appreciated.

Thanks,
Scott
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Getting closer. I did not get any errors this time, but there was no resultant text in column B. Ideas?
 
Upvote 0
try this one first and return with message if you get...
Code:
Option Explicit

Sub replace_list()
'Erik Van Geit
'050910
'using 2 columns

Dim LR As Long
Dim RW As Worksheet
Dim LW As Worksheet
Dim I As Integer
Dim r As Range, ff As String, msg As String, flg As Boolean

Set RW = Sheets("data") 'worksheet where you want to replace items
Set LW = Sheets("list") 'worksheet with replace-lists

LR = LW.Cells(65536, 1).End(xlUp).Row

For I = 2 To LR
   Set r = RW.Columns("b").Find(LW.Cells(I, 1).Value,,,xlWhole)
   If Not r Is Nothing then
      ff = r.Address : flg = True
      Do
         r.Offset(,1).Value= LW.Cells(I,2).Value
         Set r = RW.Columns("b").FindNext(r)
      Loop Until ff = r.Address
   Else
      msg = msg & LW.Cells(I,2).Value & vbLf
   End If
Next I
If Len(msg) Then
   If Not flg Then 
      msg = "Nothing Found check the data" & vbLf & msg
   Else
      msg = "Following data not found" & vbLf & msg
   end If
   MsgBox msg
End If
End Sub
 
Upvote 0
I got a pop up box that says nothing found check the data with:

NC
NC
NC
NC
NC
NC
NC
NC
NC
AL
AL
etc.

Basically, it looks like what I want to be in column B, but it doesn't seem to be able to match it based off of column A

Thanks again. I'm sure that you have better stuff to do
 
Upvote 0
OK
try change one line that is

Set r = RW.Columns("b").Find(LW.Cells(I,1).Value,,,xlWhole)

to

Set r = RW.Columns("b").Find(Lw.Cells(I,1).Value,,,xlPart)

and see what you get..
 
Upvote 0
It didn't change anything. Same error message. I tried it with a blank column B that I inserted and the normal column B that is there. I appreciate your patience. Thanks again.
 
Upvote 0
does this make any change?
Code:
Option Explicit

Sub replace_list()
'Erik Van Geit
'050910
'using 2 columns

Dim LR As Long
Dim RW As Worksheet
Dim LW As Worksheet
Dim I As Integer
Dim r As Range, ff As String, msg As String, flg As Boolean

Set RW = Sheets("data") 'worksheet where you want to replace items
Set LW = Sheets("list") 'worksheet with replace-lists

LR = LW.Cells(65536, 1).End(xlUp).Row

For I = 2 To LR
   Set r = RW.Columns("b").Find(LW.Cells(I, 1).Value,,xlValues,xlWhole)
   If Not r Is Nothing then
      ff = r.Address : flg = True
      Do
         r.Offset(,1).Value= LW.Cells(I,2).Value
         Set r = RW.Columns("b").FindNext(r)
      Loop Until ff = r.Address
   Else
      msg = msg & LW.Cells(I,1).Value & vbLf
   End If
Next I
If Len(msg) Then
   If Not flg Then 
      msg = "Nothing Found check the data" & vbLf & msg
   Else
      msg = "Following data not found" & vbLf & msg
   end If
   MsgBox msg
End If
End Sub
 
Upvote 0
Phoneboy -

that code will work if the data was in columnsB, It seems that your data was in columnsA, if that should be the case, then just change the code from;
Code:
Set r = RW.Columns("b").Find(LW.Cells(I, 1).Value,,xlValues,xlWhole)
to
Code:
Set r = RW.Columns("a").Find(LW.Cells(I, 1).Value,,xlValues,xlWhole)
and
Code:
Set r = RW.Columns("b").FindNext(r)
to
Code:
Set r = RW.Columns("a").FindNext(r)
 
Upvote 0
PhoneBoy

you have 2 sheets, "data" & "list"

sheet("list") has "what" in column.A & "replace" in Column.C

Then find each "what" in column B of sheet("data") & if find, "replace" will be in column.C of "data"

the code is doing this.

Am I missing something?
 
Upvote 0

Forum statistics

Threads
1,214,954
Messages
6,122,461
Members
449,085
Latest member
ExcelError

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