【问题标题】:VBA VLOOKUP with dynamic range具有动态范围的 VBA VLOOKUP
【发布时间】:2018-07-15 12:30:30
【问题描述】:

我是 VBA 新手。

我在同一个工作簿中有两个工作表。第一个工作表 shStudentInfo 包含我每个学生的所有信息,每个 StudentID 一行(代码中的 B4)。第二个工作表 shSchedData 包含他们的时间表,其中每个 StudentID 可能有 0-14 行,具体取决于每个学生参加的课程数量。

我正在尝试使用循环和具有动态范围的 VLOOKUP 从 shSchedData 的每一行中提取课程名称并将其复制到 shStudentInfo 中的相应单元格,然后向下移动一行。目前,我已将单元格“CO4”硬编码为适当的单元格,尽管我还需要使该引用在每次通过循环时向右移动一个单元格。

这是我不优雅的代码:

Option Explicit
Dim MyRow As Long

Sub StudentSchedules()

Dim EndRow As Long
Dim MyRng As Range

shSchedData.Activate

'hard code first row of data set
MyRow = 3
'dynamic code last row of data set
EndRow = shSchedData.Range("A1048575").End(xlUp).Row

'create a dynamic range, a single row from shSchedData
Set MyRng = ActiveSheet.Range(Cells(MyRow, 1), Cells(MyRow, 9))

'Loop through entire data set one line at a time
Do While MyRow <= EndRow

    shSchedData.Select
    MyRng = ActiveSheet.Range(Cells(MyRow,1),Cells(MyRow,9))

    shStudentInfo.Select

   'Import course name from shSchedData worksheet
    Range("CO4").Select                                                                                         
    ActiveCell.Clear

    ActiveCell.Formula = "=VLOOKUP(B4,'Schedule Data'!& MyRng,6,0)"    
    'The above line results in a #NAME? error in CO4 of shStudentInfo

    'Also tried:
    'ActiveCell.Formula = "=VLOOKUP(B4,'Schedule Data'!& MyRng.Address,6,0)" 

    'increment counter
    MyRow = MyRow + 1

Loop
End Sub

【问题讨论】:

  • 您是否将 worksheet codenames 更改为 shSchedDatashStudentInfo
  • 我无法理解 MyRng 单行范围的用途。
  • 如果您将此=VLOOKUP(B4,'Schedule Data'!&amp; MyRng,6,0) 粘贴到单元格中,它是否有效? (我认为不是。)您需要能够手动使该功能工作,然后才能使用 VBA 使其自动化。
  • 另外,如果您使用行而不是列作为范围,我相信您需要使用HLOOKUP 而不是VLOOKUP
  • 我只有一个明确的问题 - 第二张表上每个学生 ID 最多有 14 行,所以假设我们有 4 个条目,那么它们是否应该从列开始进入第一张表? CO”,然后是“CP”,然后是“CQ”,最后是“CR”——因为如果是这种情况,我什至认为你不需要 VBA

标签: excel vba vlookup named-ranges


【解决方案1】:

以下重写将使您的代码在可以确定其用途的范围内正常工作。

VLOOKUP 公式似乎不正确,无论如何,可能有更好的方法来检索数据。但是,我无法从您的叙述或代码中确定您的最终目的。样本数据连同预期结果会有所帮助。

Option Explicit

'I see no reason to put this here
'dim myRow As Long

Sub StudentSchedules()

    Dim myRow, endRow As Long, myRng As Range

    'no need to activate, just With ... End With block it
    With shSchedData

        'assigned a strarting value
        myRow = 3
        'dynamic code last row of data set
        endRow = .Cells(.Rows.Count, "A").End(xlUp).Row

        'Loop through entire data set one line at a time
        Do While myRow <= endRow

            'create a dynamic range, a single row from shSchedData
            Set myRng = .Range(.Cells(myRow, 1), .Cells(myRow, 9))

           'Import course name from shSchedData worksheet
            shStudentInfo.Range("CO4").Offset(0, myRow - 3).Formula = _
              "=VLOOKUP(B4, " & myRng.Address(external:=True) & ", 6, false)"

            'increment counter
            myRow = myRow + 1

        Loop
    End With

End Sub

【讨论】:

    【解决方案2】:

    我想出了这个,看看它是否适合你

    Dim a As Double
    Dim b As Double
    Dim ml As Worksheet
    Dim arrayrng As Variant
    Dim i As Integer
    Dim x As String
    Dim y As String
    Set ml = Worksheets("Master Data")
    
    a = ml.Cells(Rows.Count, 11).End(xlUp).Row
    b = ml.Cells(Rows.Count, 1).End(xlUp).Row
    
    For i = a To b - 1
    a = ml.Cells(Rows.Count, 11).End(xlUp).Row
    b = ml.Cells(Rows.Count, 1).End(xlUp).Row
    arrayrng = "E" & a + 1
    
    x = "=VLOOKUP(" & arrayrng
    y = ",'Data Base'!I:J,2,0)"enter code here
    
    Range("K" & a + 1) = x + y
    Next
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-30
      • 2018-10-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多