VBA 循环行和列内的 vlookup
VBA vlookup within loop row and column
对不起,我想问一下关于行和列循环使用 vlookup
的问题。这是我的代码,但没有用。你能帮助更正这段代码吗?谢谢
Private Sub CommandButton1_Click()
Dim ws As Worksheet
Dim i As Integer
Dim j As Integer
Set ws = ThisWorkbook.Sheets("GROWTH PERUSAHAAN")
With ws
For j = 3 To 100
For i = 2 To 600
If Application.WorksheetFunction.VLookup(ThisWorkbook.Sheets("GROWTH PERUSAHAAN").Cells(i, 1), ThisWorkbook.Sheets("INPUT KUANTITATIF").Range("A:AC"), j + 3, 0) = "" Then
ThisWorkbook.Sheets("GROWTH PERUSAHAAN").Cells(i, j) = 0
Else
ThisWorkbook.Sheets("GROWTH PERUSAHAAN").Cells(i, j) = (Application.WorksheetFunction.VLookup(ThisWorkbook.Sheets("GROWTH PERUSAHAAN").Cells(i, 1), ThisWorkbook.Sheets("INPUT KUANTITATIF").Range("A:AC"), j + 4, 0) / Application.WorksheetFunction.VLookup(ThisWorkbook.Sheets("GROWTH PERUSAHAAN").Cells(i, 1), ActiveWorkbook.Sheets("INPUT KUANTITATIF").Range("A:AC"), j + 3, 0)) - 1
End If
Next i
Next j
End With
End Sub
这应该更有效率:
Private Sub CommandButton1_Click()
Dim ws As Worksheet
Dim wsTable As Worksheet
Dim i As Long
Dim j As Long
Dim vMatch
Set ws = ThisWorkbook.Sheets("GROWTH PERUSAHAAN")
Set wsTable = ThisWorkbook.Sheets("INPUT KUANTITATIF")
With ws
For i = 2 To 600
vMatch = Application.Match(ws.Cells(i, 1), wsTable.Range("A:A"), 0)
' if lookup value isn't found, skip
If Not IsError(vMatch) Then
For j = 3 To 100
If wsTable.Cells(vMatch, j + 3).Value2 = "" Then
ws.Cells(i, j).Value2 = 0
Else
ws.Cells(i, j).Value2 = (wsTable.Cells(vMatch, j + 4).Value2 / wsTable.Cells(vMatch, j + 3).Value2) - 1
End If
Next j
End If
Next i
End With
End Sub
请在您的代码中使用 "on error resume next"。请设置您的工作表并设置您的范围以便于理解
Private Sub CommandButton1_Click()
Dim ws As Worksheet
Dim i As Integer
Dim j As Integer
Set ws = ThisWorkbook.Sheets("GROWTH PERUSAHAAN")
Set ws1 = ThisWorkbook.Sheets("INPUT KUANTITATIF")
With ws
For j = 3 To 100
For i = 2 To 600
On Error Resume Next
If Application.WorksheetFunction.VLookup(ws.Cells(i, 1), ws1.Range("A:AC"), j + 3, 0) = "" Then
ws.Cells(i, j) = 0
Else
ws.Cells(i, j) = (Application.WorksheetFunction.VLookup(ws.Cells(i, 1), ws1.Range("A:AC"), j + 4, 0) / Application.WorksheetFunction.VLookup(ws.Cells(i, 1), ws1.Range("A:AC"), j + 3, 0)) - 1
End If
Next i
Next j
End With
End Sub
对不起,我想问一下关于行和列循环使用 vlookup
的问题。这是我的代码,但没有用。你能帮助更正这段代码吗?谢谢
Private Sub CommandButton1_Click()
Dim ws As Worksheet
Dim i As Integer
Dim j As Integer
Set ws = ThisWorkbook.Sheets("GROWTH PERUSAHAAN")
With ws
For j = 3 To 100
For i = 2 To 600
If Application.WorksheetFunction.VLookup(ThisWorkbook.Sheets("GROWTH PERUSAHAAN").Cells(i, 1), ThisWorkbook.Sheets("INPUT KUANTITATIF").Range("A:AC"), j + 3, 0) = "" Then
ThisWorkbook.Sheets("GROWTH PERUSAHAAN").Cells(i, j) = 0
Else
ThisWorkbook.Sheets("GROWTH PERUSAHAAN").Cells(i, j) = (Application.WorksheetFunction.VLookup(ThisWorkbook.Sheets("GROWTH PERUSAHAAN").Cells(i, 1), ThisWorkbook.Sheets("INPUT KUANTITATIF").Range("A:AC"), j + 4, 0) / Application.WorksheetFunction.VLookup(ThisWorkbook.Sheets("GROWTH PERUSAHAAN").Cells(i, 1), ActiveWorkbook.Sheets("INPUT KUANTITATIF").Range("A:AC"), j + 3, 0)) - 1
End If
Next i
Next j
End With
End Sub
这应该更有效率:
Private Sub CommandButton1_Click()
Dim ws As Worksheet
Dim wsTable As Worksheet
Dim i As Long
Dim j As Long
Dim vMatch
Set ws = ThisWorkbook.Sheets("GROWTH PERUSAHAAN")
Set wsTable = ThisWorkbook.Sheets("INPUT KUANTITATIF")
With ws
For i = 2 To 600
vMatch = Application.Match(ws.Cells(i, 1), wsTable.Range("A:A"), 0)
' if lookup value isn't found, skip
If Not IsError(vMatch) Then
For j = 3 To 100
If wsTable.Cells(vMatch, j + 3).Value2 = "" Then
ws.Cells(i, j).Value2 = 0
Else
ws.Cells(i, j).Value2 = (wsTable.Cells(vMatch, j + 4).Value2 / wsTable.Cells(vMatch, j + 3).Value2) - 1
End If
Next j
End If
Next i
End With
End Sub
请在您的代码中使用 "on error resume next"。请设置您的工作表并设置您的范围以便于理解
Private Sub CommandButton1_Click()
Dim ws As Worksheet
Dim i As Integer
Dim j As Integer
Set ws = ThisWorkbook.Sheets("GROWTH PERUSAHAAN")
Set ws1 = ThisWorkbook.Sheets("INPUT KUANTITATIF")
With ws
For j = 3 To 100
For i = 2 To 600
On Error Resume Next
If Application.WorksheetFunction.VLookup(ws.Cells(i, 1), ws1.Range("A:AC"), j + 3, 0) = "" Then
ws.Cells(i, j) = 0
Else
ws.Cells(i, j) = (Application.WorksheetFunction.VLookup(ws.Cells(i, 1), ws1.Range("A:AC"), j + 4, 0) / Application.WorksheetFunction.VLookup(ws.Cells(i, 1), ws1.Range("A:AC"), j + 3, 0)) - 1
End If
Next i
Next j
End With
End Sub