I am working on a workbook for my business.
我正在为我的业务编写工作簿。
Would like to get a Macro going to copy data from a few cells in a row (cell through C to G) of one worksheet to another worksheet row (cells A,B,C,F,G).
想要将宏从一个工作表的一行(单元格到C到G)中的几个单元格复制到另一个工作表行(单元格A,B,C,F,G)。
The catch is, they need to be only transferred if D column has data entered.
问题是,只有在D列输入数据时才需要传输它们。
I'm unable to properly use code that Alex P wrote for a somewhat similar problem:
我无法正确使用Alex P为类似问题编写的代码:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim nextRow As Long
If Not Intersect(Target, Range("D:D")) Is Nothing Then
If VBA.IsDate(Target) Then
With Worksheets("Sheet2")
nextRow = IIf(VBA.IsEmpty(.Range("A1048576").End(xlUp)), 1, .Range("A1048576").End(xlUp).Row + 1)
.Range("A" & nextRow) = Target.Offset(0, -3)
.Range("B" & nextRow) = Target.Offset(0, -1)
.Range("C" & nextRow) = Target
End With
End If
End If
End Sub
1 个解决方案
#1
0
Try this. I got this worksheet:
sheet1
尝试这个。我得到了这个工作表:sheet1
sheet 2
Applying the following code:
表2应用以下代码:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim nextRow As Long
If Not Intersect(Target, Range("D:D")) Is Nothing Then
If Not VBA.IsEmpty(Target) Or VBA.IsNull(Target) Then
With Worksheets("Sheet2")
nextRow = IIf(VBA.IsEmpty(.Range("B1048576").End(xlUp)), 1, .Range("B1048576").End(xlUp).Row + 1)
.Range("A" & nextRow) = Target.Offset(0, -1)
.Range("B" & nextRow) = Target
.Range("C" & nextRow) = Target.Offset(0, 1)
.Range("F" & nextRow) = Target.Offset(0, 2)
.Range("G" & nextRow) = Target.Offset(0, 3)
End With
End If
End If
End Sub
Try enter new line at sheet 1
Result at sheet 2
尝试在工作表1输入换行第2页的结果
Something you need take note is : make sure column D at sheet 1 is the last item to be entered, if you enter it first, and empty row will be copied.
你需要注意的是:确保第1页的D列是最后输入的项目,如果先输入,则会复制空行。
#1
0
Try this. I got this worksheet:
sheet1
尝试这个。我得到了这个工作表:sheet1
sheet 2
Applying the following code:
表2应用以下代码:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim nextRow As Long
If Not Intersect(Target, Range("D:D")) Is Nothing Then
If Not VBA.IsEmpty(Target) Or VBA.IsNull(Target) Then
With Worksheets("Sheet2")
nextRow = IIf(VBA.IsEmpty(.Range("B1048576").End(xlUp)), 1, .Range("B1048576").End(xlUp).Row + 1)
.Range("A" & nextRow) = Target.Offset(0, -1)
.Range("B" & nextRow) = Target
.Range("C" & nextRow) = Target.Offset(0, 1)
.Range("F" & nextRow) = Target.Offset(0, 2)
.Range("G" & nextRow) = Target.Offset(0, 3)
End With
End If
End If
End Sub
Try enter new line at sheet 1
Result at sheet 2
尝试在工作表1输入换行第2页的结果
Something you need take note is : make sure column D at sheet 1 is the last item to be entered, if you enter it first, and empty row will be copied.
你需要注意的是:确保第1页的D列是最后输入的项目,如果先输入,则会复制空行。