簡體   English   中英

具有動態范圍的 VBA VLOOKUP

[英]VBA VLOOKUP with dynamic range

我是 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

以下重寫將使您的代碼在其目的可以確定的范圍內工作。

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

我想出了這個,看看它是否適合你

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM