Macros that copy certain range to another cell conditionally

burnin2emax

New Member
Joined
Nov 9, 2005
Messages
10
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.

Thanks in advance.
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.

jindon

MrExcel MVP
Joined
Aug 21, 2004
Messages
16,995
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
 

burnin2emax

New Member
Joined
Nov 9, 2005
Messages
10
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...
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,164,057
Messages
5,835,162
Members
430,343
Latest member
t0m_c

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
Top