简体   繁体   English

在工作表1的G列中找到非零值,将该行中的列C的值返回到工作表2(VBA)

[英]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: 我的第一张纸是这样设置的:

在此处输入图片说明

  1. 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上的单元格中。

  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.

相关问题 如果工作表 2 中 A 列中的单元格等于工作表 1 中的值,则将工作表 2 中的值返回到工作表 1 中的 B 列和 c 列 - If a cell in column A in sheet 2 equal to a value from sheet 1 then return the values from sheet 2 to column B and c in sheet 1 将工作表1中某些行中的特定单元格复制(取决于列中的非零值)到工作表2的最后一个空行 - Copy specific cells from sertain rows from Sheet 1 depending on non-zero value in column to the last empty row of Sheet 2 VBA宏可在另一张工作表中查找另一列的值 - VBA macro to find one column value to other column in different sheet 如果在其他工作表的列中未找到单元格值,则Excel VBA删除行 - Excel VBA delete row if cell value not found in a column in other sheet VBA Excel-在列中查找值,粘贴到另一张表 - VBA Excel - find a value in a column, paste to another sheet 在另一个工作表的列中的工作表的列中搜索每个值,如果找到,则将整个行粘贴到输出中 - Search a each value from a column of sheet in another sheet's column and if find then paste entire row in output 试图在 sheet1 的最后一行的 b 列中查找单元格的值 - Trying to find the value of a cell in column b of the last row in sheet1 VBA-将特定值从一个工作表列复制到下一个工作表行 - VBA - Copy specific value from one sheet column to next sheet row VBA清除工作表中的数据,但第一行和第G列除外 - VBA clear data in sheet except first Row and Column G 在VBA中使用sheet()。Range()。Value进行列引用 - Column referencing in VBA using sheet().Range().Value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM