[英]Find non zero value in column G on sheet 1, return value of Column C in that row to sheet 2 (VBA)
My first sheet is set up like this: 我的第一张纸是这样设置的:
I want to find the non zero values in column G. Then I want to read the corresponding name in column C. I, then, want to return the value of the name to a cell on Sheet 2. 我想在G列中找到非零值。然后,我想在C列中读取对应的名称。然后,我想将名称的值返回到Sheet 2上的单元格中。
At this point, it doesn't matter what cell it returns to in sheet 2. It sounds like a VLOOKUP or INDEXMATCH but my VBA isn't good enough to figure out the formatting of it. 此时,返回工作表2中的哪个单元都没有关系。听起来像VLOOKUP或INDEXMATCH,但是我的VBA不够好,无法确定其格式。 This is some code that I tried and I can get it to return the name. 这是我尝试过的一些代码,我可以得到它以返回名称。 But I don't know how to do it for all non zeros or how to have it print to sheet 2. Need a loop or need to figure out look ups! 但是我不知道如何对所有非零值进行处理,或者如何将其打印到工作表2中。需要循环或需要查找查询!
code: 码:
For Each c In Range("G6").Cells
If c.Value > 0 Then
PlayerName = Range(Cells(Selection.Row, 3).Address).Value
End If
Exit For
Next c
Here is the logic you'll need. 这是您需要的逻辑。 Will you be able to build the macro with this logic? 您是否可以使用此逻辑构建宏? It will help you understand how to maneuver rows that are greater than zero. 它将帮助您了解如何操纵大于零的行。 Then you copy the column on that row y9ou need and paste it to the other sheet. 然后,将您需要的行上的列复制并粘贴到另一张纸上。
Sub macro1()
Dim myRng As Range, lastRow As Long
lastRow = ActiveSheet.Range("G65536").End(xlUp).Row
Set myRng = Sheet1.Range("G1:G" & lastRow)
For Each Rng In myRng
If IsNumeric(Rng.Value) And Rng.Value > 0 Then
Debug.Print "Cell " & Rng.Address & " has the number " & Rng.Value & " in row " & Rng.Row
End If
Next Rng
End Sub
The following code will find the first row which has a number greater than 0 in column G (starting at row 6), and write the value in column C of that row to cell X5 of Sheet2. 下面的代码将在G列(从第6行开始)中找到数字大于0的第一行,并将该行C列中的值写入Sheet2的单元格X5。
With Worksheets("Sheet1")
For Each c In .Range("G6", .Cells(.Rows.Count, "G").End(xlUp)).Cells
If c.Value > 0 Then
Worksheets("Sheet2").Cells(5, "X").Value = c.Offset(0, -4).Value
Exit For ' Moved this inside the `If`, otherwise it will exit as soon as
' the first cell in the range is processed, irrespective of whether
' it was greater than 0 or not
End If
Next c
End With
Iterative version: 迭代版本:
Dim s2Row as Long
s2Row = 5
With Worksheets("Sheet1")
For Each c In .Range("G6", .Cells(.Rows.Count, "G").End(xlUp)).Cells
If c.Value > 0 Then
Worksheets("Sheet2").Cells(s2Row, "X").Value = c.Offset(0, -4).Value
s2Row = s2Row + 1
End If
Next c
End With
Yes, except "G" is a column, not a row. 是的,除了“ G”是列而不是行。 Replace the debug.print line with WorkSheets("sheet name to copy from here").Rows(rng.row).Copy Destination:=WorkSheets("sheet name to copy to here").Range("A" & rowCounterVariable). 将Debug.print行替换为WorkSheets(“要从此处复制的工作表名称”)。Rows(rng.row)。复制目标:= WorkSheets(“要从此处复制的工作表名称”)。Range(“ A”和rowCounterVariable) 。 Of course, change the sheet names to your actual sheet names. 当然,将工作表名称更改为您的实际工作表名称。
Here I set the first row at 2 on the page to copy to. 在这里,我将页面的第一行设置为2。 If you need to set it to the first available row then you need to research how to find the last used row on that page. 如果需要将其设置为第一行,则需要研究如何查找该页面上最后使用的行。 Put these exact terms into Google "VBA EXCEL HOW TO FIND LAST USED ROW". 将这些确切的术语放入Google“ VBA EXCEL如何找到最后使用的行”。 I have an example of finding the last used row for the activesheet inside the code. 我有一个示例,可以找到代码内活动表的最后使用行。 We could give you fish today, and teach you how to fish. 我们今天可以给您钓鱼,并教您如何钓鱼。 But you need to catch your own. 但是,您需要自己抓住。 We're not here to write code for you. 我们不是在这里为您编写代码。
Sub macro2()
Dim myRng As Range, lastRow As Long, rowCounterVariable as long
rowCounterVariable = 2
lastRow = ActiveSheet.Range("G65536").End(xlUp).Row
Set myRng = Sheet1.Range("G1:G" & lastRow)
For Each Rng In myRng
If IsNumeric(Rng.Value) And Rng.Value > 0 Then
WorkSheets("sheet name to copy from here").Rows(rng.row).Copy Destination:=WorkSheets("sheet name to copy to here").Range("A" & rowCounterVariable)
rowCounterVariable = rowCounterVariable + 1
End If
Next Rng
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.