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 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

最佳答案

有很多方法可以组合两个代码。可能最好的方法是重写所有内容,但也可能不需要或没有时间或没有技能。看看这里的一些例子 - How to combine two vba codes? .

我能想到的最懒惰的方法就是简单地模仿 Worksheet_Change()事件,将目标传递给另一个私有(private)子:

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

关于excel - 将两个vba代码合二为一,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59274383/

相关文章:

excel - 从用户定义函数返回 Empty 实际上返回 0

arrays - 如何一次从用户窗体添加多个数据行到 Excel 数据库

mysql - 使用宏删除mysql表条目

excel - 根据各种下拉菜单隐藏/取消隐藏行

mysql - 如何使用php将excel中的数据导出到mysql?

VBA 循环溢出

vba - 从多单元格区域获取格式化值

mySQL,二维转一维

excel - 使用 Excel VBA 调整列表框以显示长于列表框宽度的字符串

excel - 如何将pdf中的数据复制并粘贴到excel中?