excel 如何在MsgBox中打印单元格“值”?

jexiocij  于 2023-01-27  发布在  其他
关注(0)|答案(2)|浏览(237)

我有VBA代码,它可以转到参考工作簿,并根据所需的工作表名称生成报表。完成后,我将输出到所有找不到的工作表的单元格中,我想将它放在一个弹出的MsgBox中,其中包含找不到的工作表列表。以下是这些丢失工作表的输出:

On Error Resume Next
Do While y \<= x
Workbooks(maker).Activate
Z = Range("u10:u" & cnt).Find(what:=y, LookIn:=xlValues, lookat:=xlWhole).Select
If Err \> 0 Then
V = Range("E10:E" & cnt).Find(what:=y, LookIn:=xlValues, lookat:=xlWhole).Select
t = Selection.Offset(0, 1)
'This is where the not found worksheets are printed in column w
Range("w" & q).Value = t
q = q + 1
y = y + 1
Else
t = Selection.Offset(0, -1)
Workbooks(Filename).Sheets(t).Copy After:=Workbooks(temp).Sheets(Workbooks(temp).Sheets.Count)
Workbooks(maker).Activate
y = y + 1
End If
Loop
On Error GoTo 0

我该如何将Range(“w”&q).Value=t设置为一个列出工作表名称的消息框?
我一直在搜索谷歌寻找想法或解决方案,但我有问题,制定这个解决方案。任何帮助或指导是赞赏。

uqcuzwp8

uqcuzwp81#

On Error Resume Next
    sMsgTxt = "" ' Initialize msgbox string
Do While y <= x
Workbooks(maker).Activate
Z = Range("u10:u" & cnt).Find(what:=y, LookIn:=xlValues, lookat:=xlWhole).Select
If Err > 0 Then
V = Range("E10:E" & cnt).Find(what:=y, LookIn:=xlValues, lookat:=xlWhole).Select
t = Selection.Offset(0, 1)
'This is where the not found worksheets are printed in column w
Range("w" & q).Value = t
    sMsgTxt = sMsgTxt & t & vbCrLf ' Append to msgbox string
q = q + 1
y = y + 1
Else
t = Selection.Offset(0, -1)
Workbooks(Filename).Sheets(t).Copy After:=Workbooks(temp).Sheets(Workbooks(temp).Sheets.Count)
Workbooks(maker).Activate
y = y + 1
End If
Loop
    MsgBox sMsgTxt ' Output msgbox string
On Error GoTo 0
lb3vh1jj

lb3vh1jj2#

首先,哪一行弹出错误消息?
无论哪种方式,这都是一种从多行构建消息的方法。我知道您正在查找不存在的工作表,因此整个For Each WS的事情并不适用......但演示它是有用的。

Sub MessageFromCellValues()

    Dim Msg As String
    Dim maker As String
    Dim WS As Worksheet
    Dim X As Long
    Dim Y As Long
    
    maker = "Your Worksheet Name.xlsm"
    
    'Do While Y <= X
        
        ' ... Your Code
        
    'Loop
    
    Msg = " This is the list of Sheet Names " & vbCrLf & vbCrLf
    
    For Each WS In Workbooks(maker).Worksheets
        Msg = Msg & " - " & WS.Name & vbCrLf
    Next WS '
    
    MsgBox Msg, vbOKOnly, "Sheets List"
    
End Sub

示例:

相关问题