Excel VBA,将Msgbox中的数据导入工作表单元格

时间:2017-09-18 13:24:58

标签: excel vba

我正在从工作表单元格中读取少量数据。我可以看到多个Msgbox中显示的数据。数据还可以。当我试图让Msgbox将这些数据输入到单独的工作表单元格中时,它将它们全部放入一个单元格中,覆盖每个先前的条目,或者使用其他代码将其输入最后一个数据到多个单元格中。

这应该是非常简单的事情吗?

您能告诉我如何将Msgbox数据导入工作表单元格吗?

Sub Macro3()
    Dim myLastRow As Variant
    Dim myRow As Integer
    Dim myFind As String
    Dim myMatch As String
    Dim myReplace As Variant
    Dim cell, myColumn As Range
    Dim arr() As Variant

    Sheets("Test").Select
    arr = Range("D2:D6")
    For Each myReplace In arr
    'MsgBox "" & myReplace

    Range("A2:A40").Select
    Set myColumn = Cells.Find(myReplace, After:=Range("A1"), LookAt:=xlWhole, SearchOrder:=xlByColumns)
    'MsgBox "" & myColumn
    'MsgBox "" & myColumn.Address
    myFind = myColumn.Offset(0, 1)
    MsgBox "" & myFind

     Range("E2").Value = myFind


    Next

End Sub

2 个答案:

答案 0 :(得分:0)

在您的代码中,当它显示MsgBox...时,您告诉Excel显示包含某些信息的消息框(例如myFind)。如果您需要工作表(或任何工作表)中的信息,则需要将MsgBox...替换为Range()子句。 这是非常基本的VBA,所以任何教程都应该帮助你。

答案 1 :(得分:-2)

请尝试以下代码:

Sub Macro3()
    Dim myLastRow As Variant
    Dim myRow As Integer
    Dim myFind As String
    Dim myMatch As String
    Dim myReplace As Range
    Dim cell As Range
    Dim myColumn As Range
    Dim arr As Range
    Dim i As Integer

    i = 2

    Sheets("Test").Select
    Set arr = Range("D2:D6")
    For Each myReplace In arr
    'MsgBox "" & myReplace

    Range("A2:A40").Select
    Set myColumn = Cells.Find(myReplace.Value, After:=Range("A1"), LookAt:=xlWhole, SearchOrder:=xlByColumns)
    'MsgBox "" & myColumn
    'MsgBox "" & myColumn.Address
    myFind = myColumn.Offset(0, 1).Value


     If myFind <> "" Then

    MsgBox "" & myFind

     Cells(i, 5).Value = myFind

     i = i + 1
     End If

    Next
End Sub