如何阻止旧数据被我的宏更新?

时间:2014-05-28 10:04:20

标签: excel vba excel-vba range

我一直致力于基本上是销售的注册。我的电子表格记录了销售额,利润等,然后从中打印出收据,但是,我想保留自己的数据记录以用于图表等。此时我的宏从指定的列向上移动,从第100000行开始,然后当它到达有数据的行时,向下移动一个到空单元格。在其中,它为已找到的新数据放置单元格引用。我是VBA的新手,所以我不确定我是否采取了正确的方法,这是我的代码:

Sub DataLog()
'
' DataLog Macro
' Record sales in the data log.
'

'
Sheets("Data").Select

Dim rngd As Range
Set rngd = Range("A100000").End(xlUp).Offset(1, 0)

Dim coke As Range
Set coke = Range("C100000").End(xlUp).Offset(1, 0)

Dim creamsoda As Range
Set creamsoda = Range("D100000").End(xlUp).Offset(1, 0)

Dim lemonade As Range
Set lemonade = Range("E100000").End(xlUp).Offset(1, 0)

Dim pasito As Range
Set pasito = Range("F100000").End(xlUp).Offset(1, 0)

Dim pepsi As Range
Set pepsi = Range("G100000").End(xlUp).Offset(1, 0)

Dim pepsim As Range
Set pepsim = Range("H100000").End(xlUp).Offset(1, 0)

Dim solo As Range
Set solo = Range("I100000").End(xlUp).Offset(1, 0)

Dim sprite As Range
Set sprite = Range("J100000").End(xlUp).Offset(1, 0)

Dim sunkist As Range
Set sunkist = Range("K100000").End(xlUp).Offset(1, 0)

Dim water As Range
Set water = Range("L100000").End(xlUp).Offset(1, 0)

rngd.Value = "=Form!D1"

coke.Value = "=Form!E7"

creamsoda.Value = "=Form!E8"

lemonade.Value = "=Form!E9"

pasito.Value = "=Form!E10"

pepsi.Value = "=Form!E11"

pepsim.Value = "=Form!E12"

solo.Value = "=Form!E13"

sprite.Value = "=Form!E14"

sunkist.Value = "=Form!E15"

water.Value = "=Form!E16"

当我为宏按钮计时时,它第一次工作,第二次也是如此。虽然,当它确实在下面的行中记录第二组数据时,它会更新每个单元格中的所有先前数据。如何阻止任何先前输入的数据被更新,以便我可以保留每周的信息记录?非常感谢任何帮助或见解,谢谢:)

编辑:

我应该补充一下,我已经尝试在添加数据后锁定单元格但这只能保护它们免受手动更改,宏仍然会更新数据。

1 个答案:

答案 0 :(得分:0)

要使用值覆盖Data表单中的所有公式,请将这两行添加到脚本的底部:

Sheets("Data").Cells.Copy
Sheets("Data").Cells.PasteSpecial (xlPasteValues)

如果您想要保留公式,您只需要定义您想要覆盖的Range苏打值并遵循相同的模式:

Dim SodaData As Range
'ex below sets SodaData range from A3 to C3
Set SodaData = Sheets("Data").Range(Sheets("Data").Cells(3, 1), Sheets("Data").Cells(3, 3))
SodaData.Copy
SodaData.PasteSpecial (xlPasteValues)