Results 1 to 4 of 4

Thread: [RESOLVED] [EXCEL] Convert =IF(G2="","",G2/1.1) to VBA

  1. #1
    New Member
    Join Date
    May 12
    Posts
    8

    Resolved [RESOLVED] [EXCEL] Convert =IF(G2="","",G2/1.1) to VBA

    Hi There,

    I have a workbook that will be shared by a range of users with different levels of exposure to excel. I need the calculation =IF(G2="","",G2/1.1) for all of colomn H if there is a value in G, but I need it to be hidden so noone can "accidentally" delete it. I cannot protect the sheet to hide it because the sheet is constantly being manipulated.

    I have the following, which works in a test sheet to calculate GST (I actually want it to remove the GST amount from the whole number), but not in the actual workbook -

    Private Sub Worksheet_Change(ByVal Target As Range)

    Dim rng As Range, cell As Range
    On Error GoTo exit_here
    Set rng = Application.Intersect(Target, Range("A:A"))
    If Not rng Is Nothing Then
    Application.EnableEvents = False
    For Each cell In rng
    Select Case cell.Column
    Case 1
    If cell.Value > 0 Then
    cell.Offset(, 1).Value = cell.Value * 0.11
    End If
    Case 2
    If cell.Value < 0 Then
    'nothing
    End If
    End Select
    Next cell
    End If
    Application.EnableEvents = True
    Exit Sub
    exit_here:
    Application.EnableEvents = True
    End Sub

    Please help!!

    TA

  2. #2
    PowerPoster
    Join Date
    Dec 04
    Posts
    18,522

    Re: [EXCEL] Convert =IF(G2="","",G2/1.1) to VBA

    the easy way to do the calculation is when the value in column G changes, update H

    vb Code:
    1. Private Sub Worksheet_Change(ByVal Target As Range)
    2.  
    3. if target.column = 7 then  'column G
    4.   if not isempty(target) then
    5.      target.offset(,1) = target / 1.1
    6.     else
    7.      target.offset(,1) = ""
    8.   end if
    9. end if
    10.  
    11. End Sub
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3
    New Member
    Join Date
    May 12
    Posts
    8

    Re: [EXCEL] Convert =IF(G2="","",G2/1.1) to VBA

    westconn1, Thank you so much, this works beautifully!
    However, when i delete the values in "G" in bulk, I get a Run-Time error "13", Type Mismatch. How do I stop that?

  4. #4
    PowerPoster
    Join Date
    Dec 04
    Posts
    18,522

    Re: [EXCEL] Convert =IF(G2="","",G2/1.1) to VBA

    try like, but i have not tested
    Code:
    Private Sub Worksheet_Change(ByVal Target As Range)
        for each cel in target.cells 
        if cel.column = 7 then  'column G
          if not isempty(cel) then
             cel.offset(,1) = target / 1.1
            else
             cel.offset(,1) = ""
          end if
        end if
        next
        End Sub
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

Posting Permissions

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