Copy If

plwandatw

New Member
Joined
Apr 17, 2023
Messages
9
Office Version
  1. 2016
Platform
  1. Windows
Good afternoon,
I am trying to copy a cell based on another cells value and paste on another worksheet in column A at the next available cell.
Worksheet1 If F10="A" and if J10=0 then Copy C10 to Worksheet2 Column A at next available cell.

Thank you in advance.
Johnboy
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
If you are talking about a VBA solution, then it would be something like this.
VBA Code:
'I am trying to copy a cell based on another cells value and paste on another worksheet in column A at the next available cell.
'Worksheet1 If F10="A" and if J10=0 then Copy C10 to Worksheet2 Column A at next available cell.
Sub CopyCell()
    Dim WS1 As Worksheet, WS2 As Worksheet
    
    Set WS1 = Worksheets("MyWorkSheet1")
    Set WS2 = Worksheets("MyWorkSheet2")
    
    If WS1.Range("F10").Value = "A" And WS1.Range("J10").Value = 0 Then
        WS1.Range("C10").Copy WS2.Range("A" & WS2.Rows.Count).End(xlUp).Offset(1)
    End If
End Sub
 
Upvote 0
Thank you for the quick response, this works great, Next question, is there a way to check each row on sheet1 until last row with data?

Johnboy
 
Upvote 0
There probably is, but you have not provided enough information about what that actually means for me to do anything with.
 
Upvote 0
There probably is, but you have not provided enough information about what that actually means for me to do anything with.
instead of a static cell use dynamic cell...
For each row of data on worksheet1 check the values in Column F and K, and copy data in Column C to worksheet2... I tried to Loop, but just not enough VBA experience.
 
Upvote 0
VBA Code:
'I am trying to copy a cell based on another cells value and paste on another worksheet in column A at the next available cell.
'Worksheet1 If Fn="A" and if Jn=0 then Copy Cn where n = row number to Worksheet2 Column A at next available cell.
Sub CopyCell()
    Dim WS1 As Worksheet, WS2 As Worksheet
    Dim I As Long

    Set WS1 = Worksheets("MyWorkSheet1")
    Set WS2 = Worksheets("MyWorkSheet2")

    Set WS1 = ActiveSheet
    Set WS2 = ActiveSheet

    For I = 1 To 100                                  'rows
        If WS1.Range("F" & I).Value = "A" And WS1.Range("J" & I).Value = 0 Then
            WS1.Range("C" & I).Copy WS2.Range("A" & WS2.Rows.Count).End(xlUp).Offset(1)
        End If
    Next I
End Sub
 
Upvote 0
Solution
VBA Code:
'I am trying to copy a cell based on another cells value and paste on another worksheet in column A at the next available cell.
'Worksheet1 If Fn="A" and if Jn=0 then Copy Cn where n = row number to Worksheet2 Column A at next available cell.
Sub CopyCell()
    Dim WS1 As Worksheet, WS2 As Worksheet
    Dim I As Long

    Set WS1 = Worksheets("MyWorkSheet1")
    Set WS2 = Worksheets("MyWorkSheet2")

    Set WS1 = ActiveSheet
    Set WS2 = ActiveSheet

    For I = 1 To 100                                  'rows
        If WS1.Range("F" & I).Value = "A" And WS1.Range("J" & I).Value = 0 Then
            WS1.Range("C" & I).Copy WS2.Range("A" & WS2.Rows.Count).End(xlUp).Offset(1)
        End If
    Next I
End Sub
The Copy / Paste function copies WS1 column C to same WS1 column A, WS2 no populating from WS1
 
Upvote 0
Not sure why the second "Set WS1 & WS2" but I removed, and the macro works Great... Thank you for your help.

Sub CopyCell()
Dim WS1 As Worksheet, WS2 As Worksheet
Dim I As Long

Set WS1 = ActiveSheet
Set WS2 = Worksheets("Income")

'Set WS1 = ActiveSheet
'Set WS2 = ActiveSheet

For I = 1 To 100 'rows
If WS1.Range("F" & I).Value = "Deposit" And WS1.Range("J" & I).Value = 0 Then
WS1.Range("C" & I).Copy WS2.Range("A" & WS2.Rows.Count).End(xlUp).Offset(1)
End If
Next I
End Sub
 
Upvote 0
Not sure why the second "Set WS1 & WS2" but I removed, and the macro works Great... Thank you for your help.
Well, that is what we in the programming community like to refer to as a "mistake" :). The second set was something I put in there to make testing easier for me then forgot to take out again. Cheers.
 
Upvote 0
Not sure why the second "Set WS1 & WS2" but I removed, and the macro works Great... Thank you for your help.
The marked solution has been changed accordingly. In your future questions, please mark the post as the solution that actually answered your question, instead of your feedback message as it will help future readers. No further action is required for this thread.
 
Upvote 0

Forum statistics

Threads
1,215,137
Messages
6,123,253
Members
449,093
Latest member
Vincent Khandagale

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