简体   繁体   English

需要根据另一个工作表中的值用连续的行数据填充工作表

[英]Need to fill a sheet with consecutive rows data based on value in another worksheet

OK here's the situation. 好的,这是情况。 I've created a workbook with 5 sheets in it. 我创建了一个包含5张纸的工作簿。

The first sheet has columns for QTY, DESCRIPTION, PRICE and some other data, but those are the pertinent ones. 第一页包含用于“数量”,“描述”,“价格”和其他数据的列,但这些都是相关的。

Based on the data entered, a labor invoice and several order forms are filled out. 根据输入的数据,填写劳工发票和若干订单。 That is working correctly. 那工作正常。

The issue I am having is I also need it to fill out a sheet named Contract. 我遇到的问题是,我还需要它来填写一张名为“合同”的表格。 There are approximately 75 items on the main sheet, but the contract will never have more than 30 items. 主表上大约有75个项目,但合同永远不会超过30个项目。

I need to pull over only rows that have a non-zero value for QTY on the main sheet to the contract sheet, consecutively so that there are no blank rows until the data-set runs out of items with non zero QTY on the main sheet. 我需要连续仅将主表上的QTY值非零的行拖到合同表,以便在数据集用完主表上的QTY非零的项目用完之前没有空白行。 。

IE if there are 15 non-consecutive rows on the main sheet with non-zero values for QTY, on the Contract sheet I need the first 15 rows out of 30 to pull over QTY, DESCRIPTION, PRICE from the main worksheet rows with non-zero QTY values. IE如果主表上有15个非连续行,其QTY值非零,则在合同表上,我需要从30个表中的前15行中提取非零个QTY值。

I hope I am making sense.. It's got me stumped! 我希望我有道理..这让我感到困惑!

Thanks 谢谢

EDIT: I just realized, I need to only pull data that has non zero values for QTY AND Contract Cost to the contract sheet! 编辑:我刚刚意识到,我只需要将具有非零值的“数量”和“合同成本”数据拉到合同表即可! oops! 哎呀!

Consider the following screenshot. 考虑以下屏幕截图。 The left is the "Main" sheet, on the right is the "Contract" sheet. 左边是“主”表,右边是“合同”表。

在此处输入图片说明

Cell A2 on the "Contract" sheet has the formula “合同”表上的单元格A2具有以下公式

=IFERROR(INDEX(Main!A:A,SMALL(IF(ISNUMBER(Main!$A$1:$A$10)*(Main!$A$1:$A$10>0),ROW(Main!$A$1:$A$10),""),ROW(A1))),"")

This is an array formula and must be confirmed with Ctrl + Shift + Enter . 这是一个数组公式,必须使用Ctrl + Shift + Enter进行确认。 Then copy down and across. 然后向下复制。

Adjust cell references to reflect your scenario. 调整单元格引用以反映您的情况。 Use ranges with row numbers. 将范围与行号一起使用。 Don't use whole column references with array formulas. 不要将整个列引用与数组公式一起使用。

Edit: to return only rows where in addition to existing conditions the cost is greater than 0, use this formula, also array-entered with Ctrl-Shift-Enter. 编辑:仅返回除现有条件之外成本大于0的行,请使用此公式,并按Ctrl-Shift-Enter数组输入。

=IFERROR(INDEX(Main!A:A,SMALL(IF(ISNUMBER(Main!$A$1:$A$10)*(Main!$A$1:$A$10>0)*(Main!$c$1:$c$10>0),ROW(Main!$A$1:$A$10),""),ROW(A1))),"")

Not sure if this is exactly what you're looking for but if you want to do it in a macro, I believe something like this would work: 不知道这是否正是您要查找的内容,但是如果您想在宏中执行此操作,我相信类似的方法会起作用:

Sub test()
'
' test Macro
'

'
    Dim i As Integer
    Range("A:A").Select
    i = Application.WorksheetFunction.Count(Selection) + 1
    Sheets("Sheet2").Select
    Dim numZero As Integer
    numZero = 0
    Dim temp As Integer
    For j = 2 To i
        temp = Range("Sheet1!A" & j).Value
        If temp = 0 Then
            numZero = numZero + 1
        Else
            Range("A" & j - numZero).Select
            Selection.Formula = "=IF(Sheet1!A" & j & "<> 0, Sheet1!A" & j & ",FALSE)"
            Range("B" & j - numZero).Select
            Selection.Value = "=Sheet1!B" & j
            Range("C" & j - numZero).Select
            Selection.Value = "=Sheet1!C" & j
        End If
    Next j
End Sub

This is not the cleanest macro cause I threw it together quick but it gets the job done and should be fast enough if your data isn't gigantic. 这不是最干净的宏,因为我将其快速组合在一起,但是它可以完成工作,并且如果您的数据不是很大,则应该足够快。

Try this. 尝试这个。 I assumed that you need pull data from main to contract. 我假设您需要从主体到合同的拉动数据。 I hope i get your question right 我希望我能正确回答你的问题
Main worksheet : 主要工作表:
主要工作表
Contract worksheet (Result) 合同工作表(结果)

合同工作表

Option Explicit
Dim MyWorkbook As Workbook
Dim MyWorksheet As Worksheet
Dim MyOutputWorksheet As Worksheet

Sub PullData()
Set MyWorkbook = Workbooks(ActiveWorkbook.Name)
Set MyWorksheet = MyWorkbook.Sheets("Main")
Set MyOutputWorksheet = MyWorkbook.Sheets("Contract")

    Dim myValue As Long
    Dim RowPointer As Long

    For RowPointer = 2 To MyWorksheet.Cells(Rows.Count, "B").End(xlUp).Row
        If MyWorksheet.Range("A" & RowPointer).Value > 0 And MyWorksheet.Range("A" & RowPointer).Value <> "" Then
            If MyOutputWorksheet.Cells(Rows.Count, "B").End(xlUp).Row > 15 Then
                Exit Sub
            End If
            MyWorksheet.Range(("A" & RowPointer) & ":C" & RowPointer).Copy Destination:=MyOutputWorksheet.Range("A" & MyOutputWorksheet.Cells(Rows.Count, "B").End(xlUp).Row + 1)
        End If
    Next RowPointer


End Sub

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 根据另一个工作表中的单元格值隐藏一个工作表中的行 - Hide rows in one worksheet based on a cell value in another worksheet 根据另一张表中的数据填写公式 - Fill Down Formula based on data in another sheet 基于一张纸的过滤计数值,需要在另一张纸中插入空行 - Based on a Filtered count value from one sheet, need to insert empty rows in another sheet 根据所选数量将数据从工作表中的行复制到另一个工作表 - Copy data from rows in Worksheet to another worksheet based on quantity selected 根据特定列上的单元格值将多行添加到另一个工作表 - Adding a number of rows to another worksheet based on cell value on a certain column 使用VBA将基于单元格值的行添加到另一个工作表上 - Using VBA to Add Rows based on a cell value onto another worksheet 根据单元格值将行复制并粘贴到另一个工作表 - Copy and Paste Rows to another worksheet based on a cells value 需要根据第一库仑值将数据范围从一个Excel工作表复制到另一个工作表 - Need to copy range of data from one excel sheet to another based on first coulmn value 根据源行中的值,有条件地将一张表中的数据统计到另一张表的可变行 - Tally data from 1 sheet conditionally to variable rows of another based on a value in source row 如何根据另一个工作表中的单元格值填充工作表 - How to fill a worksheet based on cell values from another worksheet
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM