![](/img/trans.png)
[英]How Does One Combine two Private Sub Worksheet_Change(ByVal Target As Range) in One Work Sheet?
[英]How to use Multiple Private Sub Worksheet_Change(ByVal Target As Range) in one sheet
嗨團隊,請任何人都可以幫助我,當我在我的工作表中使用此代碼時
Option Explicit
Private Sub ComboBox1_GotFocus()
ComboBox1.ListFillRange = "DropDownList"
Me.ComboBox1.DropDown
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
On Error Resume Next
If Target.Column = 2 And (Target.Row > 18 And Target.Row < 39) Then
Sheet12.[F5] = ActiveCell.Row
End If
End Sub
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
On Error Resume Next
If Target.Column = 2 And (Target.Row > 18 And Target.Row < 39) Then
Sheet12.[F5] = ActiveCell.Row
End If
End Sub
**它完美地工作,我沒有收到任何錯誤或消息,
當我在我的工作表中單獨使用此代碼時**
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rngCell As Range, m, v
Dim rngCell1 As Range, m1, v1
Check1:
If Application.Intersect(Target, Range("B19:B38")) Is Nothing Then GoTo Check2:
For Each rngCell In Range("B19:B38")
v = rngCell.Value
If Len(v) > 0 Then
'See if the value is in your lookup table
m = Application.VLookup(v, _
ThisWorkbook.Sheets("ItemName").Range("D2:E1001"), 2, False)
'If found a match then replace wiht the vlookup result
If Not IsError(m) Then rngCell.Value = m
End If
Next
Exit Sub
Check2:
If Application.Intersect(Target, Range("A6,D6")) Is Nothing Then Exit Sub
For Each rngCell1 In Range("A6,D6")
v1 = rngCell1.Value
If Len(v1) > 0 Then
'See if the value is in your lookup table
m1 = Application.VLookup(v1, _
ThisWorkbook.Sheets("PARTY LEDGER").Range("B2:C1001"), 2, False)
'If found a match then replace wiht the vlookup result
If Not IsError(m1) Then rngCell1.Value = m1
End If
Next
On Error GoTo Hell
If Target.Address(False, False) = "A6" And Target.Validation.Type = 3 Then
Range("B14:B23").Value = ""
End If
Hell:
End Sub
**它也完美地工作。 我從來沒有遇到任何問題或錯誤。 但是當我在我的工作表中使用這兩個代碼時,它給了我一個錯誤,因為兩個
Private Sub Worksheet_Change(ByVal Target As Range)
如何將這些代碼合二為一。 請幫我。 **
這是因為您不能兩次使用相同的子名稱。 但是,您可以將其中一個的內容復制到另一個 Sub 的內容之前。 然后他們被互相處決。
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
On Error Resume Next
If Target.Column = 2 And (Target.Row > 18 And Target.Row < 39) Then
Sheet12.[F5] = ActiveCell.Row
End If
On Error Goto 0
Dim rngCell As Range, m, v
Dim rngCell1 As Range, m1, v1
Check1:
If Application.Intersect(Target, Range("B19:B38")) Is Nothing Then GoTo Check2:
For Each rngCell In Range("B19:B38")
v = rngCell.Value
If Len(v) > 0 Then
'See if the value is in your lookup table
m = Application.VLookup(v, _
ThisWorkbook.Sheets("ItemName").Range("D2:E1001"), 2, False)
'If found a match then replace wiht the vlookup result
If Not IsError(m) Then rngCell.Value = m
End If
Next
Exit Sub
Check2:
If Application.Intersect(Target, Range("A6,D6")) Is Nothing Then Exit Sub
For Each rngCell1 In Range("A6,D6")
v1 = rngCell1.Value
If Len(v1) > 0 Then
'See if the value is in your lookup table
m1 = Application.VLookup(v1, _
ThisWorkbook.Sheets("PARTY LEDGER").Range("B2:C1001"), 2, False)
'If found a match then replace wiht the vlookup result
If Not IsError(m1) Then rngCell1.Value = m1
End If
Next
On Error GoTo Hell
If Target.Address(False, False) = "A6" And Target.Validation.Type = 3 Then
Range("B14:B23").Value = ""
End If
Hell:
End Sub
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.