Find and Replace?

jmr0036

New Member
Joined
Jun 30, 2012
Messages
4
I just started trying to use VBA and have run into a problem that I haven't been able to work through.

Lets say in column A I have values 1-20 and in column B there are random numbers. In range E1:H8, the numbers from column A are located randomly. Would it be possible to replace the numbers in range E1:H8 with their corresponding numbers in column B?

ABCDEFGH
1491324
2488756
3479101112
446131714
5451516
64418
74319
84220
941
1040
1132
1254
1326
1412
1557
1638
1769
1824
1927
2038

<tbody>
</tbody>


Any help would be much appreciated!
 
Last edited:

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
Code:
for i = 1 to 20
    currentvalue = cells(i,2)' this is the value in b
    for b = 5 to 8
                if(cells(i,b) <> "") then
                         cells(i,b) = currentvalue
                end if
         next b
next i

hopefully this is what I understood from your question.
 
Upvote 0
This code will do what you asked...

Code:
Sub ReplaceAwithB()
  Dim X As Long
  For X = 1 To 20
    Range("E1:H8").Replace Cells(X, "A").Value, Cells(X, "A").Offset(, 1).Value, xlWhole
  Next
End Sub
 
Upvote 0
Thanks for your response. That is close to what I was asking but not quite. I don't know how better to explain it but I guess pictures might help.

The code you gave produced this:
EFGH
49494949
48484848
47474747
464646
4545
44
43
42

<colgroup><col width="64" span="4" style="width: 48pt; "></colgroup><tbody>
</tbody>



But I was trying to produce this:

ABCDEFGH
14949474846
24842434544
34741403254
446266912
54557 38
64424
743 27
84238
941
1040
1132
1254
1326
1412
1557
1638
1769
1824
1927
2038

<colgroup><col width="64" span="8" style="width: 48pt; "></colgroup><tbody>
</tbody>
 
Upvote 0
Ok this is weird. It worked perfectly with the small data set but when I tried using it with my full set it seemed to replace the numbers at random.


In order to account for the larger data set I changed the x value from 1to 20 to 1 to 574 and adjusted the range accordingly.
Sub ReplaceAwithB()
Dim X As Long
For X = 1 To 574
Range("e2:af29").Replace Cells(X, "a").Value, Cells(X, "a").Offset(, 1).Value, xlWhole
Next
End Sub


Any ideas?
 
Upvote 0
Ok this is weird. It worked perfectly with the small data set but when I tried using it with my full set it seemed to replace the numbers at random.


In order to account for the larger data set I changed the x value from 1to 20 to 1 to 574 and adjusted the range accordingly.
Sub ReplaceAwithB()
Dim X As Long
For X = 1 To 574
Range("e2:af29").Replace Cells(X, "a").Value, Cells(X, "a").Offset(, 1).Value, xlWhole
Next
End Sub


Any ideas?

I'll be stepping out for a few hours, so I won't be able to get back to this question for awhile, but what I think may be happening is, given the way I set things up, the expanded set is allowing numbers that were substituted with a smaller value to be substituted again from a later number in the list... maybe. That's an off-the-top-of-my-head guess... as I said, I'll look at this when I get back.
 
Upvote 0
Code:
Sub repl()
Dim c As Range
For Each c In Range("E1:H20")
    If Len(c) > 0 Then c.Value = Cells(c.Value, 2)
Next
End Sub
 
Upvote 0
I'll be stepping out for a few hours, so I won't be able to get back to this question for awhile, but what I think may be happening is, given the way I set things up, the expanded set is allowing numbers that were substituted with a smaller value to be substituted again from a later number in the list... maybe. That's an off-the-top-of-my-head guess... as I said, I'll look at this when I get back.

Actually, I have looked at what I posted and I do not see why it should not be working for you. My earlier guess (that perhaps it was re-substituting values) cannot happen because I specified xlWhole for the replacements, so smaller numbers cannot be being substituted because only whole cell values are looked at. Can you provide more details as to what you are seeing my function do incorrectly? Better would be if you could upload a copy of your workbook to one of the free file-sharing website so I (and others) can look directly at your data. If you have a SkyDrive account, use that, otherwise http://www.box.net/files works well (don't forget to post the URL they give you for your file). If you are reluctant to do that, you could send your workbook directly to me to look at. My email address is rickDOTnewsAtverizonDOTnet (replace the upper case letters with the symbols they spell out).
 
Upvote 0

Forum statistics

Threads
1,214,646
Messages
6,120,716
Members
448,985
Latest member
chocbudda

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