VBA to automatically loop through validation list with any change

mgerving

New Member
Joined
Mar 18, 2010
Messages
33
I have a validation list that when a specific date is selected, it runs a "PasteData" macro.
How do I insert code to automatically loop through all the dates in the list, any time there is a change in the Sheet?
The PasteData sub runs perfectly, just trying to make this more automatic.

Sub Worksheet_Change(ByVal Target As Range)
Set Target = Range("B3")
If Target.Value = "Nov 20" Then
Call PasteData
End If
Set Target = Range("B3")
If Target.Value = "June 21" Then
Call PasteData
End If
Set Target = Range("B3")
If Target.Value = "Nov 21" Then
Call PasteData
End If
Set Target = Range("B3")
If Target.Value = "June 22" Then
Call PasteData
End If
Set Target = Range("B3")
If Target.Value = "Nov 22" Then
Call PasteData
End If
Set Target = Range("B3")
If Target.Value = "June 23" Then
Call PasteData
End If
Set Target = Range("B3")
If Target.Value = "Nov 23" Then
Call PasteData
End If
End Sub
 
What is the validation rule in B3?
 
Upvote 0

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
On this same worksheet, "Metropolitan", I have a "Blend" command button that takes you to the worksheet, "Blend Rates" that holds all the pastes values.
Private Sub Blend_Click()
MsgBox ("Cycle Through all rate periods before proceeding")
Sheets("Blend Rates").Select
Sheets("Blend Rates").Range("B6").Select
Blend.AutoSize = False
Blend.AutoSize = True
Blend.Height = 25.5
Blend.Left = 213
Blend.Width = 105
End Sub

Is it easier to make this command button do the Loop?
Screen shot:
REI LU26 Labor Rate Worksheet - test.5.xlsm
ABALAM
1
2Metropolitan
3Nov 20
40%
5ClassificationBase RateTotal RateProfit on Total Rate
6GF$ 58.00$ 92.50$ 92.50
7GF 10% Shift$ 63.80$ 99.54$ 99.54
8GF 15% Shift$ 66.70$103.06$103.06
9GF Overtime$ 87.00$127.71$127.71
10GF Overtime + 10% Shift$ 95.70$138.27$138.27
11GF Overtime + 15% Shift$ 100.05$143.56$143.56
12GF Double Time$ 116.00$162.92$162.92
Metropolitan
Cell Formulas
RangeFormula
B6B6=B$62+10
B7,B10B7=B6*1.1
B8,B11B8=B6*1.15
B9B9=B6*1.5
B12B12=B6*2
AL6:AL12AL6=SUM(U6:AK6)
AM6:AM12AM6=SUM(AL6*$AM$4)+AL6
Cells with Data Validation
CellAllowCriteria
B2List=Local
B3List=Period
 
Upvote 0
Sorry Xl2bb isn't capturing all the items:
Capture.PNG
 
Upvote 0
Is the code in the Metropolitan sheet module?
 
Upvote 0
You need to remove the word "period" from the code so it's just
VBA Code:
Range(Replace(Cl.Validation.Formula1, "=", ""))
in both places.
 
Upvote 0
Can you post the code you are using?
 
Upvote 0
I put this all into a much smaller workbook...Book100
Sheet 1
Book100.xlsm
ABCDE
1
2MonthOctober
3Week2
4
5BaseCom 1Com 2Total
6Joe6005575730
7Tom7005530785
8Sally800552001055
9Betty9005525980
10
Sheet1
Cell Formulas
RangeFormula
B6B6=INDEX(Months!B3:E6,MATCH($E$2,Month,0),MATCH($E$3,Person,0))
B7B7=INDEX(Months!B10:E13,MATCH($E$2,Month,0),MATCH($E$3,Person,0))
B8B8=INDEX(Months!B17:E20,MATCH($E$2,Month,0),MATCH($E$3,Person,0))
B9B9=INDEX(Months!B24:E27,MATCH($E$2,Month,0),MATCH($E$3,Person,0))
E6:E9E6=SUM(B6:D6)
Named Ranges
NameRefers ToCells
Month=Months!$A$3:$A$6B6:B9
Person=Months!$B$2:$E$2B6:B9
Cells with Data Validation
CellAllowCriteria
E2List=Month
E3:E4List=Person


Months Worksheet
Book100.xlsm
ABCDE
1Joe
21234
3Sept100500900400
4October200600100500
5November300700200600
6December400800300700
7
8Tom
91234
10Sept200600100500
11October300700200600
12November400800300700
13December500900400800
14
15Sally
161234
17Sept300700200600
18October400800300700
19November500900400800
20December600100500900
21
22Betty
231234
24Sept400800300700
25October500900400800
26November600100500900
27December700200600100
Months


Blend Worksheet
Book100.xlsm
ABCDEF
1
2Week
31234
4Joe0
5Tom0
6Sally0
7Betty0
Blend
Cell Formulas
RangeFormula
F4:F7F4=IFERROR(AVERAGE(B4:E4),0)


Sheet1 VBA
VBA Code:
Private Sub Blend_Click()
    MsgBox ("Cycle Through all rate periods before proceeding")
    Sheets("Blend").Select
    Sheets("Blend").Range("B2").Select
End Sub

Sub Worksheet_Change(ByVal Target As Range)
   Dim Cl As Range, ListRng As Range
   Dim Ary As Variant
   Dim i As Long

   Set Cl = Range("E3")
   On Error Resume Next
   Set ListRng = Range(Replace(Cl.Validation.Formula1, "=", ""))
   On Error GoTo 0
  
   If ListRng Is Nothing Then
      MsgBox "No validation"
      Exit Sub
   End If
   ReDim Ary(1 To ListRng.Rows.Count)

   For i = 1 To ListRng.Rows.Count
       Ary(i) = Range(Replace(Cl.Validation.Formula1, "=", "")).Cells(i, 1)
   Next i
   For i = 1 To UBound(Ary)
      Cl.Value = Ary(i)
      Application.Calculate
      Call PasteData
   Next i
   'Set Target = Range("E3")
    'If Target.Value = "1" Then
        'Call PasteData
    'End If
    'Set Target = Range("E3")
    'If Target.Value = "2" Then
        'Call PasteData
    'End If
    'Set Target = Range("E3")
    'If Target.Value = "3" Then
        'Call PasteData
    'End If
    'Set Target = Range("E3")
    'If Target.Value = "4" Then
        'Call PasteData
    'End If
End Sub
Sub PasteData()
    If Worksheets("Sheet1").Range("E3").Value = 1 Then
        Worksheets("Sheet1").Range("E6:E9").Copy
        Worksheets("Blend").Range("B4:B7").PasteSpecial Paste:=xlPasteValues
        Application.CutCopyMode = False
    End If
    If Worksheets("Sheet1").Range("E3").Value = 2 Then
        Worksheets("Sheet1").Range("E6:E9").Copy
        Worksheets("Blend").Range("C4:C7").PasteSpecial Paste:=xlPasteValues
        Application.CutCopyMode = False
    End If
    If Worksheets("Sheet1").Range("E3").Value = 3 Then
        Worksheets("Sheet1").Range("E6:E9").Copy
        Worksheets("Blend").Range("D4:D7").PasteSpecial Paste:=xlPasteValues
        Application.CutCopyMode = False
    End If
    If Worksheets("Sheet1").Range("E3").Value = 4 Then
        Worksheets("Sheet1").Range("E6:E9").Copy
        Worksheets("Blend").Range("E4:E7").PasteSpecial Paste:=xlPasteValues
        Application.CutCopyMode = False
    End If
       
End Sub
 
Last edited by a moderator:
Upvote 0

Forum statistics

Threads
1,214,985
Messages
6,122,605
Members
449,089
Latest member
Motoracer88

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