|
-
Sep 5th, 2012, 08:53 PM
#1
Thread Starter
New Member
[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
-
Sep 6th, 2012, 05:22 AM
#2
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:
Private Sub Worksheet_Change(ByVal Target As Range)
if target.column = 7 then 'column G
if not isempty(target) then
target.offset(,1) = target / 1.1
else
target.offset(,1) = ""
end if
end if
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
-
Sep 10th, 2012, 06:08 PM
#3
Thread Starter
New Member
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?
-
Sep 11th, 2012, 04:04 PM
#4
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|