来自不同工作表VBA的Msgbox

时间:2018-09-25 05:55:40

标签: vba excel-vba

我有2个不同的工作表:

  • worksheet-1
  • worksheet-2

我希望worksheet-1中的msgbox引用worksheet-2中的值。

例如:

worksheet-1中:

| A |
| B |
| C |

worksheet-2中:

| A | 10 | 5  | 6 | 4 |
| B | 5  | 4  | 2 | 1 |
| C | 5  | 10 | 5 | 6 |

当我在worksheet-1上输入密钥时,它应该引用worksheet-2中的值并显示为msgbox。

消息框:

| A  |
| 10 |
| 5  |
| 6  |
| 4  |

到目前为止,我的代码是:

Sub Search()
    Msg = "CALL DETAILS" & vbCr
    For Each cell In Sheets("sheet1").Range("B2:B" & Sheets("sheet1").Cells(Rows.Count, "B").End(xlUp).Row)
        If LCase(cell.Value) = LCase(Selection.Value) Or InStr(1, LCase(cell.Value), _
            LCase(Selection.Value)) > 0 Then
                Msg = Msg & vbCr & cell.Offset(0, 1) & " / " & vbCr & cell.Offset(0, 2) & " / " & cell.Value
        End If
    Next 
    i = MsgBox(Msg, vbInformation)
End Sub 

1 个答案:

答案 0 :(得分:0)

 sheets(‘worksheet-2’).cells(row,column).value

将在工作表2的特定单元格中给出值

您的子项或函数可以循环浏览并将输入的键与工作表2中的值进行比较:

Dim row as integer 
 row = 1
Do While isEmpty (sheets(‘worksheet-2’).cells(row,1).value)=false
//do your comparison and msgbox 
   row = row + 1
   Loop