VBA copy cell to same row if criteria match

kitsa

Board Regular
Joined
Mar 4, 2016
Messages
111
Office Version
  1. 365
  2. 2016
Hi,
I'm looking to see how I create VBA to copy cell value from 1 cell to another cell in same row, of criteria e.g. "S" or "V".
As per pic:
If Cell B:B = "S" or "V", copy Cell G:G to Cell J:J on same row.

1652142921025.png
 

Attachments

  • 1652142831002.png
    1652142831002.png
    91.5 KB · Views: 8

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
Try this:
VBA Code:
Sub Copy_If_S_Or_V()
'Modified  5/9/2022  10:37:54 PM  EDT
Application.ScreenUpdating = False
Dim i As Long
Dim Lastrow As Long
Lastrow = Cells(Rows.Count, "B").End(xlUp).Row

    For i = 1 To Lastrow
        Select Case Cells(i, 2).Value
            Case "S", "V": Cells(i, "G").Copy Cells(i, "J")
        End Select
    Next
Application.ScreenUpdating = True
End Sub
 
Upvote 0
Try this:
VBA Code:
Sub Copy_If_S_Or_V()
'Modified  5/9/2022  10:37:54 PM  EDT
Application.ScreenUpdating = False
Dim i As Long
Dim Lastrow As Long
Lastrow = Cells(Rows.Count, "B").End(xlUp).Row

    For i = 1 To Lastrow
        Select Case Cells(i, 2).Value
            Case "S", "V": Cells(i, "G").Copy Cells(i, "J")
        End Select
    Next
Application.ScreenUpdating = True
End Sub
Hi,
I tried the code and it works, but when I tried on other spreadsheet it doesn't work due to the cap between data as show below.
Also I want to go to another sheet and it doesn't work once it goes to the other sheet.
I created a command button and put the code in Design Mode, is that correct?

1652220040685.png
 
Upvote 0
I also noticed I made a mistake on my request. I need the copy to be paste special.
"Case "S", "V": Cells(i, "I").Copy Cells(i, "J")"
 
Upvote 0
Well, if the script worked on one worksheet but not on another.
It's not because of my script.
You must have things laid out differently on other sheet

And I do not understand this:
I tried on other spreadsheet it doesn't work due to the cap between data as show below.
what is cap?

And why Pastespecial
 
Upvote 0
Well, if the script worked on one worksheet but not on another.
It's not because of my script.
You must have things laid out differently on other sheet

And I do not understand this:
I tried on other spreadsheet it doesn't work due to the cap between data as show below.
what is cap?

And why Pastespecial
Pastespecial has a lot of possibilities
So tell me what is wrong with copy
 
Upvote 0
And what does this mean?
You said:
Also, I want to go to another sheet and it doesn't work once it goes to the other sheet.

Do you mean you want it copied to another sheet?
And if so what is name of other sheet
And you never mentioned this in your original post
 
Upvote 0
Sorry all, I mean due to the gap, it doesn't work.
I need to put a command tab on "Sheet1" and for the VBA to work on "Sheet2"
What I need is copy from Column "I" and paste special to Column "J" (with no formula).
Hope this makes sense?

Thanks
Kitsa
 
Upvote 0
In your original post you said:
If Cell B:B = "S" or "V", copy Cell G:G to Cell J:J on same row.

But in your last post you said:
What I need is copy from Column "I" and paste special to Column "J" (with no formula).

So what is it Copy column "I" or column "G"
 
Upvote 0
This script here assumes we are dealing Column G and Not Column I

In the script Change G to I if you want:

VBA Code:
Sub Copy_If_S_Or_V()
'Modified  5/10/2022  9:11:09 PM  EDT
Application.ScreenUpdating = False
Dim i As Long
Dim Lastrow As Long
Lastrow = Sheet2.Cells(Rows.Count, "B").End(xlUp).Row
    With Sheet2
        For i = 1 To Lastrow
            Select Case .Cells(i, 2).Value
                Case "S", "V": .Cells(i, "J").Value = .Cells(i, "G").Value
            End Select
        Next
    End With
Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,745
Messages
6,126,634
Members
449,324
Latest member
AmirMalik

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