vba 更改单元格中的值时在同一行中插入数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19794905/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Insert data in same row when a value in a cell is changed
提问by John Janssen
I have code that retrieves information from SQL and VFP and populates a dropdown list in every cell in column "A" except A1 - this is a header.
我有代码可以从 SQL 和 VFP 中检索信息,并在“A”列中除 A1 之外的每个单元格中填充下拉列表 - 这是一个标题。
I need to populate the "G" column on the row where the user selects the value from a dropdown in the "A" column.
我需要在用户从“A”列的下拉列表中选择值的行上填充“G”列。
I believe I need to be in Private Sub Worksheet_SelectionChange(ByVal Target As Range)
which is in the sheet object.
我相信我需要Private Sub Worksheet_SelectionChange(ByVal Target As Range)
在工作表对象中。
Below is something similar to what I want to do.
下面是类似于我想做的事情。
If cell "a2".valuechanged then
Set "g2" = "8000"
End if
If cell "a3".valueChanged then
Set "g3" = "8000"
End if
The code above doesn't work, but I think it is easy to understand. I want to make this dynamic, so I don't have too many lines of code.
上面的代码不起作用,但我认为很容易理解。我想让这个动态,所以我没有太多的代码行。
回答by Siddharth Rout
I have already explained about events and other things that you need to take care when working with Worksheet_Change
HERE
我已经解释了在使用HERE时需要注意的事件和其他事项Worksheet_Change
You need to use Intersect
with Worksheet_Change
to check which cell the user made changes to.
您需要使用Intersect
withWorksheet_Change
来检查用户对哪个单元格进行了更改。
Is this what you are trying?
这是你正在尝试的吗?
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
回答by engineersmnky
Try this
尝试这个
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
If you only need it to fire on column A then
如果您只需要它在 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
回答by David Smolinski
I had a similar problem. I used Siddharth Rout's code. My modifications allow a user to paste a range of cells in column a (ex. A3:A6) and have multiple cells modified (ex. H3:H6).
我有一个类似的问题。我使用了 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
Letscontinue: Application.EnableEvents = True Exit Sub Whoa: MsgBox Err.Description Resume Letscontinue End Sub
回答by Ross
can you not put an if statement in column G , as in
你能不能在 G 列中放置一个 if 语句,如
If (A1<>"", 8000,0)
如果 (A1<>"", 8000,0)
Other wise something like this will get you going:
其他明智的类似这样的事情会让你前进:
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
Thanks Ross
谢谢罗斯