VBA logical test on active cell, copy paste to other sheet if true

patricknord

New Member
Joined
Apr 1, 2013
Messages
5
Hello--I am new to this board and a bit new to Excel VBA.

I am trying to write a script that will do the following--when run, test if the active cell in the current sheet (sheet1) is equal to the active cell in a second sheet (I have named it creatively--sheet2). If yes, then I want it to return a value of 2 in the cell one to the right of the active cell in sheet2; if they are not equal (else, right?), I want it to move down one cell, paste the active cell from sheet1 and enter a value of one in the cell one to the right.

Any help?

Thank you!!!

p
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
Hello--I am new to this board and a bit new to Excel VBA.

I am trying to write a script that will do the following--when run, test if the active cell in the current sheet (sheet1) is equal to the active cell in a second sheet (I have named it creatively--sheet2). If yes, then I want it to return a value of 2 in the cell one to the right of the active cell in sheet2; if they are not equal (else, right?), I want it to move down one cell, paste the active cell from sheet1 and enter a value of one in the cell one to the right.

Any help?

Thank you!!!

p

Hi,

Is this what you were looking for :
Code:
Sub logicalTest()


    Dim currCell, pasteCell As Range
    
    Set currCell = ActiveCell
    
    MsgBox currCell.Address
    
    If currCell.Value = Sheet2.Cells(currCell.Row, currCell.Column) Then
        Sheet2.Cells(currCell.Row, currCell.Column + 1).Value = "2"
        
    Else
        currCell.Copy
        Sheet2.Activate
        
        Sheet2.Cells(currCell.Row + 1, currCell.Column).PasteSpecial (xlPasteAll)
        Sheet2.Cells(currCell.Row, currCell.Column + 1).Value = "1"
        
    End If
    
End Sub

Regards,

Sagar Yerunkar
 
Upvote 0
Sagar--

You are awesome, thank you! I made a couple adjustments--I didn't need the message box and it was dropping the txt value down a cell, but this got me there. Thank you thank you!!

p
 
Upvote 0

Forum statistics

Threads
1,223,270
Messages
6,171,102
Members
452,379
Latest member
IainTru

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