2013-10-30 50 views
0

我寫了一個宏,雖然它的工作,功能上它不是什麼需要。這是一個交互式清單,可以分解機器的多個區域,如果它們正在檢查它們,然後這將更新包含多個部分的主列表。但是,它一次只能處理一個單元格,並且需要能夠一次處理多個單元格(都包含行和列)。這裏是我當前的代碼:Worksheet_Change宏 - 改變多個單元格

'Updates needed: 
'  Make so more than one cell works at a time 
'  in both x and y directions 

Private Sub Worksheet_Change(ByVal Target As Excel.range) 
    Dim wb As Workbook 
    Dim mWS As Worksheet 
    Dim conName As String 
    Dim mCol As range 
    Dim mCon As Integer 
    Dim count As Long 
    Dim cell As range 
    Dim y As String 

    count = 1 
    y = "" 
    Set wb = ActiveWorkbook 
    Set mWS = wb.Sheets("Master") 
    Set mCol = mWS.range("B:B") 
    mCon = 0 


    'Selects the name of the string value in which we need to search for in master list 
    If Target.Column < 100 Then 
     ThisRow = Target.Row 
     conName = ActiveSheet.Cells(ThisRow, "B") 
     y = Target.Value 
    End If 

    'search for matching string value in master list 
    For Each cell In mCol 
     If cell.Value = conName Then 
      mCon = count 
       Exit For 
     End If 
     count = count + 1 
    Next 
    'mark as "x" in Master list 
    Dim cVal As Variant 
    Set cVal = mWS.Cells(count, Target.Column) 
    cVal.Value = y 
End Sub 

正在發生的事情 - 如果我拖累「×」多行或我的代碼中斷列在y = Target.Value,只會上更新我首先選擇的單元格,其對應主列表。它應該做的是,如果我將「x」拖放到多行列上,它應該更新我正在處理的工作表和主列表中的所有列。我一次只設置一個單元的宏,我不知道如何設置它來拖放多行的「x」值

+1

你能否解釋一下您這是什麼意思是:*它需要能夠同時與多個細胞的工作(包括與行和列)* ??可能包括更好的描述你期望發生的事情,以及發生什麼*。 –

+0

發生了什麼 - 如果我向下拖動多個行或列的「x」,我的代碼會在 處折斷y = Target.Value ,並且將僅更新首先選中的單元格及其在主列表上的對應部分。它應該做的是,如果我將「x」拖放到多行列上,它應該更新我正在處理的工作表和主列表中的所有列。我一次只爲一個單元格設置宏,我不知道如何設置它以拖放多行的「x」值。 – CinCity

+2

你有'y'作爲'String'變量,並且包含多個單元格的範圍將始終只返回左上角單元格的值,所以當你執行作爲問題一部分的'y = Target.Value'時。你需要在'Target'範圍內進行'For Each'迭代。 –

回答

3

我認爲你需要對Target按順序迭代與多個單元格一起工作。正如Michael在評論中指出的那樣,_Change事件只觸發一次,但Target反映了所有更改的單元,因此您應該可以遍歷Target範圍。我測試了使用這個簡單的事件處理程序:

Private Sub Worksheet_Change(ByVal Target As Range) 
Dim myRange As Range 
Dim myCell As Range 
Set myRange = Target 

For Each myCell In myRange.Cells 
    Debug.Print myCell.Address 
Next 

End Sub 

我不能對你的數據/工作表顯然考,但我想應該把你在正確的軌道上。

Private Sub Worksheet_Change(ByVal Target As Excel.range) 
Dim wb As Workbook 
Dim mWS As Worksheet 
Dim conName As String 
Dim mCol As range 
Dim mCon As Integer 
Dim count As Long 
Dim cell As range 
Dim y As String 

count = 1 
y = "" 
Set wb = ActiveWorkbook 
Set mWS = wb.Sheets("Master") 
Set mCol = mWS.range("B:B") 
mCon = 0 

'Add some new variables: 
Dim myRange as Range 
Dim myCell as Range 
Set myRange = Target 

Application.EnableEvents = False '## prevents infinite loop 
For each myCell in myRange.Cells 
    If myCell.Column < 100 Then 
     ThisRow = myCell.Row 
     conName = ActiveSheet.Cells(ThisRow, "B") 
     y = myCell.Value 
    End If 

    'search for matching string value in master list 
    For Each cell In mCol 
     If cell.Value = conName Then 
      mCon = count 
       Exit For 
     End If 
     count = count + 1 
    Next 
    'mark as "x" in Master list 
    Dim cVal As Variant 
    Set cVal = mWS.Cells(count, Target.Column) 
    cVal.Value = y 

Next 
Application.EnableEvents = True '## restores event handling to True 
End Sub 
+0

這會讓我走上正確的軌道,謝謝你。我會測試這個並報告給你。 – CinCity

1

您需要使用ForEach循環遍歷單元格。

此外,您可以更好地使用Selection對象,而不是Target

Private Sub Worksheet_Change(ByVal Target As Range) 

Application.EnableEvents = False 
Application.ScreenUpdating = False 
Application.Calculation = xlCalculationManual 


For Each cell In Selection 
    Debug.Print cell.Address 
Next cell 



Application.EnableEvents = True 
Application.ScreenUpdating = True 
Application.Calculation = xlCalculationAutomatic 

Exit Sub 
相關問題