简体   繁体   English

带复制/粘贴功能的Vlookup-VBA

[英]Vlookup with Copy/Paste - VBA

I am fairly new to VBA and still finding my way around the codes! 我对VBA还是很陌生,仍然可以在代码中找到自己的方式!

I want to create a Vlookup within an IF statement. 我想在IF语句中创建Vlookup I want to check the manufacturer first (Cell B11) , then using the selected device (Cell C11) lookup a range on a separate tab ("List", Range O3:Q11) and copy/paste the $ value (Cell P3) onto the initial tab in a new cell (Cell L11) . 我想先检查制造商(Cell B11) ,然后使用选定的设备(Cell C11)在单独的选项卡(“ List”,范围O3:Q11)上查找范围,然后将$值复制/粘贴到(Cell P3)上新单元格(单元格L11)中的初始标签。 As I said I am fairly new to VBA, but here is some code I have attempted: 正如我所说的,我对VBA还是很陌生,但是这里尝试了一些代码:

Sub ServiceCosts()

Dim SamsungServ As Range
Dim Manufacturer As Range
Dim Device As Range
Dim MonoSamService As String

Set SamsungServ = Sheets("List").Range("O3:Q11")
Set Manufacturer = Range("B11")
Set Device = Range("C11")

If Manufacturer = "Samsung" Then
MonoSamService = Application.WorksheetFunction.VLookup(Device, SamsungServ, 2, False)
ActiveCell.Copy
Sheets("Finance Calculations").Range("L11").Select
ActiveCell.PasteSpecial Paste:=xlPasteValues


Else
MsgBox "Please Select Device"

End If

End Sub

Any help at all will be very much appreciated!!! 任何帮助将不胜感激!!!

Thanks 谢谢

It looks to me like you're assuming MonoSamService is the 'ActiveCell' which it won't be unless you specifically tell it to. 在我看来,您假设MonoSamService是“ ActiveCell”,除非您明确告知,否则它不会。

Instead, I would do something like: 相反,我会做类似的事情:

MonoSamService = Application.WorksheetFunction.VLookup(Device, SamsungServ, 2, False) Sheets("Finance Calculations").Range("L11").Value = MonoSamService MonoSamService = Application.WorksheetFunction.VLookup(Device,SamsungServ,2,False)Sheets(“财务计算”).Range(“ L11”)。Value = MonoSamService

Ie. 就是 set the value of the cell in Finance Calculations to what you grabbed from the vlookup. 将财务计算中单元格的值设置为您从vlookup中获取的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM