2014-04-19 57 views
0

我開發了一個宏,用於檢查範圍內的值,並根據更改前的值將粗體應用於來自同一行的不同列中的某些值。 這工作正常我很高興abbout得到那個。代碼 的部分是這個vba worksheet_change爲不同工作表中的單元格

Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range) 

昏暗KeyCells作爲範圍

Dim valorActual As Long 

    Dim valorAntic As Long 

Set KeyCells = Range("BF3:BF378") 

If Not Application.Intersect(KeyCells, Range(Target.Address)) _ 
     Is Nothing Then 



    valorActual = Target.Value 
valorAntic = Rows(Target.Row).Columns("BG").Value 


    If (valorActual > 4 And valorAntic < 5) Or (valorActual > 8 And valorAntic < 9) Or (valorActual > 12 And valorAntic < 13) Or (valorAntic > 4 And valorActual < 5) Or (valorAntic > 8 And valorActual < 9) Or (valorAntic > 12 And valorActual < 13) Then 

    Rows(Target.Row).Columns("C").Font.Bold = True 
    Rows(Target.Row).Columns("C").Font.Underline = True 
    Rows(Target.Row).Columns("R").Font.Bold = True 
    Rows(Target.Row).Columns("R").Font.Underline = True 
    End If 

的問題是,現在這個工作,因爲它應該,我想從另一個片檢查。 換句話說, 直到現在我唯一要檢查的是列bf。如果有新的價值,我會檢查它。但是在部署的那一刻,我意識到這個列已經改變了,因爲其他工作表中的另一個已經被更改過了。 我的意思是真正的變化是在另一張紙上,而其內部的單元格的列是 「= anotherSheet!thecellchanged」 ,如果這個「另一張紙」發生了變化,原始工作表中的宏就不會改變它

so 是否有辦法檢查影響第三張紙的紙張的變化,並取出=「anothersheet」單元格的範圍? 感謝您的網站,並幫助提前

馬庫斯 http://www.funhelps.com

回答

0

你的代碼是ThisWorkbook代碼模塊中,因此任何引用您對您的宏將參照ActiveSheet默認範圍,除非你明確使用圖紙參考對其進行限定。

在這種情況下引用傳遞給事件處理程序的Sh參數:您應該使用在你的代碼,以確保您正在處理正確的工作表。

E.g. :

Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range) 
    Dim KeyCells As Range 
    Dim valorActual As Long 
    Dim valorAntic As Long 

    Set KeyCells = Sh.Range("BF3:BF378") 

    If Not Application.Intersect(KeyCells, Target) Is Nothing Then 

     valorActual = Target.Value 
     valorAntic = Target.EntireRow.Cells(1,"BG").Value 


    If (valorActual > 4 And valorAntic < 5) Or (valorActual > 8 And _ 
     valorAntic < 9) Or (valorActual > 12 And valorAntic < 13) Or _ 
     (valorAntic > 4 And valorActual < 5) Or (valorAntic > 8 And _ 
     valorActual < 9) Or (valorAntic > 12 And valorActual < 13) Then 

     With Sh.Rows(Target.Row) 
      .Columns("C").Font.Bold = True 
      .Columns("C").Font.Underline = True 
      .Columns("R").Font.Bold = True 
      .Columns("R").Font.Underline = True 
     End With 
    End If 
相關問題