[英]Excel how copy row of data from sheet 1 to sheet 2 if a cell <= Value
I'm trying to put together an Excel sheet that will help track when services are due for certain vehicles. 我正在尝试整理一张Excel表,以帮助跟踪某些车辆的服务到期时间。
The idea is that the sheet will automatically track how many days until a service is needed. 这个想法是工作表将自动跟踪需要服务的天数。 I have conditional formatting on the countdown column that if it displays a number less than or equal to 5 it turns red. 我在倒计时列上有条件格式,如果它显示的数字小于或等于5则变为红色。 I would like to take this a step further so that any vehicle displaying 5 or less days gets copied to sheet 2. 我想更进一步,以便将显示5天或更少天数的所有车辆复制到工作表2中。
So if the countdown column displays 5 or less days I would like it to copy some of the columns in the row to Sheet 2 and continue function (the countdown still ticks down). 因此,如果倒计时列显示5天或更短的时间,我希望它将行中的一些列复制到工作表2并继续功能(倒计时仍然会下降)。
If that isn't possible the entire row would be fine also. 如果不可能的话,整行也可以。
This way any vehicle that needs a service is displayed in the same area. 这样,任何需要服务的车辆都显示在同一区域。
Any help would be greatly appreciated. 任何帮助将不胜感激。
I'm not VBA expert but you can try this for starter. 我不是VBA专家,但你可以试试这个首发。
Paste this code in Alt+F11 -> VBAProject -> Microsoft Excel Objects -> Sheet1 (Sheet1) 将此代码粘贴到Alt + F11 - > VBAProject - > Microsoft Excel对象 - > Sheet1(Sheet1)
Private Sub Worksheet_Change(ByVal Target As Range)
'Declarations
Dim c1 As Object
Dim c2 As Object
Dim countdown As Integer
Dim rego As String
Dim duedt As String
Application.ScreenUpdating = False
Worksheets("Sheet2").Range("A2:C500").Clear
'Copy row
For Each c1 In Worksheets("Sheet1").Range("E3:E500").Cells
If c1.Value <= 5 And c1.Value <> 0 Then
countdown = c1.Value
rego = c1.Offset(0, -4).Value
duedt = c1.Offset(0, -1).Value
For Each c2 In Worksheets("Sheet2").Range("C2:C500").Cells
If c2.Value = "" Then
c2.Value = countdown
c2.Offset(0, -2).Value = rego
c2.Offset(0, -1).Value = duedt
Exit For
End If
Next c2
End If
If c1.Value = 0 Then
Exit For
End If
Next c1
Application.ScreenUpdating = True
End Sub
I am assuming 'Countdown' won't have '0' value and you will have maximum 500 entries. 我假设'倒计时'不会有'0'值,你将有最多500个条目。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.