在excel中记录每次迭代的数据

在excel中记录每次迭代的数据,excel,vba,Excel,Vba,我迫切需要解决我长期悬而未决的问题,如下所示 基于可编辑的选择(可以在我的工作表中设置的几个单元格中完成),单元格C28到F28不断根据选择计算答案。每次我更改可编辑选择时,这些单元格(C28到F28)中的值都会更改。我想在每次进行最后一次更改之前保留这些单元格中最后一个编号的记录。然后,我需要显示以前的编号和当前更改编号之间的关系(百分比),这很容易 我最好不要使用VBA来做这件事,因为我以前从未使用过它,因此不舒服。但是如果VBA是解决这个问题的唯一方法,那就这样吧! 如果这样做的话,这个工

我迫切需要解决我长期悬而未决的问题,如下所示

基于可编辑的选择(可以在我的工作表中设置的几个单元格中完成),单元格C28到F28不断根据选择计算答案。每次我更改可编辑选择时,这些单元格(C28到F28)中的值都会更改。我想在每次进行最后一次更改之前保留这些单元格中最后一个编号的记录。然后,我需要显示以前的编号和当前更改编号之间的关系(百分比),这很容易

我最好不要使用VBA来做这件事,因为我以前从未使用过它,因此不舒服。但是如果VBA是解决这个问题的唯一方法,那就这样吧! 如果这样做的话,这个工作表需要发送给许多用户,所以,无论是什么窍门-需要为任何用户工作,一旦文件被邮寄

谢谢,

这不是VBA的答案:

在进行更改之前,您需要复制范围(C28 toF28)并仅使用图纸某些部分中的值粘贴特殊值

你会有你所有计算的记录

使用VBA解决方案编辑:

Dim valueOfC28 As Long
Dim valueOfD28 As Long
Dim valueOfE28 As Long
Dim valueOfF28 As Long

Private Sub Worksheet_Change(ByVal Target As Range)

If Target.AddressLocal = Range("C23").AddressLocal Then
    Range("C29").Value = valueOfC28
End If
If Target.AddressLocal = Range("D23").AddressLocal Then
    Range("D29").Value = valueOfD28
End If
If Target.AddressLocal = Range("E23").AddressLocal Then
    Range("E29").Value = valueOfE28
End If
If Target.AddressLocal = Range("F23").AddressLocal Then
    Range("F29").Value = valueOfF28
End If


End Sub

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

If Target.AddressLocal = Range("C23").AddressLocal Then
    valueOfC28 = Range("C28").Value
End If
If Target.AddressLocal = Range("D23").AddressLocal Then
    valueOfD28 = Range("D28").Value
End If
If Target.AddressLocal = Range("E23").AddressLocal Then
    valueOfE28 = Range("E28").Value
End If
If Target.AddressLocal = Range("F23").AddressLocal Then
    valueOfF28 = Range("F28").Value
End If
End Sub
如果我理解正确,您的问题如图所示:

1。您需要进入Visual Basic编辑器(Alt+F11)(您将得到如下结果):

Dim valueOfC28 As Long
Dim valueOfD28 As Long
Dim valueOfE28 As Long
Dim valueOfF28 As Long

Private Sub Worksheet_Change(ByVal Target As Range)

If Target.AddressLocal = Range("C23").AddressLocal Then
    Range("C29").Value = valueOfC28
End If
If Target.AddressLocal = Range("D23").AddressLocal Then
    Range("D29").Value = valueOfD28
End If
If Target.AddressLocal = Range("E23").AddressLocal Then
    Range("E29").Value = valueOfE28
End If
If Target.AddressLocal = Range("F23").AddressLocal Then
    Range("F29").Value = valueOfF28
End If


End Sub

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

If Target.AddressLocal = Range("C23").AddressLocal Then
    valueOfC28 = Range("C28").Value
End If
If Target.AddressLocal = Range("D23").AddressLocal Then
    valueOfD28 = Range("D28").Value
End If
If Target.AddressLocal = Range("E23").AddressLocal Then
    valueOfE28 = Range("E28").Value
End If
If Target.AddressLocal = Range("F23").AddressLocal Then
    valueOfF28 = Range("F28").Value
End If
End Sub

2。然后,您需要双击要执行此代码的工作表名称

3。复制此代码并将其粘贴到右侧窗口中,如下所示:

Dim valueOfC28 As Long
Dim valueOfD28 As Long
Dim valueOfE28 As Long
Dim valueOfF28 As Long

Private Sub Worksheet_Change(ByVal Target As Range)

If Target.AddressLocal = Range("C23").AddressLocal Then
    Range("C29").Value = valueOfC28
End If
If Target.AddressLocal = Range("D23").AddressLocal Then
    Range("D29").Value = valueOfD28
End If
If Target.AddressLocal = Range("E23").AddressLocal Then
    Range("E29").Value = valueOfE28
End If
If Target.AddressLocal = Range("F23").AddressLocal Then
    Range("F29").Value = valueOfF28
End If


End Sub

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

If Target.AddressLocal = Range("C23").AddressLocal Then
    valueOfC28 = Range("C28").Value
End If
If Target.AddressLocal = Range("D23").AddressLocal Then
    valueOfD28 = Range("D28").Value
End If
If Target.AddressLocal = Range("E23").AddressLocal Then
    valueOfE28 = Range("E28").Value
End If
If Target.AddressLocal = Range("F23").AddressLocal Then
    valueOfF28 = Range("F28").Value
End If
End Sub

代码:

Dim valueOfC28 As Long
Dim valueOfD28 As Long
Dim valueOfE28 As Long
Dim valueOfF28 As Long

Private Sub Worksheet_Change(ByVal Target As Range)

If Target.AddressLocal = Range("C23").AddressLocal Then
    Range("C29").Value = valueOfC28
End If
If Target.AddressLocal = Range("D23").AddressLocal Then
    Range("D29").Value = valueOfD28
End If
If Target.AddressLocal = Range("E23").AddressLocal Then
    Range("E29").Value = valueOfE28
End If
If Target.AddressLocal = Range("F23").AddressLocal Then
    Range("F29").Value = valueOfF28
End If


End Sub

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

If Target.AddressLocal = Range("C23").AddressLocal Then
    valueOfC28 = Range("C28").Value
End If
If Target.AddressLocal = Range("D23").AddressLocal Then
    valueOfD28 = Range("D28").Value
End If
If Target.AddressLocal = Range("E23").AddressLocal Then
    valueOfE28 = Range("E28").Value
End If
If Target.AddressLocal = Range("F23").AddressLocal Then
    valueOfF28 = Range("F28").Value
End If
End Sub
我希望这会有帮助

编辑您需要的收藏:

Dim valueOfC28 As Long
Dim valueOfD28 As Long
Dim valueOfE28 As Long
Dim valueOfF28 As Long

Private Sub Worksheet_Change(ByVal Target As Range)

If Target.AddressLocal = Range("C23").AddressLocal Then
    Range("C29").Value = valueOfC28
End If
If Target.AddressLocal = Range("D23").AddressLocal Then
    Range("D29").Value = valueOfD28
End If
If Target.AddressLocal = Range("E23").AddressLocal Then
    Range("E29").Value = valueOfE28
End If
If Target.AddressLocal = Range("F23").AddressLocal Then
    Range("F29").Value = valueOfF28
End If


End Sub

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

If Target.AddressLocal = Range("C23").AddressLocal Then
    valueOfC28 = Range("C28").Value
End If
If Target.AddressLocal = Range("D23").AddressLocal Then
    valueOfD28 = Range("D28").Value
End If
If Target.AddressLocal = Range("E23").AddressLocal Then
    valueOfE28 = Range("E28").Value
End If
If Target.AddressLocal = Range("F23").AddressLocal Then
    valueOfF28 = Range("F28").Value
End If
End Sub
用现有代码替换此代码:

Dim valueOfC28 As Long
Dim valueOfD28 As Long
Dim valueOfE28 As Long
Dim valueOfF28 As Long

Private Sub Worksheet_Change(ByVal Target As Range)
Dim inc As Integer
Dim flag As Boolean
Dim OLDvalueOfC29 As Long
Dim OLDvalueOfD29 As Long
Dim OLDvalueOfE29 As Long
Dim OLDvalueOfF29 As Long
Dim SheetName As String
flag = False
OLDvalueOfC29 = Range("C29").Value
OLDvalueOfD29 = Range("D29").Value
OLDvalueOfE29 = Range("E29").Value
OLDvalueOfF29 = Range("F29").Value
If Target.AddressLocal = Range("C23").AddressLocal Then
    flag = True
    Range("C29").Value = valueOfC28
End If
If Target.AddressLocal = Range("D23").AddressLocal Then
    flag = True
    Range("D29").Value = valueOfD28
End If
If Target.AddressLocal = Range("E23").AddressLocal Then
    flag = True
    Range("E29").Value = valueOfE28
End If
If Target.AddressLocal = Range("F23").AddressLocal Then
    flag = True
    Range("F29").Value = valueOfF28
End If
If Target.AddressLocal = Range("C23:F23").AddressLocal Then
    flag = True
    Range("C29").Value = valueOfC28
    Range("D29").Value = valueOfD28
    Range("E29").Value = valueOfE28
    Range("F29").Value = valueOfF28
End If

If flag = True Then
    inc = 1
    Do While Range("K" & inc).Value <> ""
        inc = inc + 1
    Loop
    Range("K" & inc) = OLDvalueOfC29
    Range("L" & inc) = OLDvalueOfD29
    Range("M" & inc) = OLDvalueOfE29
    Range("N" & inc) = OLDvalueOfF29
End If
End Sub

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

If Target.AddressLocal = Range("C23").AddressLocal Then
    valueOfC28 = Range("C28").Value
End If
If Target.AddressLocal = Range("D23").AddressLocal Then
    valueOfD28 = Range("D28").Value
End If
If Target.AddressLocal = Range("E23").AddressLocal Then
    valueOfE28 = Range("E28").Value
End If
If Target.AddressLocal = Range("F23").AddressLocal Then
    valueOfF28 = Range("F28").Value
End If

If Target.AddressLocal = Range("C23:F23").AddressLocal Then
    valueOfC28 = Range("C28").Value
    valueOfD28 = Range("D28").Value
    valueOfE28 = Range("E28").Value
    valueOfF28 = Range("F28").Value
End If
End Sub
尺寸值C28的长度
变暗值为28倍长
变暗值为28倍长
变暗值Off28的长度相同
私有子工作表_更改(ByVal目标作为范围)
作为整数的Dim公司
将标志变暗为布尔值
暗淡的旧值等于29的长度
变暗的旧值等于29的长度
Dim OLD值为29的长度
变暗OLDvalueOfF29的长度为
将SheetName设置为字符串
flag=False
OLDvalueOfC29=范围(“C29”)。值
OLDvalueOfD29=范围(“D29”)。值
OLDvalueOfE29=范围(“E29”)。值
OLDvalueOfF29=范围(“F29”)。值
如果Target.AddressLocal=范围(“C23”).AddressLocal,则
flag=True
范围(“C29”)。值=28的值
如果结束
如果Target.AddressLocal=范围(“D23”).AddressLocal,则
flag=True
范围(“D29”)。值=值228
如果结束
如果Target.AddressLocal=范围(“E23”).AddressLocal,则
flag=True
范围(“E29”)。值=28的值
如果结束
如果Target.AddressLocal=范围(“F23”).AddressLocal,则
flag=True
范围(“F29”)。值=值28
如果结束
如果Target.AddressLocal=范围(“C23:F23”).AddressLocal,则
flag=True
范围(“C29”)。值=28的值
范围(“D29”)。值=值228
范围(“E29”)。值=28的值
范围(“F29”)。值=值28
如果结束
如果flag=True,则
inc=1
Do While范围(“K”和inc).值“”
inc=inc+1
环
范围(“K”&inc)=旧值F29
范围(“L”&inc)=旧值OFD29
范围(“M”&inc)=旧值29
范围(“N”&inc)=旧值OFF29
如果结束
端接头
专用子工作表\u选择更改(ByVal目标作为范围)
如果Target.AddressLocal=范围(“C23”).AddressLocal,则
VALUEOF28=范围(“C28”)。值
如果结束
如果Target.AddressLocal=范围(“D23”).AddressLocal,则
VALUEOF28=范围(“D28”)。值
如果结束
如果Target.AddressLocal=范围(“E23”).AddressLocal,则
valueOfE28=范围(“E28”)。值
如果结束
如果Target.AddressLocal=范围(“F23”).AddressLocal,则
valueOfF28=范围(“F28”)。值
如果结束
如果Target.AddressLocal=范围(“C23:F23”).AddressLocal,则
VALUEOF28=范围(“C28”)。值
VALUEOF28=范围(“D28”)。值
valueOfE28=范围(“E28”)。值
valueOfF28=范围(“F28”)。值
如果结束
端接头

不幸的是,这不是我所看到的。因为自动化和给用户选择的实时反馈的整个想法已经失去了。用户不愿意在每次看到编号变化时都进行此复制粘贴,并且希望看到与以前编号的百分比差异。。。。我认为应该有一个出路,要么通过VBA,要么我不知道,甚至可能是透视表。任何进一步的帮助都将不胜感激。非常感谢您为我起草了完整的代码以及实现它的方法,非常感谢。我试图完全复制你说的操作方式,但是,我的细胞从C29到F29自动分配了0号,我相信是由于代码。在那之后,无论我对单元格C23:F23做了什么更改,它都在C29:F29中保留了0。您能否进一步帮助找出错误。另外,代码是如何正常工作的?每次C23:F23发生变化时,它都会自动更新C29:F29中的值,这才是真正的情况!我也希望撤销功能能在代码中正常工作。@Ranup Rathi,第一个问题。您是否链接C23->C28,D23->D28…F23->F28,例如在C28单元格中写入“=C23”?如果不这样做,您将在单元格中获得0。对于第二个问题的第二部分,我的答案是“撤消”不适用于任何宏命令。如果您有这样的问题,您需要收集每个更改并写入预定义的行,以便以后可以访问它们。