Consulting

Results 1 to 12 of 12

Thread: Solved: Calculate the cell type that value to calculate

  1. #1
    VBAX Tutor
    Joined
    Jan 2011
    Posts
    272
    Location

    Solved: Calculate the cell type that value to calculate

    Hi.
    I would like the calculations in column "C" were made in the "B".

    I type a value in B2, then B2 will result.

    The calculation will be well, when entering a value in any cell in column "B".

    will be sought in the spreadsheet "X", the value in column B of this worksheet will be multiplied by a cell in column "A".

    See the formula in column "C", to better understand.




    I tried this code but it did not work.
    [VBA]Private Sub Worksheet_Change(ByVal Target As Range)
    If Intersect(Target, Range("B2:B10")) Is Nothing Then
    Exit Sub
    Else:
    Sheets("Plan1").Range("B2").Value = Sheets("X").Range("B2:B10").Value * Sheets("
    Sheets("Plan1").Range("B2").Value = Sheets("X").Range("B2:B10").Value
    End If
    End Sub
    [/VBA]
    Attached Files Attached Files

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]
    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim pos As Long
    If Not Intersect(Target, Range("A2:B10")) Is Nothing Then

    On Error Resume Next
    pos = Application.Match(Me.Cells(Target.Row, "A").Value, Sheets("X").Range("A2:A10"), 0)
    On Error GoTo 0

    If pos > 0 Then

    Me.Cells(Target.Row, "C").Value = Me.Cells(Target.Row, "B").Value _
    * Sheets("X").Range("B2:B10").Cells(pos, 1).Value
    Else

    Me.Cells(Target.Row, "C").Value = ""
    End If
    End If
    End Sub[/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    VBAX Tutor
    Joined
    Jan 2011
    Posts
    272
    Location
    Hi
    XLD, thank you.

    I wonder how do I enter in B2, and resultdo, aparcer in B2?
    Example.

    I typed in B2 the value 10, after giving B2 enter = 120.

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    You would change

    [vba]
    Me.Cells(Target.Row, "C").Value[/vba]

    to

    [vba]
    Me.Cells(Target.Row, "B").Value[/vba]

    But you would also need to disable events at the start and re-enable at tyhe end, otherwise the code will cascade on and on forever.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  5. #5
    VBAX Tutor
    Joined
    Jan 2011
    Posts
    272
    Location
    I had tried so the result remains in C2

    Thank you!

  6. #6
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Impossible!
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  7. #7
    VBAX Tutor
    Joined
    Jan 2011
    Posts
    272
    Location
    hi.
    I had changed in the wrong place, so I tried but gave error.

    This would, if it is giving error!

    Error 28
    Not enough space to stack


    [VBA]Private Sub Worksheet_Change(ByVal Target As Range)
    Dim pos As Long
    If Not Intersect(Target, Range("A2:B10")) Is Nothing Then

    On Error Resume Next
    pos = Application.Match(Me.Cells(Target.Row, "A").Value, Sheets("Números x Nomes").Range("A2:A10"), 0)
    On Error GoTo 0

    If pos > 0 Then

    Me.Cells(Target.Row, "C").Value = Me.Cells(Target.Row, "B").Value _
    * Sheets("Números x Nomes").Range("B2:B10").Cells(pos, 1).Value
    Else

    Me.Cells(Target.Row, "C").Value = ""
    End If
    End If
    End Sub[/VBA]

  8. #8
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by xld
    But you would also need to disable events at the start and re-enable at the end, otherwise the code will cascade on and on forever.
    ... or else you will get an Error 28.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  9. #9
    VBAX Tutor
    Joined
    Jan 2011
    Posts
    272
    Location
    But you would also need to disable events at the start and re-enable at the end, otherwise the code will cascade on and on forever.
    To be honest, I do not how.
    I could not understand.

  10. #10
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,053
    Location
    What Bob has suggested, is that you enclose the active part of the code with EnableEvents code, similar to the following

    [vba]Private Sub Worksheet_Change(ByVal Target As Range)
    Dim pos As Long
    Application.EnableEvents = False
    If Not Intersect(Target, Range("A2:B10")) Is Nothing Then
    On Error Resume Next
    pos = Application.Match(Me.Cells(Target.Row, "A").Value, Sheets("Números x Nomes").Range("A2:A10"), 0)
    On Error Goto 0
    If pos > 0 Then
    Me.Cells(Target.Row, "C").Value = Me.Cells(Target.Row, "B").Value _
    * Sheets("Números x Nomes").Range("B2:B10").Cells(pos, 1).Value Else
    Me.Cells(Target.Row, "B").Value = ""
    End If
    End If
    Application.EnableEvents = True
    End Sub[/vba]
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  11. #11
    VBAX Tutor
    Joined
    Jan 2011
    Posts
    272
    Location
    Excellent, you guys are awesome.

    I loved the genius of you deal with Excel and VBA.

    Thank you very much!

    thanks to two geniuses excel, thanks!



    For some reason I can not put it as solved if someone can do I'm very grateful!

  12. #12
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,053
    Location
    Quote Originally Posted by marreco
    thanks to two geniuses excel, thanks!
    All credit should go to Bob, I just clarified his solution.
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

Posting Permissions

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