updating values macro problem

P5X

New Member
Joined
Feb 16, 2002
Messages
18
I am trying to update the value of a cell if it matches the value in another cell:

Sub update()
Range("J18,J20,J22").Copy
Do Until ActiveCell.Value = ""
If ActiveCell.Value = Range("J18") Then
ActiveCell.PasteSpecial _
Transpose:=True
Else
ActiveCell.Offset(1, 0).Activate
End If
Loop
End Sub

It works if I dont use the "Do until" loop but without the loop it isn't much use.
If I use the loop then it carries on repeating itself, its probably a simple problem but which part of the code needs adjustment?
Thanks
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
On 2002-02-28 15:00, P5X wrote:
I am trying to update the value of a cell if it matches the value in another cell:

Sub update()
Range("J18,J20,J22").Copy
Do Until ActiveCell.Value = ""
If ActiveCell.Value = Range("J18") Then
ActiveCell.PasteSpecial _
Transpose:=True
Else
ActiveCell.Offset(1, 0).Activate
End If
Loop
End Sub

It works if I dont use the "Do until" loop but without the loop it isn't much use.
If I use the loop then it carries on repeating itself, its probably a simple problem but which part of the code needs adjustment?
Thanks

You have to either make sure that you have the correct cell selected before you start your macro, or select it w/in the code itself, something like this:

Cells(1,1).Select
' or
Range("A1").Select

Hope this helps,

Russell
 
Upvote 0
Yeah, I haven't put that into the code yet as i'm just testing it so i'm manually selecting the start cell but it doesn't seem to work, once it finds the matching cell it pastes the required data into it again and again...and again :(
 
Upvote 0
Sub update()
Range("J18,J20,J22").Copy
Do Until ActiveCell.Value = ""
If ActiveCell.Value = Range("J18") Then
ActiveCell.PasteSpecial _
Transpose:=True
Else
ActiveCell.Offset(1, 0).Activate
End If
Loop
End Sub

Where you have ActiveCell.Offset(1,0).Activate this moves the cell down one if the cell doesn't match your requirements.

You don't have this in the part if it does match your requirements, so I guess when it gets to a cell that is true then it will keep pasting over the same cell, try:

Sub update()
Range("J18,J20,J22").Copy
Do Until ActiveCell.Value = ""
If ActiveCell.Value = Range("J18") Then
ActiveCell.PasteSpecial _
Transpose:=True
ActiveCell.Offset(1, 0).Activate ' This moves down one row.
Else
ActiveCell.Offset(1, 0).Activate
End If
Loop
End Sub

Make sure that after pasting the offset is putting you back into an Activecell in the right place, i.e. column and row.
 
Upvote 0

Forum statistics

Threads
1,213,563
Messages
6,114,332
Members
448,566
Latest member
Nickdozaj

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