【问题标题】:How to use VLOOKUP and get details from other sheet using VBA如何使用 VLOOKUP 并使用 VBA 从其他工作表中获取详细信息
【发布时间】:2016-02-25 03:49:57
【问题描述】:

我的工作簿中有以下工作表

test,input,model,cm,mm,output

我想从输入表中获取输入。

  1. 将测试列之前的列“mark”和基于 house(,) 的 vlookup 添加到工作表“CM”house 以进行标记。
  2. 我需要从基于“CM”的房屋中获取“亲爱的”详细信息
  3. 我需要从基于“MM”的房子中获取“儿子”的详细信息
  4. 我需要在兄弟和儿子之间添加栏。其中基于儿子和模型表命名为mark和vlook。

输入

test    mail    god house   dear    moon    son brother loosee  man boy girl    test
dd  d   d   sd  dfd 123 dfd ad  d   df  sd  d   d


model                                               
pop mark                                            
123 jklm                                            
CM                                              
house   dear    mark                                        
sd  dfd love                                        
MM                                              

moon    son                                         
123 dfd                                         

我正在尝试编写以下代码来获取所有详细信息并获得输出。但我失败了,请指导我解决它。

Option Explicit

Sub CopyRows()
    Dim ws1 As Worksheet, ws2 As Worksheet
    Dim i As Integer, k As Integer
    Dim ws1LR As Long, ws2LR As Long

    Set ws1 = Sheets("input")
    Set ws2 = Sheets("output")

    ws1LR = ws1.Range("A" & Rows.Count).End(xlUp).Row + 1
    ws2LR = ws2.Range("A" & Rows.Count).End(xlUp).Row + 1

    i = 2
    k = ws2LR
    Do Until i = ws1LR
        With ws1
            .Range(.Cells(i, 1), .Cells(i, 18)).Copy
        End With

        With ws2
            .Cells(k, 1).PasteSpecial
            .Cells(k, 1).Offset(1, 0).PasteSpecial
        End With

        k = k + 2
        i = i + 1
    Loop
End Sub

【问题讨论】:

  • 无法理解您的确切目标。请给出一个开始和最终工作表的真实例子
  • 很好的第一步,打开宏记录器并手动执行这些步骤。这将为您提供在 VBA 中自动执行任务所需的大部分内容。

标签: vba excel excel-formula vlookup


【解决方案1】:

我认为实现目标最简单的方法是:

Sub prueba()


    Const adOpenStatic = 3
    Const adLockOptimistic = 3
    Const adCmdText = &H1

    Dim point2 As String
    Dim point3 As String

    Dim query As String

    Set objConnection = CreateObject("ADODB.Connection")
    Set objRecordset = CreateObject("ADODB.Recordset")

    objConnection.Open "Provider=Microsoft.ACE.OLEDB.12.0;" & _
        "Data Source=C:\Users\Daniel\Desktop\prueba.xlsx;" & _
            "Extended Properties=""Excel 8.0;HDR=Yes;"";"

    query = "Select cm.dear FROM [input$] as i inner join [cm$] as cm on i.house = cm.house where test is not null"

    objRecordset.Open query, _
        objConnection, adOpenStatic, adLockOptimistic, adCmdText

    'since query returns one row so:

    point2 = objRecordset.Fields.Item("dear") '<---- returns "dfd"

    objRecordset.Close

    query = "Select mm.moon FROM [input$] as i inner join [mm$] as mm on i.house = mm.house where test is not null"

    'following line cant return any value because there is not any house field on mm table
    objRecordset.Open query, _
        objConnection, adOpenStatic, adLockOptimistic, adCmdText

    point3 = objRecordset.Fields.Item("moon")


End Sub

关于这段代码,你应该知道的只是如何编写正确的查询。

注意查询变量,它就像一个sql查询,你可以连接几张表,并在两个字段(列)中这两个值相等的地方寻找一个值。那是因为您无法运行第二个查询,因为 mm 表中没有名为 house 的字段(根据您的示例)。

【讨论】:

    猜你喜欢
    • 2016-10-20
    • 1970-01-01
    • 1970-01-01
    • 2020-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多