I am new to using Macros in excel. I am trying to figure out how to pull a range of cell values into different range of cells. The cell values pulled will depend on the value of another cell. Below is an example of code I made to pull the cells using multiple lines of code.
Sub CopyRange()
If Range("Q2").Value = "A" Then
Range("R2").Value = Range("A2")
Range("S2").Value = Range("B2")
Range("R3").Value = Range("A3")
Range("S3").Value = Range("B3")
Range("R4").Value = Range("A4")
Range("S4").Value = Range("B4")
Range("R5").Value = Range("A5")
Range("S5").Value = Range("B5")
End If
If Range("Q2").Value = "B" Then
Range("R2").Value = Range("C2")
Range("S2").Value = Range("D2")
Range("R3").Value = Range("C3")
Range("S3").Value = Range("D3")
Range("R4").Value = Range("C4")
Range("S4").Value = Range("D4")
Range("R5").Value = Range("C5")
Range("S5").Value = Range("D5")
End If
End Sub
Is there a way I can do it in fewer lines of code? For example I tried writing it like shown below and it did not work:
If Range("Q2").Value = "A" Then
Range("R2:S5").Value = Range("A2:B5")
Also, would it be possible to pull values from A2:B5 on Sheet2 into cells R2:S5 on Sheet1?
Thanks in advance for the help!
Bookmarks