2014-11-05 99 views
0

我想在Excel中保存單元格的值。保存單元格的值

多數民衆贊成使用的代碼IM:

Dim oldval As Range 
Sub Macro0() 

Set oldval = ActiveCell 

End Sub 

Sub Macro2() 

Dim y As Integer 
Dim x As Variant 

Dim rng2 As Range 
Set rng2 = ActiveCell 

Dim rng As Range 
Set rng = ActiveCell 

If rng.Column = 8 And rng.Value <> "" Then 
    'Extract the number part of string 
    x = Split(rng2.Value, "_")(0) 
    y = Split(rng.Value, "_")(0) 

    If y < 1 Or x = "" Then Exit Sub 

    If x < y Then 
     MsgBox "Attenzione, si sta decrementando di stato un fornitore !" 
    Else 
     MsgBox "Stato cambiato con successo" 

End If 
End If 


End Sub 

我需要保存,這是在細胞就得到改變之前的值。 我試圖用代碼:

Private Sub Worksheet_Change(ByVal Target As Range) 
Call Macro1 
End Sub 




Private Sub Worksheet_SelectionChange(ByVal Target As Range) 
Call Macro0 
End Sub 

但使用不位於工作簿宏犯規因爲即時通訊工作。 我需要做一個控制後,我已經保存舊的值,並檢查它是否高於已插入的新值。 如何保存舊值? 感謝

編輯: @Jeeped 這就是它的外觀文件 File

+0

通過「Worksheet_Change」事件宏,有時可以在*它被更改後保存單元格*的值。您將不得不提供一些特定的單元格地址(在您的示例代碼中,* rng *和* rng2 *指向相同的'ActiveCell'),最好查看要保存的內容樣本。 – Jeeped 2014-11-05 17:50:49

+0

可靠地做到這一點的唯一方法是將工作表中的值存儲在另一個隱藏的('VeryHidden')工作表中。然後,您可以將新值(使用「更改」事件)與另一張紙上的值進行比較,然後在比較後更新隱藏紙張。 – 2014-11-05 18:05:48

+0

爲了在單元格被更改後獲得單元格的值,您需要使用Application.Undo然後記錄該值,然後您可以再次使用Application.Undo將更改後的值放回 – tigeravatar 2014-11-05 18:41:55

回答

1

我想我剛纔完全理解你的需要。這裏是一個變通:這樣

Sub macro1(ByVal oldVal As String, ByVal newVal As String) 
    MsgBox "The value was " & oldVal & ", but now is " & newVal 
End Sub 

在工作表模塊

在你的模塊,添加宏1(),添加以下兩個事件+聲明

Dim oldVal As String, newVal As String 'the top declaration allows you to use the variables even when the macro/method of the worksheet has finished to run 
Private Sub Worksheet_Change(ByVal Target As Range) 
    newVal = Target.Value 'this will just take the current value of the cell 
    Call macro1(oldVal,newVal) 'they are both passed to your procedure 
End Sub 
Private Sub Worksheet_SelectionChange(ByVal Target As Range) 
    oldVal = Target.Value 'this will record the value of the cell everytime you are selecting it 
End Sub 

下面是它的工作原理,將單元格的內容從「buongiorno」更改爲「buonasera」:

enter image description here

enter image description here

當然,這是一個例子,但你只需要重新適應您的代碼的解決辦法,它應該工作的罰款。 注:我剛剛發現消息框顯示的信息與編碼的語言不同,這是因爲我在測試方法時做了屏幕截圖。對不起,如果您認爲這可能會造成混淆,請提出修改建議。

+0

好吧,但它不工作在這裏:'Dim rng2 As Range Set rng2 = oldVal'因爲我需要對改變後的值 – Kiirito 2014-11-05 19:49:47

+0

@Kiirito做一個控制,我仍然沒有理解你爲什麼繼續引用對象而不是它的價值。 '設置rng2 = oldVal'意味着你攜帶着整個範圍,而不僅僅是它的價值......所以如果價值調用會讓你回到當前價值是正常的,因爲你帶着範圍對象和不是它的價值!請嘗試按照我在代碼中顯示的那樣使用示例,其中我定義了'As String'(或其他任何值數據類型),或者更改了問題標題,因爲您要求範圍的值:) – 2014-11-05 19:52:55

+0

I我會盡力去適應它。謝了哥們 !如果我不能適應它,我會打開一個新的線程與問題。 – Kiirito 2014-11-05 19:53:19

1

您需要在項目級別聲明變量。這裏有一個簡單的例子:

Dim a As String 
Sub macro1() 
    a = Range("A1") 
End Sub 
Sub macro2() 
    MsgBox a 
End Sub 

如果您運行的宏1,變量a(這是在頂層聲明)將存儲單元格的範圍(「A1」)的值。然後,如果您在第二時間運行macro2,該值將仍然存在。

您只需要在項目頂部聲明變量oldval,並在您做出更改之前將您的舊值存儲爲oldValue = ActiveCell.Value。該變量將保持該值,即使在退出宏之後,也可以使用該值在以後進行比較。

+0

我無法在同一個Workbook_change事件中使用這2個宏。它不會工作,我在哪裏把第一個宏?否則第二個? – Kiirito 2014-11-05 18:01:58

+0

如果您使用'SelectionChange'事件來存儲該值,則可能會忽略此操作,但如果用戶兩次更改同一個單元格而不移動到另一個單元格中,則此操作不起作用。 – 2014-11-05 18:03:25

+0

它似乎我不能繼續與此。它只是沒有保存它的宏之後的價值。檢查我的編輯,看看代碼即時通訊使用 – Kiirito 2014-11-05 18:10:11