问题
OK here's the situation. I've created a workbook with 5 sheets in it.
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.
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.
I.E.
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.
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!
回答1:
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
回答2:
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.
回答3:
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
=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. 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.
=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))),"")
来源:https://stackoverflow.com/questions/30656997/need-to-fill-a-sheet-with-consecutive-rows-data-based-on-value-in-another-worksh