Excel 将两个vba代码合并为一个

Excel 将两个vba代码合并为一个,excel,vba,Excel,Vba,我对vba和excel也不熟悉,我在excel中创建了一个数据库,这两个代码对我的项目的工作至关重要。。我想同时运行这些vba代码,你能帮我合并代码并使其工作吗?我自己试过了,但没有成功 Private Sub Worksheet_Change(ByVal Target As Range) Dim Item As String Dim SearchRange As Range Dim rFound As Range 'Don't run the macro if: 'Target is n

我对vba和excel也不熟悉,我在excel中创建了一个数据库,这两个代码对我的项目的工作至关重要。。我想同时运行这些vba代码,你能帮我合并代码并使其工作吗?我自己试过了,但没有成功

Private Sub Worksheet_Change(ByVal Target As Range)


Dim Item As String
Dim SearchRange As Range
Dim rFound As Range

'Don't run the macro if:
'Target is not a single cell:
If Target.Cells.Count > 1 Then Exit Sub
'or Target belongs to the A1.CurrentRegion:
If Not Intersect(Target, Range("A1").CurrentRegion) Is Nothing Then Exit Sub

'Avoid the endless loop:
Application.EnableEvents = False

'Looks for matches from the here first:
Set SearchRange = Range("A1:A" & Range("A1").CurrentRegion.Rows.Count)

Item = Target.Value

'Clears the Target:
Target.Value = ""

If Application.WorksheetFunction.CountIf(SearchRange, Item) > 0 Then
'There's a match already:
    Set rFound = Columns(1).Find(What:=Item, After:=Cells(1, 1) _
            , LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows _
            , SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
'Adds one to the Quantity:
        rFound.Offset(0, 2).Value = rFound.Offset(0, 2).Value + 1

Else

'Writes the value for the Barcode-list:
Range("A" & SearchRange.Rows.Count + 1).Value = Item

'Looks for the match from sheet "Inventory" column A
    With Sheets("Inventory")
        Set rFound = .Columns(1).Find(What:=Item, After:=.Cells(1, 1) _
                , LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows _
                , SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)

        On Error GoTo 0

            If Not rFound Is Nothing Then
'Writes the Product Name and puts 1 to the Quantity column:
                Range("B" & SearchRange.Rows.Count + 1).Value = rFound.Offset(0, 1).Value
                Range("C" & SearchRange.Rows.Count + 1).Value = 1
            End If
    End With
End If

'Enable the Events again:
Application.EnableEvents = True



End Sub
第二个:

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column <> 3 Then Exit Sub
If Target.Cells.Count > 1 Then Exit Sub
With Target.Offset(0, 3)
.Value = Now
.NumberFormat = "DD/MM/YYYY"
End With
End Sub
Private子工作表\u更改(ByVal目标作为范围)
如果目标为。第3列,则退出Sub
如果Target.Cells.Count>1,则退出Sub
带Target.Offset(0,3)
.Value=现在
.NumberFormat=“DD/MM/YYYY”
以
端接头

有很多方法可以组合两种代码。也许最好的方法是重写所有内容,但也可能是没有必要、没有时间或没有技能。看看这里的一些例子-

我能想到的最懒的方法就是简单地模拟
工作表\u Change()
事件,将目标传递给另一个私有子系统:

Private Sub Worksheet_Change(ByVal target As Range)

    FirstCode target
    SecondCode target

End Sub

Private Sub FirstCode(ByVal target As Range)
    Debug.Print target.Address & " from FirstCode()"
End Sub

Private Sub SecondCode(ByVal target As Range)
    Debug.Print target.Address & " from SecondCode()"
End Sub

有很多方法可以组合两种代码。也许最好的方法是重写所有内容,但也可能是没有必要、没有时间或没有技能。看看这里的一些例子-

我能想到的最懒的方法就是简单地模拟
工作表\u Change()
事件,将目标传递给另一个私有子系统:

Private Sub Worksheet_Change(ByVal target As Range)

    FirstCode target
    SecondCode target

End Sub

Private Sub FirstCode(ByVal target As Range)
    Debug.Print target.Address & " from FirstCode()"
End Sub

Private Sub SecondCode(ByVal target As Range)
    Debug.Print target.Address & " from SecondCode()"
End Sub

似乎无法让它工作,你能帮一个兄弟吗?@user12513810-如果两个事件都独立工作,那么通过复制和粘贴使用它们的代码,并将其替换到
FirstCode()
SecondCode()
-如果它们不能独立工作,但出现另一个错误,那么你需要问另一个问题,解释什么不起作用。玩得高兴根据业务逻辑,
FirstCode()
应该是
SecondCode()
,反之亦然。谢谢您的回答。嗯,代码没有任何错误,但是..只有在我手动修改单元格时,日期才会更改:(我尝试切换调用代码,但没有成功。我想做的是扫描条形码并在a列中匹配,然后在c列中写入“1”在另一个专栏中是日期。我可以用一些美元来支持你,我知道最好是自己学习,但不幸的是,我必须尽快实现这一点。@user12513810-“只有当我手动修改单元格时,日期才会改变:(我尝试切换调用代码,但没有成功。”-什么日期?这看起来像是新的。建议-拍摄一张excel数据的屏幕截图(在将其放到网上之前更改)并解释你试图在一个新问题中实现什么。似乎无法实现,你能帮助一个兄弟吗?@user12513810-如果两个事件都独立工作,那么通过复制和粘贴使用它们的代码,并在
FirstCode()
SecondCode()中替换它们
-如果它们不能独立工作,但是出现了另一个错误,那么您需要问另一个问题,解释什么不起作用。玩得开心。可能是
FirstCode()
应该是
SecondCode()
或反之亦然,基于业务逻辑。谢谢你的回答Vityata。好吧,代码没有任何错误,但是..只有手动修改单元格,日期才会更改:(我尝试切换调用代码,但不起作用。我想做的是扫描a列中的条形码和匹配项,然后在c列中写入“1”在另一个专栏中是日期。我可以用一些美元来支持你,我知道最好是自己学习,但不幸的是,我必须尽快实现这一点。@user12513810-“只有当我手动修改单元格时,日期才会改变:(我尝试切换调用代码,但没有成功。”-什么日期?这看起来像是新的。建议-拍摄一张excel数据的屏幕截图(在将其放到网上之前进行更改),并解释您在一个新问题中试图实现的目标。