在VBA中使用IF

时间:2019-02-19 22:50:18

标签: excel vba

有人可以告诉我这是否是表达此VBA宏的正确方法吗?

这个想法是要检查A2:A70范围内的所有单元格(实际上,如果更容易,它可以是A列的全部),并且如果FA_Win_2或FA_Win_3的文本在那里,则使该单元格为粗体。

If Range(“A2:A70”) = FA_Win_2 Or _
   Range(“A2:A70”) = FA_Win_3 Then
   Range(“A2:A70”).font.Bold = “True”
End If

我曾尝试过其他方法,例如将FA_Win_2括在“”中,在范围后添加.Value,但总是会出现编译错误。

任何帮助将不胜感激。

欢呼

2 个答案:

答案 0 :(得分:1)

检查值范围

用于下一个循环

Sub ForNext()

    Dim i As Long   ' Source Column Range Cells Counter

    ' In Source Column Range
    With Range(A2:A70)
        ' Loop through cells of Source Column Range.
        For i = 1 To .Cells.Count
            ' Check for Search Criterias.
            If .Cells(i) = "FA_Win_2" Or .Cells(i) = "FA_Win_3" Then
                ' Apply formatting.
                .Cells(i).Font.Bold = True
            End If
        Next
    End With

End Sub

对于每个循环

Sub ForEach()

    Dim Cell As Range   ' Current Cell Range (For Each Control Variable)

    ' Loop through cells of Source Column Range.
    For Each Cell In Range("A2:A70")
        ' Check for Search Criterias.
        If Cell = "FA_Win_2" Or Cell = "FA_Win_3" Then
            ' Apply formatting.
            Cell.Font.Bold = True
        End If
    Next

End Sub

编辑

对于每个循环2

Sub ForEach2()

    Const cSheet As String = "Sheet3"   ' Source Worksheet Name
    Const cRange As String = "A2:A70"   ' Source Column Range Address
    Const cStr1 As String = "FA_Win_2"  ' Search Criteria 1"
    Const cStr2 As String = "FA_Win_3"  ' Search Criteria 2"

    Dim Cell As Range   ' Current Cell Range (For Each Control Variable)

    ' Loop through cells of Source Column Range in Source Worksheet.
    For Each Cell In ThisWorkbook.Worksheets(cSheet).Range(cRange)
        ' Check for Search Criterias.
        If Cell = cStr1 Or Cell = cStr2 Then
            ' Apply formatting.
            With Cell
                .Font.Bold = True
                .Font.Color = RGB(255, 255, 255)    ' White
                .Interior.Color = RGB(255, 0, 255)  ' Pink
            End With
        End If
    Next

End Sub

答案 1 :(得分:1)

我的方法是将fd_set player_fd_set; FD_ZERO(&player_fd_set); int max_fd = players[0].connected_socket_on_master; for (int i = 0; i < num_players; ++i) { FD_SET(players[i].connected_socket_on_master, &player_fd_set); if (players[i].connected_socket_on_master > max_fd) max_fd = players[i].connected_socket_on_master; } if (select(max_fd + 1, &player_fd_set, NULL, NULL, NULL) > 0) { int offset = 0; for (int i = 0; i < num_players; ++i) { printf("Check fd # %d\n", i); if (!FD_ISSET(players[offset].connected_socket_on_master, &player_fd_set)) continue; printf("Coming from player # %d\n", i); ssize_t recvStatus = recv(players[offset].connected_socket_on_master, potato.trace, sizeof(potato.trace), 0); if (recvStatus > 0) { // process potato.trace up to recvStatus bytes as needed... ++offset; continue; } if (recvStatus < 0) { if ((errno == EWOULDBLOCK) || (errno == EAGAIN)) { // no more data to read right now... ++offset; continue; } printf("Error: Could not recv final potato from player # %d\n", i); } else printf("Player # %d disconnected\n", i); close(players[offset].connected_socket_on_master); for(int j = i+1; j < num_players; ++j) players[j-1] = players[j]; --num_players; } } 设置为等于您的测试语句。

我不确定您是否真的只需要遍历该范围,但可以通过将.Font.Bold = TRUE/FALSE交换到变量最后一行calc来轻松更新以使其更加动态


70