Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel用VBA替换单元格格式_Vba_Excel - Fatal编程技术网

Excel用VBA替换单元格格式

Excel用VBA替换单元格格式,vba,excel,Vba,Excel,好的,我想找出一种方法,用VBA在Excel 2007后台更改工作表中的单元格,因此,如果单元格A1和B1中有内容,则C1将自动填充答案,而不会在框中包含任何公式 我试过了 Private Sub Worksheet_Change() dim weekCost as integer dim monthCost as integer dim salesTax as integer dim totalCost as integer weekCost = Range("$A$1") monthCos

好的,我想找出一种方法,用VBA在Excel 2007后台更改工作表中的单元格,因此,如果单元格A1和B1中有内容,则C1将自动填充答案,而不会在框中包含任何公式

我试过了

Private Sub Worksheet_Change()
dim weekCost as integer
dim monthCost as integer
dim salesTax as integer
dim totalCost as integer

weekCost = Range("$A$1")
monthCost = Range("$B$2")

salesTax = (weekCost + monthCost) * .08
totalCost = weekCost + monthCost

totalCost = salesTax + totalCost

totalCost = range("$C$1")

end sub

我无法获得任何人都知道的坚守该单元格的总成本。

您的分配声明是反向的,请尝试:

Range("C1") = totalCost

最明显的修复方法是将最后一行更改为
范围($C$1”)。Value=totalCost

如果这不起作用,以下是需要考虑的其他事项:

  • 我在代码中看到的一个直接问题是,工作表\u Change sub实际上应该定义为
    私有子工作表\u Change(ByVal Target作为范围)

  • 确保函数位于要捕获事件的特定工作表的代码页中。如果将其放入常规代码模块中,则不会调用它

  • 您可能需要解决的另一个问题是,现在代码将在每次修改任何单元格时重新计算,而不仅仅是A1或B1。您可以将代码放入If语句中,以限制执行重新计算的时间


  • 工作表(“表1”)。单元格(3,1)。值=总成本

    没问题。请考虑将答案标记为“接受”,如果它解决了你的问题:)这个选项位于哪里?