Consulting

Results 1 to 14 of 14

Thread: Please help - Procedure too long in VBA

  1. #1
    VBAX Regular
    Joined
    Dec 2015
    Posts
    11
    Location

    Question Please help - Procedure too long in VBA

    Dear all,

    I need a help with putting my code into loop because my procedure is too long and I don't know how to do that :/. I have many cells that can be user modifiable and 5 different options (countries). Aim is that when user changes value of one cell (e.g. C14) for particular option in one sheet, that this value 'goes' into the 'Data sheet'.

    Sample of my code is below. I have many more lines like this. Thank you very much for your help!!!

    Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Address = "$C$14" Then
    If IsNumeric(Target.Value) And Range("P5") = 1 Then
    Worksheets("Data Sheet").Range("C53").Value = Target.Value
    End If
    End If
    If Target.Address = "$C$15" Then
    If IsNumeric(Target.Value) And Range("P5") = 1 Then
    Worksheets("Data Sheet").Range("C54").Value = Target.Value
    End If
    End If
    If Target.Address = "$C$16" Then
    If IsNumeric(Target.Value) And Range("P5") = 1 Then
    Worksheets("Data Sheet").Range("C55").Value = Target.Value
    End If
    End If
    If Target.Address = "$C$17" Then
    If IsNumeric(Target.Value) And Range("P5") = 1 Then
    Worksheets("Data Sheet").Range("C56").Value = Target.Value
    End If
    End If
    Last edited by Aussiebear; 12-20-2015 at 03:24 PM. Reason: Added hash tag to code

  2. #2
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,876
    this is just for the code you wrote, up to you to change it for the cells you want it to respond to:
    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim RngToProcess As Range
    Set RngToProcess = Intersect(Range("C14:C17"), Target)
    For Each cll In RngToProcess.Cells
      If IsNumeric(cll.Value) And Range("P5") = 1 Then Worksheets("Data Sheet").Range("C" & cll.Row + 39).Value = cll.Value
    Next cll
    End Sub
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  3. #3
    VBAX Regular
    Joined
    Dec 2015
    Posts
    11
    Location
    Thank yo very much for your help! Works perfectly!!!

  4. #4
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,876
    I missed something out:
    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim RngToProcess As Range
    Set RngToProcess = Intersect(Range("C14:C17"), Target)
    If Not RngToProcess Is Nothing Then
      For Each cll In RngToProcess.Cells
        If IsNumeric(cll.Value) And Range("P5") = 1 Then Worksheets("Data Sheet").Range("C" & cll.Row + 39).Value = cll.Value
      Next cll
    End If
    End Sub
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  5. #5
    VBAX Regular
    Joined
    Dec 2015
    Posts
    11
    Location
    Thank you again! Can I please ask you one more thing.

    If I want that range is e.g. c14: E17 how to write that in syntax for range ' Range("C" & cll.Row + 39) '? I am trying few things but I have an error.

  6. #6
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,876
    I don't understand, what do you want to do?
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  7. #7
    VBAX Regular
    Joined
    Dec 2015
    Posts
    11
    Location
    Now the range was c14:c17, but in the real file the range expands form c14:g17 and I don't know how to adjust above code in the part underlined below. Can you please help me?

    If IsNumeric(cll.Value) And Range("P5") = 1 Then Worksheets("Data Sheet").Range("C" & cll.Row + 39).Value = cll.Value

  8. #8
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,876
    So where do you want to put values from:
    D14
    E15
    F16
    G17

    Answer those and I should be able to discern the pattern.
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  9. #9
    VBAX Regular
    Joined
    Dec 2015
    Posts
    11
    Location
    Not really, the desired range is every cell from c14:G17 so I want that each of these cells is modifiable. I will include bit more sample code (from c to D) so maybe it will be clearer. Sorry for not explain it better.

    Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Address = "$C$14" Then
    If IsNumeric(Target.Value) And Range("P5") = 1 Then
    Worksheets("Data Sheet").Range("C53").Value = Target.Value
    End If
    End If
    If Target.Address = "$C$15" Then
    If IsNumeric(Target.Value) And Range("P5") = 1 Then
    Worksheets("Data Sheet").Range("C54").Value = Target.Value
    End If
    End If
    If Target.Address = "$C$16" Then
    If IsNumeric(Target.Value) And Range("P5") = 1 Then
    Worksheets("Data Sheet").Range("C55").Value = Target.Value
    End If
    End If
    If Target.Address = "$C$17" Then
    If IsNumeric(Target.Value) And Range("P5") = 1 Then
    Worksheets("Data Sheet").Range("C56").Value = Target.Value
    End If
    End If
    'D'
    If Target.Address = "$D$14" Then
    If IsNumeric(Target.Value) And Range("P5") = 1 Then
    Worksheets("Data Sheet").Range("D53").Value = Target.Value
    End If
    End If
    If Target.Address = "$D$15" Then
    If IsNumeric(Target.Value) And Range("P5") = 1 Then
    Worksheets("Data Sheet").Range("D54").Value = Target.Value
    End If
    End If
    If Target.Address = "$D$16" Then
    If IsNumeric(Target.Value) And Range("P5") = 1 Then
    Worksheets("Data Sheet").Range("D55").Value = Target.Value
    End If
    End If
    If Target.Address = "$D$17" Then
    If IsNumeric(Target.Value) And Range("P5") = 1 Then
    Worksheets("Data Sheet").Range("D56").Value = Target.Value
    End If
    End If
    Last edited by Aussiebear; 12-20-2015 at 03:24 PM. Reason: Added hash tag to code

  10. #10
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,876
    Quote Originally Posted by maorlov View Post
    Not really,
    ??!!

    I'm going to assume the following:
    D14 >> D53
    E15 >> E54
    F16 >> F55
    G17 >> G56

    ('cos that's all I wanted to know):
    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim RngToProcess As Range
    Set RngToProcess = Intersect(Range("C14:G17"), Target)
    If Not RngToProcess Is Nothing Then
      For Each cll In RngToProcess.Cells
        If IsNumeric(cll.Value) And Range("P5") = 1 Then Worksheets("Data Sheet").Range(cll.Offset(39).Address).Value = cll.Value
      Next cll
    End If
    End Sub
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  11. #11
    VBAX Regular
    Joined
    Dec 2015
    Posts
    11
    Location
    yes, that is what I wanted! Thank you!

  12. #12
    VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    Option Explicit
    Private Sub Worksheet_Change(ByVal Target As Range)
    If (IsNumeric(Target) And Range("P5") = 1) And _
    Not Intersect(Target, Range("C14:E17")) Is Nothing Then AdjustData Target
    End Sub
    Sub AdjustData(Rng As Range)
    Worksheets("Data Sheet").Range(Rng.Address).Offset(, 39).Value = Rng.Value
    End Sub
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  13. #13
    Private Sub Worksheet_Change(ByVal Target As Range) 
        on error resume next
        If Target * cells(5,16) = target and Not Intersect(Target, Range("C14:E17")) Is Nothing Then target.offset(39)=Target.value
    End Sub

  14. #14
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,876
    Quote Originally Posted by snb View Post
    Private Sub Worksheet_Change(ByVal Target As Range) 
        on error resume next
        If Target * cells(5,16) = target and Not Intersect(Target, Range("C14:E17")) Is Nothing Then target.offset(39)=Target.value
    End Sub
    should be:
    …Then Worksheets("Data Sheet").Range(target.offset(39).address)=Target.value
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •