Select Case Help

davidhall

Board Regular
Joined
Mar 6, 2011
Messages
174
Cell D9 varies based on inputs from another cell. The value in cell D9 could be 1, 2, or 3.

If D9 is a 1, then range p5:p10 should be copied to cell n5
If D9 is a 2, then range q5:q15 should be copied to cell n5

N5 is the beginning of a designated range for a combo box (form control) and I would like this items within the combo box to automatically update based on the value in D9.

So, I'm having trouble with my code. See below. Any help is appreciated.


Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)

Dim iTarget As Range
iTarget = Range("D9").Value

Application.ScreenUpdating = False

Select Case iTarget

Case 1
Range("P5:P15").Select
Selection.Copy
Range("N5").Select
ActiveSheet.Paste
Range("D3").Activate
Exit Sub

Case 2
Range("Q5:Q15").Select
Selection.Copy
Range("n5").Select
ActiveSheet.Paste
Range("D3").Activate
Exit Sub


End Select


Application.ScreenUpdating = True


End Sub


Any suggestions?
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
don't dim iTarget as a range. It should probably be integer

Not that it should affect whether the code works or not but you can abbrviate
Code:
Range("P5:P15").Select
Selection.Copy
Range("N5").Select
ActiveSheet.Paste
Range("D3").Activate
to
Code:
Range("P5:P15").copy range("N5")
 
Last edited:
Upvote 0
Your code, I think makes a mistake on these lines:
Rich (BB code):
Dim iTarget As Range
iTarget = Range("D9").Value
Because you're defining iTarget as a Range and then trying to assign a value to it. You can only assign the same data type to the equivalent variable

Instead, try (change in red):
Rich (BB code):
Option Explicit
 
Private Sub Worksheet_Change(ByVal Target As Range)
 
Dim iTarget As Integer
iTarget = Range("D9").Value
 
Application.ScreenUpdating = False
 
Select Case iTarget

    Case 1
      Range("P5:P15").Copy
      Range("N5").Paste
      Range("D3").Activate
      Exit Sub
 
    Case 2
      Range("Q5:Q15").Copy
      Range("N5").Paste
      Range("D3").Activate
      Exit Sub
 
End Select

Application.ScreenUpdating = True

End Sub
 
Upvote 0
Ok. Here is what I have in Sheet1.


Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column = 4 Then

Call myMacro

End If

End Sub


Here is what I have in Module1

Sub myMacro()
Application.ScreenUpdating = False

'Dim iTarget As Integer
'iTarget = Range("D9")

If Range("D9").Value = 1 Then
Range("P5:P15").Select
Selection.Copy
Range("N5").Select
ActiveSheet.Paste
Application.CutCopyMode = False

ElseIf Range("D9").Value = 2 Then


Range("Q5:Q15").Select
Selection.Copy
Range("N5").Select
ActiveSheet.Paste
Application.CutCopyMode = False

ElseIf Range("D9").Value = 3 Then

Range("R5:R15").Select
Selection.Copy
Range("N5").Select
ActiveSheet.Paste
Application.CutCopyMode = False

End If




Application.ScreenUpdating = True
Range("D3").Activate
End Sub


Here is the issue I am dealing with.
I can run the macro "myMacro" and it works fine. What I am trying to accomplish is if the value in D9 changes, then the macro is called. Right now, I have to click on cell D9 and press enter or something then the macro runs. The problem is that users wont have the ability to click on D9 because it will be locked. D9 contains a formula with an "if" statement linked to a variable input cell (C5) the user has the option to change. Based on the value input in C5, D9 changes and the macro should runs. It's not doing that right now and can't figure it out.


Any help?
 
Upvote 0

Forum statistics

Threads
1,224,609
Messages
6,179,877
Members
452,949
Latest member
Dupuhini

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