【问题标题】:VBA to output specific text string based upon contents of a text boxVBA to output specific text string based upon contents of a text box
【发布时间】:2022-12-02 00:20:40
【问题描述】:

I'm trying to take a specific column range (B2:B500), check if the cell contains certain text string combinations and if it contains the combinations output a 'x' into a specific column which is depending on the text string in column B. My VBA isn't populating the 'x' into any columns, and I can't identify why.

Sub x()

Dim i As Integer
For i = 2 To 500
Select Case i
Case Range("B" & i) Like "AR"
Range("M" & i).Value = "x"
Case Range("B" & i) Like "FA"
Range("L" & i).Value = "x"
End Select
Next i


End Sub

The code runs without error, but I get no output. Also, if you know how I need to make when column B contains both also populate. However, I can go manually check the VBA later to fix that, the current issue is more important.

【问题讨论】:

    标签: excel vba


    【解决方案1】:

    Your Select Case is a little off and it is always better to set the worksheet, just in case the active sheet is not the one you expect:

    Sub x()
        With Worksheets("Sheet1") 'Change to your worksheet
            Dim i As Integer
            For i = 2 To 500
                Select Case .Range("B" & i)
                    Case "AR"
                        .Range("M" & i).Value = "x"
                    Case "FA"
                        .Range("L" & i).Value = "x"
                End Select
            Next i
        End With
    End Sub
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-12-02
      • 2022-12-01
      • 2022-12-27
      • 2022-12-02
      • 2022-12-02
      • 2022-12-19
      • 2022-12-01
      • 2022-12-16
      相关资源
      最近更新 更多