0

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

'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”值

4

2 回答 2

3

我认为您需要对 进行For ... Each迭代Target才能使用多个单元格。正如迈克尔在评论中指出的那样,该_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
于 2013-10-30T15:32:16.990 回答
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
于 2013-10-30T15:35:34.913 回答