![]() |
![]() |
|
|||||||
| Excel Questions All Excel/VBA questions - formulas, macros, pivot tables, general help, etc. Please post to this forum in English only. |
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
New Member
Join Date: Feb 2002
Posts: 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 |
|
|
|
|
|
#2 | |
|
MrExcel MVP
Join Date: Feb 2002
Location: Portland, OR USA
Posts: 1,374
|
Quote:
Cells(1,1).Select ' or Range("A1").Select Hope this helps, Russell |
|
|
|
|
|
|
#3 |
|
New Member
Join Date: Feb 2002
Posts: 18
|
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
|
|
|
|
|
|
#4 |
|
Board Regular
Join Date: Feb 2002
Location: Chippenham, UK
Posts: 136
|
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.
__________________
Regards, Gary Hewitt-Long |
|
|
|
|
|
#5 |
|
New Member
Join Date: Feb 2002
Posts: 18
|
Thanks, that was what was wrong with it
|
|
|
|
![]() |
| Bookmarks |
| Thread Tools | |
| Display Modes | |
|
|