简体   繁体   English

根据条件删除特定列

[英]Delete Specific Columns based on Conditions

I'm currently trying to make a program that takes user input and stores the values in an array called FastenerNumbers.我目前正在尝试制作一个程序,该程序接受用户输入并将值存储在一个名为 FastenerNumbers 的数组中。 Based on these values the program then fills specific cells with a green color so that the user knows to enter values there.根据这些值,程序然后用绿色填充特定单元格,以便用户知道在那里输入值。 The thing is if a value in the array is 0 I would like to delete that column so that the worksheet is cleaner.问题是,如果数组中的值是 0,我想删除该列,以便工作表更干净。

The issue I'm running into is that when a column is deleted during the for loop the cells shift left.我遇到的问题是,在 for 循环期间删除列时,单元格向左移动。 Because of this some of the cells are essentially skipped over.因此,一些单元格基本上被跳过了。 To counteract this I've essentially had to brute force the program so that it loops several times to account for any skipped columns.为了解决这个问题,我基本上不得不强制程序使其循环多次以解决任何跳过的列。

Here's the code:这是代码:

'Make cells green for user to put inputs into
For i = 0 To UBound(FastenerNumbers)
    If FastenerNumbers(i) <> 0 Then
        With Range(Range("A14").Offset(0, 2 * i), Range("A14").Offset(FastenerNumbers(i) - 1, (2 * i) + 1))
            .Borders.LineStyle = xlContinuous
            .Interior.ColorIndex = 4
        End With
    End If
Next

'Define initial counter variable
j = 1

'    Do Until j = 5
'        For i = 0 To UBound(FastenerNumbers)
'            If FastenerNumbers(i) = 0 Then
'                Range(Range("A14").Offset(0, 2 * i), Range("A14").Offset(FastenerNumbers(i) - 1, (2 * i) + 1)).EntireColumn.Delete
'            End If
'        Next
'    Loop
'

Do
    For Each cell In Range("A14", Range("A14").Offset(, (UBound(FastenerNumbers) + 1) * 2))
        If cell.Interior.ColorIndex <> 4 Then
            cell.EntireColumn.Delete
        End If
        j = j + 1
        If j >= (5 * (UBound(FastenerNumbers) + 1) * 2) Then
            Exit Do
        End If
    Next
Loop

The pseudocode is another method I was going to use.伪代码是我将要使用的另一种方法。 I don't think either method is significantly better than the other.我不认为任何一种方法都明显优于另一种。 I would like the loops to be cleaner and more efficient though.不过,我希望循环更清洁、更高效。

I haven't tested this so not entirely sure it works, but give this a shot.我还没有测试过这个,所以不能完全确定它是否有效,但请试一试。 Essentially it keeps everything within the For loop, and if FastenerNumbers(i) = 0 then it deletes the column, reduces i by 1, then continues to the next (in that case the same number):本质上,它将所有内容保留在For循环中,如果FastenerNumbers(i) = 0 则删除该列,将i减少 1,然后继续下一个(在这种情况下为相同的数字):

For i = 0 To UBound(FastenerNumbers)
    If FastenerNumbers(i) <> 0 Then
        With Range(Range("A14").Offset(0, 2 * i), Range("A14").Offset(FastenerNumbers(i) - 1, (2 * i) + 1))
            .Borders.LineStyle = xlContinuous
            .Interior.ColorIndex = 4
        End With
    Else
        Range(Range("A14").Offset(0, 2 * i), Range("A14").Offset(FastenerNumbers(i) - 1, (2 * i) + 1)).EntireColumn.Delete
        i = i - 1
    End If
Next

it as simple as having a separate variable ( j ) counting the number of valid FastenerNumbers() valueslike follows它就像有一个单独的变量 ( j ) 来计算有效FastenerNumbers()值的数量一样简单,如下所示

'Make cells green for user to put inputs into
For i = 0 To UBound(FastenerNumbers)
    If FastenerNumbers(i) <> 0 Then
        With Range(Range("A14").Offset(0, 2 * j), Range("A14").Offset(FastenerNumbers(i) - 1, (2 * j) + 1)) ' use j as the column relevant variable
            .Borders.LineStyle = xlContinuous
            .Interior.ColorIndex = 4
        End With
        j = j + 1 'update column relevant variable
    End If
Next

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

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