简体   繁体   English

Excel在单元格等于值时执行获取相邻单元格的时间

[英]Excel Do while to obtain adjacent cells when cell equals value

I have two sheets. 我有两张。 Sheet 1 (Customer tracker - OC) has a column which contains either yes or no for every row. 工作表1(客户跟踪程序-OC)的一列中,每一行都包含“是”或“否”。 If the value is yes , the two adjacent cells to the right have two values in them. 如果值为yes,则右边两个相邻的单元格中都有两个值。

I would like to create a do while / for each macro that cycles through each cell in the yes/no column in sheet1, and where the value is yes , creates a new row in the blank sheet 2 which the two adjacent values from sheet 1. 我想为在工作表sheet1的yes / no列中的每个单元格中循环的每个宏创建一个do while /,其中值是yes,在空白工作表2中创建一个新行,其中工作表1中的两个相邻值。

Ie

Sheet1 工作表1

   A    B    C
1  YES  4    6 
2  NO 
3  YES  91   42

desired result in sheet 2 ("contracts") 工作表2中的预期结果(“合同”)

   A    B
1  4    6 
2  91   42

I have the below code , but it's erroring with "overflow". 我有以下代码,但是它出现“溢出”错误。

    Sub ForEach_Loop()

Dim TrackerLastRow As Integer
Dim TrackerCurrentRow As Integer
Dim ContractLastRow As Integer
Dim ContractCurrentRow As Integer

TrackerLastRow = Worksheets("Customer Tracker - OC").Range("T1").End(xlDown).Row
    TrackerCurrentRow = 1

    ContractLastRow = Worksheets("Contracts").Range("A1").End(xlDown).Row
    ContractCurrentRow = 1

    Do While TrackerCurrentRow <= TrackerLastRow

      If Worksheets("Customer Tracker - OC").Range("T" & TrackerCurrentRow).Value = "Yes" Then
            Worksheets("Contracts").Range("A" & ContractCurrentRow) = Worksheets("Customer Tracker - OC").Range("T" & TrackerCurrentRow).Offset(, 1).Value
            Worksheets("Contracts").Range("B" & ContractCurrentRow) = Worksheets("Customer Tracker - OC").Range("T" & TrackerCurrentRow).Offset(, 2).Value
            TrackerLastRow = TrackerLastRow + 1
            TrackerCurrentRow = TrackerCurrentRow + 1
            ContractLastRow = ContractLastRow + 1
            ContractCurrentRow = ContractCurrentRow + 1
        End If

        TrackerCurrentRow = TrackerCurrentRow + 1
        ContractCurrentRow = ContractCurrentRow + 1

        Loop


End Sub

Any help would be appreciated 任何帮助,将不胜感激

It seems you are over-incrementing your vars and over-extending the functional limits of an integer type var. 似乎您过度增加了var,并过度扩展了整数类型var的功能限制。

Option Explicit

Sub ForEach_Loop()

    Dim TrackerLastRow As Long, TrackerCurrentRow As Long
    Dim ContractLastRow As Long, ContractCurrentRow As Long

    With Worksheets("Contracts")
        ContractLastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
        ContractCurrentRow = 1
    End With

    With Worksheets("Customer Tracker - OC")

        TrackerLastRow = .Cells(.Rows.Count, "T").End(xlUp).Row
        TrackerCurrentRow = 1

        Do While TrackerCurrentRow <= TrackerLastRow

            If .Cells(TrackerCurrentRow, "T").Value = "Yes" Then
                Worksheets("Contracts").Range("A" & ContractCurrentRow) = .Range("T" & TrackerCurrentRow).Offset(0, 1).Value
                Worksheets("Contracts").Range("B" & ContractCurrentRow) = .Range("T" & TrackerCurrentRow).Offset(0, 2).Value
                ContractCurrentRow = ContractCurrentRow + 1
            End If

            TrackerCurrentRow = TrackerCurrentRow + 1

        Loop
    End With


End Sub

I have managed to solve it, i did change to integer and removed some unnecessary incrementing. 我设法解决了这个问题,我确实更改为整数并删除了一些不必要的增量。 Think i was a bit confused at first. 认为我起初有点困惑。 Thanks for the help! 谢谢您的帮助!

    Sub ForEach_Loop()

Dim TrackerLastRow As Long
Dim TrackerCurrentRow As Long
Dim ContractLastRow As Long
Dim ContractCurrentRow As Long

TrackerLastRow = 2486
    TrackerCurrentRow = 3


    ContractCurrentRow = 1

    Do While TrackerCurrentRow <= TrackerLastRow

      If Worksheets("Customer Tracker - OC").Range("T" & TrackerCurrentRow).Value = "Yes" Then

            Worksheets("Contracts").Range("A" & ContractCurrentRow) = Worksheets("Customer Tracker - OC").Range("T" & TrackerCurrentRow).Offset(, 1).Value
            Worksheets("Contracts").Range("B" & ContractCurrentRow) = Worksheets("Customer Tracker - OC").Range("T" & TrackerCurrentRow).Offset(, 2).Value

            TrackerCurrentRow = TrackerCurrentRow + 1
            ContractCurrentRow = ContractCurrentRow + 1

            Else

            TrackerCurrentRow = TrackerCurrentRow + 1

        End If



        Loop


End Sub

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM