excel - 如何在 vba 中放置正确的 vlookup 范围(其他工作表)?
问题描述
我在 VBA 中尝试这段代码,但在vlookup
某种程度上,VBA 没有带来任何东西。“ Pareo Cecos ”是工作簿中的另一张纸。
如何在 VBA 中放置正确的 vlookup 范围(其他工作表)?
Sub Macro3()
' --------------
' Macro3 Macro
' --------------
Worksheets("Altas").Select
ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-9],'Pareo Cecos'!C[1]:C[4],3,0)"
Range("M3").Select
Selection.AutoFill Destination:=Range("M3", "M" & Cells(Rows.Count, 1).End(xlUp).Row)
Range("M3", "M" & Cells(Rows.Count, 1).End(xlUp).Row).Select
Selection.Copy
Range("D3", "D" & Cells(Rows.Count, 1).End(xlUp).Row).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("M3", "M" & Cells(Rows.Count, 1).End(xlUp).Row).Select
Application.CutCopyMode = False
Selection.ClearContents
Range("A2").Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.Copy
Range(Selection, Selection.End(xlDown)).Select
Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Rows("2:2").Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlUp
End Sub
解决方案
我已经在复制的原始代码行之间编写了工作代码以及一些注释。实际上,代码将按原样工作(或者我希望如此),但如果您删除仅用于演示的行,它会看起来更好。
Sub Macro3()
' 247
Dim Altas As Worksheet
Dim Rng As Range
' Worksheets("Altas").Select ' don't Select any thing
Set Altas = Worksheets("Altas")
' the ActiveCell could be anywhere. Specify the address
' ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-9],'Pareo Cecos'!C[1]:C[4],3,0)"
Set Rng = Worksheets("Pareo Cecos").Range("A1:C4") ' specify the lookup range
With Altas
' I assume the cell to be M3
.Cells(3, "M").Formula = "=VLOOKUP($M3, " & Rng.Address & ", 3)"
'Range("M3").Select ' not required
' define the destination range (to be used thrice)
Set Rng = .Range("M3", "M" & .Cells(Rows.Count, 1).End(xlUp).Row)
'Selection.AutoFill Destination:=Range("M3", "M" & Cells(Rows.Count, 1).End(xlUp).Row)
.Cells(3, "M").AutoFill Destination:=Rng
'Range("M3", "M" & Cells(Rows.Count, 1).End(xlUp).Row).Select
'Selection.Copy
Rng.Copy
'Range("D3", "D" & Cells(Rows.Count, 1).End(xlUp).Row).Select
Debug.Print Rng.Offset(0, -9).Address
'Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
' :=False, Transpose:=False
Rng.Offset(0, -9).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
'Range("M3", "M" & Cells(Rows.Count, 1).End(xlUp).Row).Select
'Selection.ClearContents
Rng.ClearContents
'Range("A2").Select
Set Rng = .Range(.Cells(2, "A"), .Cells(2, .Columns.Count).End(xlToLeft))
'Range(Selection, Selection.End(xlToRight)).Select
'Selection.Copy
Rng.Copy Destination:=.Cells(.Rows.Count, "A").End(xlUp).Offset(1)
'Range(Selection, Selection.End(xlDown)).Select
'Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
' SkipBlanks:=False, Transpose:=False
'Rows("2:2").Select
'Application.CutCopyMode = False
'Selection.Delete Shift:=xlUp
End With
End Sub
推荐阅读
- java - 在 Kotlin 中使用 setter 实现 Java getter 接口
- python - 使用 Django,如何在下载后立即删除文件而不刷新页面?
- locale - 在 macOS High Sierra 上下载 syms 包
- python - 根据列表排序列表列表 - python
- r - 删除堆积条形图中的空条
- ios - 如何在 swift 中使用滑块向 UIImageView 显示多个图像 url 字符串
- python - Mac OS X预装的python2.7中这三个“python”有什么区别?
- laravel - Laravel 从关系中过滤数据
- r - 更改列名时出错,名称的属性 [1] 必须与向量 [0] 的长度相同
- python - Python venv 文件夹究竟是如何工作的?我的项目使用依赖于这个 venv 文件夹使用不同的 Python 版本执行项目?