VBA code Question If statemanet

Jhagg

New Member
Joined
Jan 8, 2023
Messages
1
Office Version
  1. 365
Platform
  1. Windows
Hi i have a Problem with som coding in VBA I am new to this and would be verry grateful if you could help me with this.

I am trying to copy value form a range in one sheet and then paste it in another worksheet based on a ID value

so if i have ID value 1 I would like the Data to be copied and pased in the other worksheet.

This is my code so far but it do not work.

Sub CopyIf()

If ActiveSheet.Range("C12").Value = Sheets("Input").Range("C13:C100").Value Then

ActiveSheet.Range("A12:CC12").Select

Selection.Copy
Sheets("Input").Select
Sheets("Input").Range("C13").PasteSpecial Paste:=xlPasteValues

End If


End Sub

Pleas help me with this.
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Value in C12 would be 1from your example? You can't compare one cell value against a range of some 88 cells that way. Use the vba Find method.
You don't need to select ranges to copy from or paste to, you can do so directly.
 
Upvote 0
Maybe something like
VBA Code:
Sub MM1()
Dim str As String, srchRng As Range
With ActiveSheet
    str = .Cells(12, 3).Value
    Set srchRng = Sheets("Input").Range("C13:C100").Find(what:=str)
    If srchRng Is Nothing Then
        MsgBox "Not found"
        Exit Sub
    End If
    .Range("A12:CC12").Copy
    Sheets("Input").Range("C13").PasteSpecial Paste:=xlPasteValues
End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,851
Messages
6,121,931
Members
449,056
Latest member
denissimo

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