简体   繁体   English

使用If / then语句在For / Next循环中调整值

[英]Adjusting a value in a For/Next loop with an If/then statement

So I have a nice convoluted series of subs that are all connected with a userform. 因此,我有一系列复杂的,与用户表单相关联的子控件。 The code basically checks a date value stored in a tabled against today, stores that T/F answer in another table, and once each row meets a criteria of Trues, moves the date to another table to be viewed. 该代码基本上对照今天存储在表中的日期值,将T / F答案存储在另一个表中,并且一旦每行满足Trues标准,就将日期移动到另一个表中进行查看。 It's this third table that's causing the problem. 正是这个第三张表引起了问题。 I want the new table to have everything listed contiguously here, but instead, all I can seem to do is copy it to the same cell on a different sheet. 我希望新表在此处连续列出所有内容,但是,我似乎可以做的就是将其复制到另一张纸上的同一单元格中。

Set ToDoRange = Sheets("ToDo").Range("A2")
 ToDoRow = 2
For i = 2 To LastRow 'Search each column in each row before moving to the next row

For Each ColVar In [PM].Columns

···
···
···

Set WorkReady = Sheets("DateList").Cells(i, j)


If LocChk2.Value = True And TFRange.Value = True And j > ColLoc Then

     With Sheets("DateList").Rows(i)
         .Interior.Color = 8257297
         .Copy
     End With
    Sheets("ToDo").Cells(ToDoRow, j) = WorkReady.Value
       With ToDoRange
        .Borders(xlEdgeBottom).Weight = xlMedium
       End With
 ElseIf LocChk2.Value = False Or TFRange.Value = False Or j <= ColLoc Then
          WorkReady.Interior.Color = 255
          With Sheets("ToDo").Cells(ToDoRow, j)
                .Clear
                .Borders(xlEdgeBottom).Weight = xlMedium
                .Interior.Color = 16755623
          End With
          If j < ColNumber Then
          Sheets("ToDo").Cells(ToDoRow, j).Borders(xlEdgeBottom).Weight = xlNone
          End If


    End If

Next

'If Len(Sheets("ToDo").Cells(ToDoRow, ColLoc).Value) <> 0 Then

ToDoRow = ToDoRow + 1
Set ToDoRange = ToDoRange.Offset(1, 0) 
'End If
Next

When the If/Then statement at the bottom is enabled, it seems to randomly select what values it meet that criteria. 启用底部的If / Then语句后,似乎随机选择了满足该条件的值。 If I move it between the two "for"s instead of the "next"s then it offsets all the values more like a staircase than a row. 如果我将其在两个“ for”之间而不是“ next”之间移动,则它会偏移所有值,更像是阶梯而不是行。 Any suggestions? 有什么建议么?

Update: if I sort the table so that everything is listed together, it crashes Excel the next time I run the sub. 更新:如果我对表格进行排序,以便所有内容一起列出,则下次运行该子程序时,它会使Excel崩溃。

So it just crashed that one time, and I still don't know why it did that, but just automatically sorting it at the end of the code seems to have solved all of my issues. 因此它一次崩溃了,我仍然不知道为什么这么做,但是在代码末尾自动排序似乎已经解决了我所有的问题。

Sheets("ToDo").ListObjects("Table1").Sort.SortFields.Add _
        Key:=Range("Table1[[#All],[Number]]"), SortOn:=xlSortOnValues, Order:= _
        xlAscending, DataOption:=xlSortNormal
With Sheets("ToDo").ListObjects("Table1").Sort
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With

It was a simple solution, and I spent longer than I'd like to admit before I remembered that you can sort tables. 这是一个简单的解决方案,在想起您可以对表格进行排序之前,我花了比我要承认的时间更长的时间。

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

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