简体   繁体   English

VBa vlookup ,将范围替换为变量

[英]VBa vlookup , replace range as variable

OK I will reformulate the question.好的,我会重新表述这个问题。

I have a worksheet with a tab named"My INT".我有一个带有名为“My INT”的选项卡的工作表。 This tab contains a data table and a button with an assigned macro called "importRMR".该选项卡包含一个数据表和一个带有名为“importRMR”的指定宏的按钮。 code below:代码如下:

Sub importRMR()
Dim rng As Range

Set rng = ActiveSheet.Range("G3")
Sheets.Add(After:=ActiveSheet).Name = "RMR " & Format(Date, "DD-MM-YY")
ActiveSheet.Buttons.Add(966.75, 27.75, 153.75, 125.25).Select
Selection.OnAction = "Cimp"

Selection.Characters.Text = "Importuj"
With Selection.Characters(Start:=1, Length:=13).Font
    .Name = "Tahoma"
    .FontStyle = "Standaard"
    .Size = 16
    .Strikethrough = False
    .Superscript = False
    .Subscript = False
    .OutlineFont = False
    .Shadow = False
    .Underline = xlUnderlineStyleNone
    End With
End Sub

This creates a tab with a specific name :RMR " & Format(Date, "DD-MM-YY")这将创建一个具有特定名称的选项卡 :RMR " & Format(Date, "DD-MM-YY")

NOw I manually paste the table( always identical ) to the newly created tab "RMR 03/08/2018".现在我手动将表格(始终相同)粘贴到新创建的选项卡“RMR 03/08/2018”。 the next step is to click the button in this tab ( Importuj) and the result I am aiming for is to : 1. Vlookup data in tab "my INT" where the range( lookup array) is tab "RMR" and then delete the rmr tab.下一步是单击此选项卡中的按钮 (Importuj),我的目标是: 1. 在选项卡“my INT”中查找数据,其中范围(查找数组)是选项卡“RMR”,然后删除rmr 选项卡。

such procedures like triming the data pasting values and so on I am able to do, just the vlookup but is a problem.像修剪数据粘贴值之类的程序我可以做,只是 vlookup 但是是一个问题。

my current code for "importuj " button is: ub TEST()我当前的“importuj”按钮代码是:ub TEST()

Dim DOTR As String

Dim shT As String
Set shT = Sheets(DOTR).Range("E2:H584")




'shT = Sheets(DOTR).Range("c1:e2").Select






DOTR = "RMR " & Format(Date, "DD-MM-YY")
'Sheets(DOTR).Range ("E2:H584").selc


Worksheets("My INT").Range("N3").Formula = "=vlookup(c3,sht,3,0)"


End Sub

Unfortunately, I get an error - "Compile Error" - Object required.不幸的是,我收到一个错误 - “编译错误” - 需要对象。

The goal is to combine several different strings into a one cohesive string:目标是将几个不同的字符串组合成一个有凝聚力的字符串:

=VLOOKUP(C3,'ABCDEF 03-08-18'!$B$4:$D$10,3,0)

That will be placed inside a cell using:这将使用以下方法放置在单元格内:

Worksheet.Range.Formula

The worksheet name "ABCDEF" is arbitrary and we create the date on the fly.工作表名称“ABCDEF”是任意的,我们可以即时创建日期。 We use a named range in Excel "myNamedRange" and reference the address property to allow flexibility without editing code.我们在 Excel 中使用命名范围“myNamedRange”并引用地址属性,以便在不编辑代码的情况下实现灵活性。

Dim strSheetName As String
Dim strNamedRange As String
Dim strDateSegment As String
Dim strPrefix As String
Dim shT As String

strSheetName = "My INT"
strNamedRange = "myNamedRange"
strDateSegment = Format(Day(Date), "00") & "-" & Format(Month(Date), "00") & "-" & Right(Year(Date), 2) & "'!"
strPrefix = "'" & "ABCDEF"

shT = "=VLOOKUP(C3," & strPrefix & strDateSegment & Worksheets(strSheetName).Range(strNamedRange).Address & ",3,0)"

Worksheets("My INT").Range("N3").Formula = shT

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

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