在单元格中的值更改时将数据插入同一行 – excel – vba
我目前有一个Excel文件,有一些VBA代码。 这段代码从SQL和VFP中检索信息,并在列“A”的每个单元格中填充一个下拉列表,除了A1 – 这是一个标题。 我需要在用户从“A”列中的下拉列表中select值的同一行填充“G”列…
我相信我需要的领域是
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
,这是在工作表excel对象。
以下是我想要做的事情。
If cell "a2".valuechanged then Set "g2" = "8000" End if If cell "a3".valueChanged then Set "g3" = "8000" End if
上面的代码显然不起作用,但我认为这很容易理解。 我想使这个dynamic,所以我没有太多的代码行…
任何帮助表示赞赏。
我已经解释了在使用Worksheet_Change
HERE时需要注意的事件和其他事情
您需要使用与Worksheet_Change
Intersect
来检查用户所做更改的单元格。
这是你正在尝试?
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
尝试这个
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
你可不可以在G列中加上if语句,如
如果(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
谢谢罗斯