Worksheet_Change宏 - 更改多个单元格

时间:2013-10-30 15:13:54

标签: excel vba excel-vba

我写了一个宏,虽然它有效但功能上并不是我需要的。这是一个交互式清单,可以分解机器的多个区域,如果它们正在工作,则检查它们,然后更新包含多个部分的主列表。但是,它一次只能用于一个单元格,并且它需要能够一次处理多个单元格(包括行和列)。这是我目前的代码:

'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

发生了什么 - 如果我向下拖动多个行或列的“x”,我的代码会在y = Target.Value处断开,并且只会更新我首先选择的单元格以及主列表中的对应单元格。它应该做的是,如果我将“x”拖放到多行列上,它应该更新我正在处理的工作表和主列表中的所有列。我一次只设置一个单元格的宏,我不知道如何设置它来拖放多行的“x”值

2 个答案:

答案 0 :(得分:3)

我认为您需要对For ... Each进行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

答案 1 :(得分: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