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

Show in MSgBox all nonempty values from row

我正在处理行大小 A:ZZ 的 Excel sheet,应该只有 7 个单元格有值。我正在尝试制作一种 "filter" 来检查非空单元格的数量,如果有超过 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 

有什么问题吗?

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

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