第三细胞中两个细胞之间的差异 [英] Difference between two cells in third cell

查看:112
本文介绍了第三细胞中两个细胞之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个VBA脚本,计算两个单元格之间的差异,并将其放在第三个单元格中。它应该在以下情况下工作:


  1. 选择了三个单元格。

  2. VBA脚本运行。

  3. VBA脚本使用值计算单元格之间的差异

  4. 差异记录在第三(空白)单元格中。

了解这样一个VBA脚本应该能够记录下面的值的右边的单元格中的差异。



我是一个新手在vba中,所以我的vba编码取决于很多论坛上都是类似的问题。但是这次我找不到解决方案。

解决方案

尝试这样:

  Dim rng As Range,cel As Range 

设置rng =选择

如果rng.Cells.Count<> 3然后退出Sub

与Application.WorksheetFunction
如果.CountA(rng)<> 2然后退出Sub
对于每个cel在rng
如果cel.Value =然后
选择案例True
案例cel.Address = rng(1).Address
cel.Value = rng(2)-rng(3)
案例cel.Address = rng(2).Address
cel.Value = rng(1)-rng(3)
案例cel.Address = rng(3)。地址
cel.Value = rng(1)-rng(2)
结束选择
退出
结束如果
下一个
结束

没有测试soi留给你。

为simoco修改


I was trying to make a VBA script that would calculate the differene between two cells and place it in the third cell. It should work in the following situation:

  1. Three cells are selected. Two of them have values, the third one is blank.
  2. The VBA script is run.
  3. The VBA script calculates the difference between cells with values.
  4. The difference is recorded in the third (blank) cell.

As you could understand such a VBA script should be able to record the difference in a cell on the right from the values, below, on the left or above.

I'm a newbie in vba so my vba coding depends a lot on the forums were similar issues are discussed. But this time I could not find a solution.

解决方案

Try this:

Dim rng As Range, cel As Range

Set rng = Selection

If rng.Cells.Count <> 3 Then Exit Sub

With Application.WorksheetFunction
    If .CountA(rng) <> 2 Then Exit Sub
    For Each cel In rng
        If cel.Value = "" Then
            Select Case True
            Case cel.Address = rng(1).Address
                cel.Value = rng(2)-rng(3)
            Case cel.Address = rng(2).Address
                cel.Value = rng(1)-rng(3)
            Case cel.Address = rng(3).Address
                cel.Value = rng(1)-rng(2)
            End Select
            Exit For
        End If
    Next
End With

not tested soi leave it to you.
Edited for simoco

这篇关于第三细胞中两个细胞之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆