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

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.
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,226,729
Messages
6,192,696
Members
453,747
Latest member
tylerhyatt04

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