# Macros that copy certain range to another cell conditionally

#### burnin2emax

##### New Member
Hi there,

I need help in a macro to perform the following.

The situation is as such :
I have a cell C6 waiting for user input.
I want to create a control button which runs a macro which will copy certain range of cells according to the user input.

For example, when a user keys in "a" at cell C6, when the macro is run, columns(A:D) of Worksheet A are copied to columns(B:E) of Worksheet B.
When a user keys in "b" at cell C6, when the macro is run, columns(E:H) of Worksheet A are copied to columns(B:E) of Worksheet B.

Hope someone can help.

### Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Hi
paste the code onto sheet module
Code:
``````Option Compare Text

Private Sub Worksheet_Change(ByVal Target As Range)
Dim txt As String
With Target.Cells(1, 1)
If .Address(0, 0) <> "C6" Then Exit Sub
Select Case .Value
Case "a"
txt = "A:D"
Case "b"
txt = "E:H"
Case Else
Exit Sub
End Select
Me.Range(txt).Copy Sheets("sheet2").Range("b1")
End With
End Sub``````

Hi ,

Being the noob that I am, I don't quite grasp what's happening in the code that was given. Perhaps, someone can enlighten me.

As mentioned in earlier post, I want to create a control button which runs a macro which copy certain range of cells according to a user input and copy it into another sheet.

Situation :
If user input "a" into cell C6, when the macro is run, the range(A1:D1) of "worksheet A" are copied onto (A5:D5) of another worksheet, "worksheet B".
If user input "b" into cell C6, when the macro is run, the range(A2:D2) of "worksheet A" are copied onto (A5:D5) of another worksheet, "worksheet B".

Thanks in advance for the help...

Replies
3
Views
180
Replies
4
Views
348
Replies
3
Views
154
Replies
1
Views
300
Replies
1
Views
151

1,216,313
Messages
6,130,010
Members
449,550
Latest member
8073662045

### 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.

### Which adblocker are you using?

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

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