简体   繁体   English

Excel如果单元格<=值,如何将数据行从工作表1复制到工作表2

[英]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.

相关问题 复制并粘贴 excel 表,要在代码中引用的表单元格数据的值 - Copy and Paste a excel sheet , Value of Sheet Cell Data to be referenced in code 如何根据单元格值将行内的范围从一个 Excel 工作表复制到另一个 - How to copy a range within a row from one excel sheet to another based on a cell value Excel宏可根据单元格值将一行中的选定单元格从一张纸复制到另一张纸 - Excel Macro to copy select cells from a row from one sheet to another based upon a cell value 如何将数据从工作表复制到空行上的另一个工作表 - How to copy data from sheet to another sheet on empty row 根据单元格值将特定数据(不是整行!)复制到另一张表 - Copy Specific data (Not the entire row!) to another sheet based on cell value Excel 宏在工作表中查找单元格并将整行复制到另一个工作表中 - Excel macro to find a cell in a sheet and copy the entire row in another sheet Excel:值表1(在表2中查找)将值复制到单元格旁边 - Excel : Value Sheet 1 (Find at Sheet 2) Copy the Value next to the cell Excel单元格匹配 - 从工作表复制其他数据 - Excel Cell Match - Copy other data from sheet 如何从工作表复制到另一个工作表Excel? - How to copy from a sheet to another sheet excel? Excel 使用 VBA 将单元格数据从一张纸复制到另一张纸 - Excel Copy Cell Data from one sheet to another using VBA
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM