2

我有从 SQL 和 VFP 检索信息并在除 A1 之外的“A”列中的每个单元格中填充下拉列表的代码 - 这是一个标题。

我需要在用户从“A”列的下拉列表中选择值的行上填充“G”列。

我相信我需要在Private Sub Worksheet_SelectionChange(ByVal Target As Range)工作表对象中。

下面是类似于我想做的事情。

If cell "a2".valuechanged then
    Set "g2" = "8000"
End if
If cell "a3".valueChanged then
    Set "g3" = "8000"
End if

上面的代码不起作用,但我认为它很容易理解。我想让这个动态,所以我没有太多的代码行。

4

4 回答 4

4

我已经解释了使用HERE时需要注意的事件和其他事项Worksheet_Change

您需要使用IntersectwithWorksheet_Change来检查用户对哪个单元格进行了更改。

这是你正在尝试的吗?

Private Sub Worksheet_Change(ByVal Target As Range)
    On Error GoTo Whoa

    '~~> Check if user has selected more than one cell
    If Target.Cells.CountLarge > 1 Then Exit Sub

    Application.EnableEvents = False

    '~~> Check if the user made any changes in Col A
    If Not Intersect(Target, Columns(1)) Is Nothing Then
        '~~> Ensure it is not in row 1
        If Target.Row > 1 Then
            '~~> Write to relevant cell in Col G
            Range("G" & Target.Row).Value = 8000
        End If
    End If

Letscontinue:
    Application.EnableEvents = True
    Exit Sub
Whoa:
    MsgBox Err.Description
    Resume Letscontinue
End Sub
于 2013-11-05T17:41:12.177 回答
1

试试这个

Private Sub Worksheet_Change(ByVal Target As Range)
  If Target.Row > 1 And Target.Column <> 7 Then
    Cells(Target.Row, "G").Value = 8000
  End If
End Sub

如果您只需要它在 A 列上触发,那么

Private Sub Worksheet_Change(ByVal Target As Range)
  If Target.Row > 1 And Target.Column = 1 Then
    Cells(Target.Row, "G").Value = 8000
  End If
End Sub
于 2013-11-05T17:41:18.927 回答
0

你能不能把 if 语句放在 G 列中,如

如果 (A1<>"", 8000,0)

其他明智的事情是这样的:

Private Sub Worksheet_Change(ByVal Target As Range)
On Error Resume Next
If Target.Column = 1 Then
If Target.Value2 <> "" Then
Target.Offset(0, 6) = "8000"
Else
Target.Offset(0, 6) = ""
End If
End If
On Error GoTo 0
End Sub

谢谢罗斯

于 2013-11-05T17:41:08.800 回答
0

我有一个类似的问题。我使用了 Siddharth Rout 的代码。我的修改允许用户在 a 列中粘贴一系列单元格(例如 A3:A6)并修改多个单元格(例如 H3:H6)。

Private Sub Worksheet_Change(ByVal Target As Range)
On Error GoTo Whoa

'~~> Check if user has selected more than one cell
If Target.Cells.CountLarge < 1 Then Exit Sub
If Target.Cells.CountLarge > 500 Then Exit Sub


Debug.Print CStr(Target.Cells.CountLarge)

Application.EnableEvents = False

Dim the_row As Range
Dim the_range As Range

Set the_range = Target

'~~> Check if the user made any changes in Col A
If Not Intersect(the_range, Columns(1)) Is Nothing Then
    For Each the_row In the_range.Rows
        '~~> Ensure it is not in row 2
        If the_row.Row > 2 Then
            '~~> Write to relevant cell in Col H
            Range("H" & the_row.Row).Value = Now
        End If
    Next
End If

Letscontinue:Application.EnableEvents = True Exit Sub Whoa:MsgBox Err.Description Resume Letscontinue End Sub

于 2019-03-11T16:02:40.653 回答