如何输出例如列#34; A"中的所有活动单元格。到一个msgbox
示例文件
chicken stew
fish pie
apple cake
msgbox会显示
chicken
fish
apple
此代码有效
MsgBox Worksheets("Sheet1").Range("A2").Value & vbCrLf & Worksheets("Sheet1").Range("A3")
但这不是
MsgBox Worksheets("Sheet1").Range("A:A").Value
非常感谢
答案 0 :(得分:1)
如果列A为空,我怎么能不显示消息框
试试这个
Sub test()
Dim lastrow As Long, res As String
With Worksheets("Sheet1")
If WorksheetFunction.CountA(.Range("A:A")) = 0 Then
MsgBox "Column A is empty"
Exit Sub
End If
lastrow = .Cells(.Rows.Count, "A").End(xlUp).Row
res = Join(Application.Transpose(.Range("A2:A" & lastrow + 1).Value), vbCrLf)
Do While InStr(1, res, vbCrLf & vbCrLf) ' remove extra vbCrLf (if there're empty rows)
res = Replace(res, vbCrLf & vbCrLf, vbCrLf)
Loop
MsgBox Left(res, Len(res) - 1)
End With
End Sub
答案 1 :(得分:0)
考虑:
Sub dural()
For Each r In Intersect(Range("A:A"), ActiveSheet.UsedRange)
If r.Value <> "" Then
msg = msg & vbCrLf & r.Value
End If
Next r
MsgBox msg
End Sub
修改#1 强>:
此版本测试列 A 是否为空:
Sub dural()
If Application.WorksheetFunction.CountA(Range("A:A")) = 0 Then
MsgBox "Column A is empty"
Exit Sub
End If
For Each r In Intersect(Range("A:A"), ActiveSheet.UsedRange)
If r.Value <> "" Then
msg = msg & vbCrLf & r.Value
End If
Next r
MsgBox msg
End Sub