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以特殊方式复制特定范围?_Excel_Vba - Fatal编程技术网

Excel 当带有公式的单元格发生更改时,如何使用VBA以特殊方式复制特定范围?

Excel 当带有公式的单元格发生更改时,如何使用VBA以特殊方式复制特定范围?,excel,vba,Excel,Vba,我使用过这段代码,但实际情况是,该范围被复制并粘贴了多次,最后只得到了更改后的数字 此工作簿代码 Private Sub Workbook_Open() prevval = Sheet1.Range("O133").Value End Sub 工作表代码 Private Sub Worksheet_Calculate() If Range("O133").Value <> prevval Then Call movetoleft prevval = Range

我使用过这段代码,但实际情况是,该范围被复制并粘贴了多次,最后只得到了更改后的数字

此工作簿代码

Private Sub Workbook_Open()
prevval = Sheet1.Range("O133").Value
End Sub
工作表代码

Private Sub Worksheet_Calculate()


If Range("O133").Value <> prevval Then

    Call movetoleft
    prevval = Range("O133").Value


End If
End Sub

Private Sub movetoleft()
Range("C139:Q142").Select
Selection.Copy
Range("B139").Select
ActiveSheet.PasteSpecial Format:=3, Link:=1, displayasicon:=False, iconfilename:=False

End Sub
因此,单元格Q140、Q141和Q142包含一个使用O133中的值的公式(这是一个带有公式的单元格)-因此我需要这些单元格的值考虑到(以任何方式)O133的值的变化。其余的都必须复制并粘贴在这些(Q140、Q141和Q142)单元格左侧的一个位置。在这个范围的顶部单元格(C139到Q139)中,日期的形式为(2013年5月31日,2013年6月30日,…,2014年8月31日),我想忘记2013年5月31日(C139单元格)的数据,将其余的数据移到左边一个位置,这样在O133发生变化后(因为新月份的数据——在本例中是9月份的数据)我们会将顶部单元格更改为2013年6月30日、2013年7月31日、2014年9月30日

这段特定代码的情况是,当由于O133中的值发生变化而调用movetoleft子语句时,它不会在“end子语句”中结束,而是直接返回if语句内部并重复多次。最后,我给出了(2014年9月30日,2014年9月30日,…,2014年9月30日)的数据

我非常感谢你的帮助。我发现每个月手工做这些报告真的很累。这就是为什么我决定学习VBA,也可能学习其他语言

平静下来


Aleš

编辑代码:

工作簿打开:

Private Sub Workbook_Open()
prevval = Sheet1.Range("O133").Value
MsgBox "At startup_prevval = " & prevval     'inactive like this : single quote before line start
End Sub
工作表计算:

Private Sub Worksheet_Calculate()
MsgBox "before calculate_prevval = " & prevval     'inactive like this : single quote before line
If Range("O133").Value <> prevval Then
'Call movetoleft
Call movetoleft2                           'call movetoleft or movetoleft2 : check which one works
prevval = Range("O133").Value
End If
MsgBox "after calculate_prevval = " & prevval      'inactive like this : single quote before line
End Sub

事情有点不清楚。O133单元格本身有一个公式。当此单元格值更改时,其他3个单元格也会更改。将单元格复制到左移1个单元格后,Q列中会出现什么内容?发布一个很好的工作表截图,如果可能的话,澄清更多,以获得建议性的度量。Q列将反映新的月度数据(在本例中为9月)。例如,Q141=O133是Q141单元格的公式,因此该单元格尤其是在将范围粘贴到左侧一个位置后,将具有新更改的O133值。我不能发布图片,因为这是我的第一篇文章,我没有10岁的名声。我是否可以以任何其他方式向您发送屏幕截图?您可以上载到dropbox或sendspace或任何类似的免费文件存储区域,并在此处发布链接。看来,将Public prevval设置为整数的问题在于,如果工作表中的任何单元格发生更改,它都会执行此过程。你所说的“它完成了程序”是什么意思。你的问题解决了吗?整个过程。因此,当它复制和粘贴范围时,它会被记录为值的另一个变化,因此它会一次又一次地复制和粘贴,。。。您可以在untitled.png in dropbox中找到打印屏幕。所以我的问题不是通过你的代码解决的。这让事情变得有点糟糕,不应该如此。我尝试并测试了这个。见附图。你所说的“注意到另一个价值变化”是什么意思。当带有公式的单元格更新其值时,会发生工作表_计算。但这并不意味着每次调用movetoleft。使用msgbox,您需要检查prevval是否正在按应有的方式更改。请参阅我的附加图像。
Private Sub Worksheet_Calculate()
MsgBox "before calculate_prevval = " & prevval     'inactive like this : single quote before line
If Range("O133").Value <> prevval Then
'Call movetoleft
Call movetoleft2                           'call movetoleft or movetoleft2 : check which one works
prevval = Range("O133").Value
End If
MsgBox "after calculate_prevval = " & prevval      'inactive like this : single quote before line
End Sub
Public prevval As Double   'it seems double is good for your current data

Sub movetoleft()
MsgBox "movetoleft is executing once"    'inactive like this : single quote before line
Range("C139:Q142").Select
Selection.Copy
Range("B139").Select
ActiveSheet.PasteSpecial Format:=3, Link:=1, displayasicon:=False, iconfilename:=False
End Sub

Sub movetoleft2()
 MsgBox "movetoleft2 is executing once"    'inactive like this : single quote before line
 Range("C139:Q142").Copy
 Range("B139").PasteSpecial Paste:=xlPasteValues
End Sub