如何向单元格添加值

时间:2015-05-17 07:04:40

标签: excel vba excel-vba excel-2010

我正在使用电子表格在bin中进行库存扫描 我正在尝试在(Target.Row, 5)(row 6)

旁边添加A2值

如何制作A2或A9或下一个扫描值显示在F列上,如果列E&我不知道这部分是坐在错误的垃圾箱里

我尝试了.Value = Target.Worksheet.Cells(Target.Row, 1).Value但没有工作

enter image description here

这是我的完整代码

Option Explicit
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)

    If Target.Cells.Count > 1 Or IsEmpty(Target) Or Target.Column <> 1 Then Exit Sub

    If Not SheetExists("WarehouseInventory") Then Exit Sub

    Dim Result As Variant
    Set Result = Sheets("WarehouseInventory").Cells.Range("E:E").Find(Target)

    If Result Is Nothing Then
       Target.Worksheet.Cells(Target.Row, 2) = "Data New Bin"
    Else
        Target.Worksheet.Cells(Target.Row, 2) = Result.Worksheet.Cells(Result.Row, 4)
        Target.Worksheet.Cells(Target.Row, 3) = Result.Worksheet.Cells(Result.Row, 5)
        Target.Worksheet.Cells(Target.Row, 4) = Result.Worksheet.Cells(Result.Row, 6)
        Target.Worksheet.Cells(Target.Row, 5) = Result.Worksheet.Cells(Result.Row, 7)
    End If

End Sub

Public Function SheetExists(SheetName As String) As Boolean
    Dim ws As Worksheet
    SheetExists = False

    For Each ws In ThisWorkbook.Worksheets
        If ws.Name = SheetName Then SheetExists = True
    Next ws
End Function

2 个答案:

答案 0 :(得分:1)

Target.Worksheet.Cells(Target.Row, 6).Value = Target.Worksheet.Cells(Target.Row,1).Value怎么样?

如果您只想要A2,则在所有行中将Target.Row替换为2

答案 1 :(得分:1)

这是一个如何实现所需结果的例子

Private Sub Worksheet_Change(ByVal Target As Range)
Dim x&, y&
If Target.Column = 6 Then
    y = 5: x = Target.Row
    While Cells(x, y) <> "": x = x - 1: Wend
    If Cells(Target.Row, 5) <> "" Then Cells(Target.Row, 6) = Cells(x, 1)
End If
End Sub

输出

enter image description here

更新

此处您的更新代码已经过测试,工作正常,请将其插入"Cycle Count"

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim x&, y&, Result As Range, ws As Worksheet
    Application.EnableEvents = 0
    If Target.Count > 1 Then
        Application.EnableEvents = 1
        Exit Sub
    End If
    If Not Intersect(Target, [A1:A99999]) Is Nothing Then
        With ThisWorkbook
            For Each ws In .Worksheets
                If ws.Name = "WarehouseInventory" Then
                    Set ws = .Worksheets("WarehouseInventory")
                    Set Result = ws.[E:E].Find(Target.Value)
                    Exit For
                End If
            Next ws
            If Result Is Nothing Then
                Target.Offset(, 1).Value = "Data New Bin"
                Application.EnableEvents = 1
                Exit Sub
            Else
                Target.Offset(, 1).Value = ws.Range(Result.Address).Offset(, -1).Value
                Target.Offset(, 2).Value = ws.Range(Result.Address).Value
                Target.Offset(, 3).Value = ws.Range(Result.Address).Offset(, 1).Value
                Target.Offset(, 4).Value = ws.Range(Result.Address).Offset(, 2).Value
                y = 5: x = Target.Row
                While .ActiveSheet.Cells(x, y) <> "": x = x - 1: Wend
                Target.Offset(, 5).Value = .ActiveSheet.Cells(x, 1).Value
            End If
        End With
    End If
    Application.EnableEvents = 1
End Sub

示例文件http://www.filedropper.com/book1_5