无法搜索范围

时间:2016-06-16 17:47:02

标签: excel-vba search text numbers vba

我是VB的新手。尝试编写宏来帮助我完成日常任务。在这个网站上相当多,帮助我走了这么远。仍然有一个问题,所以我想发布我的问题。

我需要在A列中搜索某个Word,然后在B列中搜索某个数字,然后计算并显示在某个范围内的单元格值中。

因此,如果列A =“Word”并且列B< 0然后 Cell.value = Countif(ColumnC,Range) 其他 MsgBox =“找不到任何东西”

见下文。它工作但由于某种原因我得到所有写入ColumnC范围的值,它不读取列A“Word”或列B .. 任何评论都会有帮助。 提前干杯!!

Sub Word()

Dim i As Integer, HDIpctCompl As Single
   For i = 1 To 10000

    If Worksheets("1").Cells(i, 1) = "Word " And Worksheets("1").Cells(i, 2) < 0 Then
        Worksheets("1").Cells(2, 12).Value = Application.WorksheetFunction.CountIfs(Worksheets("1").Range("C:C"), ">=0.5", Worksheets("1").Range("C:C"), "<=0.599")

        Worksheets("1").Cells(3, 12).Value = Application.WorksheetFunction.CountIfs(Worksheets("1").Range("C:C"), ">=0.4", Worksheets("1").Range("C:C"), "<=0.499")

        Worksheets("1").Cells(4, 12).Value = Application.WorksheetFunction.CountIfs(Worksheets("1").Range("C:C"), ">=0.3", Worksheets("1").Range("C:C"), "<=0.399")

        Worksheets("1").Cells(5, 12).Value = Application.WorksheetFunction.CountIfs(Worksheets("1").Range("C:C"), ">=0.2", Worksheets("1").Range("C:C"), "<=0.299")

        Worksheets("1").Cells(6, 12).Value = Application.WorksheetFunction.CountIfs(Worksheets("1").Range("C:C"), ">=0.1", Worksheets("1").Range("C:C"), "<=0.199")

HDIpctCompl = i     'Progress bar, works, different Sub
HDIprogress HDIpctCompl

    End If
Next i

End Sub

我一直在搞乱下面的事情

Sub Word()

Dim i As Long, wb As Workbook, sh1 As Worksheet, ClmA As Range, Find As     Range, FindB As Range, ClmB As Range
Set wb = ThisWorkbook
Set sh1 = wb.Sheets("1")
Set ClmA = sh1.Range("A1:A10000")
Set ClmB = sh1.Range("B1:B10000")
Set Find = ClmA.Find("Word")
Set FindB = ClmB.Find("< 0 ")

For i = 1 To 10000
If Find = 0 And FindB = 1 Then

Else
            sh1.Cells(2, 7).Value = Application.WorksheetFunction.CountIfs(sh1.Range("C:C"), ">=0.4", sh1.Range("C:C"), "<=0.499")
            sh1.Cells(3, 7).Value = Application.WorksheetFunction.CountIfs(sh1.Range("C:C"), ">=0.3", sh1.Range("C:C"), "<=0.399")
            sh1.Cells(4, 7).Value = Application.WorksheetFunction.CountIfs(sh1.Range("C:C"), ">=0.2", sh1.Range("C:C"), "<=0.299")
            sh1.Cells(5, 7).Value = Application.WorksheetFunction.CountIfs(sh1.Range("C:C"), ">=0.1", sh1.Range("C:C"), "<=0.199")
            sh1.Cells(6, 7).Value = Application.WorksheetFunction.CountIfs(sh1.Range("C:C"), ">=0.0", sh1.Range("C:C"), "<=0.099")

    If Find = 1 And FindB = 0 Then

    Else
 MsgBox "No information detected", vbOKOnly    
    Exit Sub       
    VIApctCompl = i
    Viaprogress VIApctCompl
   End If
  End If
 Next i

End Sub

2 个答案:

答案 0 :(得分:0)

您只需在COUNTIFS声明中添加更多条件:

Sub Word()

With Worksheets("1")
    .Cells(2, 12).Value = Application.WorksheetFunction.CountIfs(.Range("A:A"), "Word", .Range("B:B"), "<0", .Range("C:C"), ">=0.5", .Range("C:C"), "<=0.599")
    .Cells(3, 12).Value = Application.WorksheetFunction.CountIfs(.Range("A:A"), "Word", .Range("B:B"), "<0", .Range("C:C"), ">=0.4", .Range("C:C"), "<=0.499")
    .Cells(4, 12).Value = Application.WorksheetFunction.CountIfs(.Range("A:A"), "Word", .Range("B:B"), "<0", .Range("C:C"), ">=0.3", .Range("C:C"), "<=0.399")
    .Cells(5, 12).Value = Application.WorksheetFunction.CountIfs(.Range("A:A"), "Word", .Range("B:B"), "<0", .Range("C:C"), ">=0.2", .Range("C:C"), "<=0.299")
    .Cells(6, 12).Value = Application.WorksheetFunction.CountIfs(.Range("A:A"), "Word", .Range("B:B"), "<0", .Range("C:C"), ">=0.1", .Range("C:C"), "<=0.199")
End With

End Sub

这基本上不需要进度条,因为它应该立即填入这些进度条。

或者,如果您希望简化代码并使以后更容易修改,请使用以下命令:

Sub Word()

Dim i As Long, j As Long
With Worksheets("1")
    j = 2
    For i = 5 To 1 Step -1
        .Cells(j, 12).Value = Application.WorksheetFunction.CountIfs(.Range("A:A"), "Word", .Range("B:B"), "<0", .Range("C:C"), ">=0." & i, .Range("C:C"), "<=0." & i & "99")
        j = j + 1
    Next i
End With

End Sub

答案 1 :(得分:-1)

Sub Word()

With Worksheets("1")
.Cells(2, 12).Value = Application.WorksheetFunction.CountIfs(.Range("A:A"), "Word", .Range("B:B"), "<0", .Range("C:C"), ">=0.5", .Range("C:C"), "<=0.599")
.Cells(3, 12).Value = Application.WorksheetFunction.CountIfs(.Range   ("A:A"), "Word", .Range("B:B"), "<0", .Range("C:C"), ">=0.4", .Range("C:C"), "<=0.499")
.Cells(4, 12).Value = Application.WorksheetFunction.CountIfs(.Range("A:A"), "Word", .Range("B:B"), "<0", .Range("C:C"), ">=0.3", .Range("C:C"), "<=0.399")
.Cells(5, 12).Value = Application.WorksheetFunction.CountIfs(.Range("A:A"), "Word", .Range("B:B"), "<0", .Range("C:C"), ">=0.2", .Range("C:C"), "<=0.299")
.Cells(6, 12).Value = Application.WorksheetFunction.CountIfs(.Range("A:A"), "Word", .Range("B:B"), "<0", .Range("C:C"), ">=0.1", .Range("C:C"), "<=0.199")
End With

End Sub