在MSgBox中显示来自行的所有非空值

时间:2016-02-09 08:09:43

标签: excel cell msgbox

我正在处理行大小A:ZZ的Excel工作表,并且应该只有7个单元格具有值。我试图制作一种“过滤器”,它将检查非空单元格的数量,如果有超过7个非空单元格将在MsgBox中打印一条消息(直到这一点工作)。但是在MsgBox中我还想从行中找到那些值(例如用逗号分隔) - 这虽然因为Intersect语法的某些问题而无法工作。这是代码

Sub blanks() 
  Dim a, b As Integer
  a = 0

  Range("A1").Select
  Do
    With ActiveSheet.Range(Rows(b))
      b = ActiveCell.Row
      a = Application.WorksheetFunction.CountA(ActiveSheet.Rows(b))

      If a > 7 Then
        MsgBox ("ERROR" & "/n" & Application.Intersect(.SpecialCells(xlCellTypeVisible)))
        Exit Do
      Else
        ActiveCell.Offset(1, 0).Select
      End If

    End With
  Loop Until ActiveCell = "stop"
End Sub 

有什么问题?

1 个答案:

答案 0 :(得分:0)

这将为您提供常数值(即不是来自公式):

Sub ShowValues()
    Dim rowNum As Long
    Dim rowRange As Range
    Dim valRange As Range
    Dim msg As String

    rowNum = 1
    With ThisWorkbook.Worksheets("Sheet1")
        Do While .Cells(rowNum, 1) <> "stop"
            Set rowRange = .Cells(rowNum, 1).Resize(1, 702)
            If Application.WorksheetFunction.CountA(rowRange) > 7 Then

                'Add row number to message.
                msg = msg & "Row: " & rowRange.Row & ": "

                'Add values to message separated by comman.
                For Each valRange In rowRange.SpecialCells(xlCellTypeConstants)
                    msg = msg & valRange.Value & ", "
                Next valRange

                'Remove last comma.
                msg = Left(msg, Len(msg) - 2)

                'Line break.
                msg = msg & vbCr
            End If
            rowNum = rowNum + 1
        Loop
    End With
    MsgBox msg, vbOKOnly + vbInformation
End Sub

或者这会给你地址:

Sub ShowAddressOfValues()
    Dim rowNum As Long
    Dim rowRange As Range
    Dim msg As String

    rowNum = 1
    With ThisWorkbook.Worksheets("Sheet1")
        Do While .Cells(rowNum, 1) <> "stop"
            Set rowRange = .Cells(rowNum, 1).Resize(1, 702)
            If Application.WorksheetFunction.CountA(rowRange) > 7 Then
                msg = msg & "Row: " & rowRange.Row & ". Address: " & _
                    rowRange.SpecialCells(xlCellTypeConstants).Address & vbCr
            End If
            rowNum = rowNum + 1
        Loop
    End With
    MsgBox msg, vbOKOnly + vbInformation
End Sub