Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 具有动态范围的VBA VLOOKUP_Excel_Vba_Vlookup_Named Ranges - Fatal编程技术网

Excel 具有动态范围的VBA VLOOKUP

Excel 具有动态范围的VBA VLOOKUP,excel,vba,vlookup,named-ranges,Excel,Vba,Vlookup,Named Ranges,我是VBA的新手 我在同一工作簿中有两张工作表。第一个工作表shStudentInfo包含我每个学生的所有信息,每个StudentID一行(代码中的B4)。第二个工作表shSchedData包含他们的时间表,每个学生ID可能有0-14行,这取决于每个学生正在学习的课程数量 我试图使用一个循环和一个动态范围的VLOOKUP从shSchedData的每一行中提取课程名称,并将其复制到shStudentInfo中的相应单元格中,然后向下移动一行。目前,我已经将单元格“CO4”硬编码为适当的单元格,尽管

我是VBA的新手

我在同一工作簿中有两张工作表。第一个工作表shStudentInfo包含我每个学生的所有信息,每个StudentID一行(代码中的B4)。第二个工作表shSchedData包含他们的时间表,每个学生ID可能有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
选项显式
让我的行变长
副学生附表()
暗尾行与长尾行相同
暗MyRng As范围
shSchedData,激活
'硬编码数据集的第一行
MyRow=3
'动态代码数据集的最后一行
EndRow=shSchedData.Range(“A1048575”).End(xlUp).Row
'从shSchedData创建一个动态范围,一行
设置MyRng=ActiveSheet.Range(单元格(MyRow,1),单元格(MyRow,9))
'一次一行遍历整个数据集

Do While MyRow以下重写将使您的代码工作到可以确定其用途的程度

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
选项显式
“我看没有理由把这个放在这里
“把我的路弄得那么长
副学生附表()
变暗myRow,尾行为长,myRng为范围
“无需激活,只需使用。。。以阻止它结束
和shSchedData一起
'指定了一个strarting值
myRow=3
'动态代码数据集的最后一行
endRow=.Cells(.Rows.Count,“A”).End(xlUp).Row
'一次一行遍历整个数据集

当myRow我想出这个的时候,看看它是否适合你

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

您是否已将更改为shSchedData和shStudentInfo?我无法理解MyRng的单行范围的用途。如果将此
=VLOOKUP(B4,'Schedule Data'!&MyRng,6,0)
粘贴到单元格中,是否有效?(我想不是。)在使用VBA实现自动化之前,您需要能够手动运行函数。此外,如果您使用行而不是列作为范围,我相信您需要使用
HLOOKUP
而不是
VLOOKUP
。我只有一个澄清问题-第二页上每个学生ID最多有14行,假设我们有4个条目,它们是否应该进入第一页,从列“CO”开始,然后是“CP”,然后是“CQ”,最后是“CR”-因为如果是这种情况,我甚至认为你不需要VBA