简体   繁体   English

DoWhile循环中vlookup公式中的Excel VBA引用命名为工作簿

[英]Excel VBA reference named workbook in vlookup formula in DoWhile loop

This seems like it should be simple, but I'm really stuck on it. 这看起来应该很简单,但是我真的很坚持。

This is code to get values from multiple workbooks in a directory and compile them in one workbook. 这是用于从目录中的多个工作簿获取值并将其编译到一个工作簿中的代码。 I am using vlookup because the value is not always in the exact same cell, but always has the same row header. 我使用vlookup是因为该值并不总是在完全相同的单元格中,而是始终具有相同的行标题。 The code opens each workbook in the directory and names it wb. 该代码将打开目录中的每个工作簿,并将其命名为wb。

I want to reference wb in the Vlookup formula but am having trouble doing it. 我想在Vlookup公式中引用wb,但是这样做很麻烦。 When I try the code below it gives me a "loop without do" error (but when I run the same code without the formula, just copying and pasting ranges, it is fine). 当我尝试下面的代码时,它给我一个“不做循环的错误”(但是当我运行没有公式的相同代码时,只需复制和粘贴范围,就可以了)。 I've also tried naming a range in wb and referencing that. 我也尝试过在wb中命名一个范围并引用它。

'Target File Extension
  myExtension = "*.xls"

'Target Path with Ending Extention
 myFile = Dir(myPath & myExtension)

'Loop through each Excel file in folder
  Do While myFile <> ""

'Set variable equal to opened workbook
  Set wb = Workbooks.Open(fileName:=myPath & myFile)

'Put ID from filename into sheet
  If right(wb.Name, 5) = "h.xls" Then
    wb.Worksheets(1).Range("B16").Select
    ActiveCell.Value = Left(wb.Name, 9)

'Get value from wb using Vlookup
  MasterBook.Activate
    ActiveCell.Offset(1, 0).Select
    ActiveCell.FormulaR1C1 = "=VLOOKUP(""Sugar (g)"",wb!R18C1:R28C6,2,FALSE)"
'Close Workbook
  wb.Close SaveChanges:=True

'Get next file name
  myFile = Dir
Loop

'Message Box when done
  MsgBox "Task Complete!"

You cannot use the Range object directly when putting together a string to be used as a formula. 将字符串用作公式时,不能直接使用Range对象 Concatenate the Range.Address property into the string. Range.Address属性连接到字符串中。

ActiveCell.FormulaR1C1 = "=VLOOKUP(""Sugar (g)"", " &  _
                      wb.Worksheets(1).Range("A18:F28").address(External:=true, ReferenceStyle:=xlR1C1) & _
                      ", 2, FALSE)"
'or,
ActiveCell.Formula = "=VLOOKUP(""Sugar (g)"", " &  _
                      wb.Worksheets(1).Range("A18:F28").address(External:=true) & _
                      ", 2, FALSE)"

For the formula, you need to add the name of the workbook into the string. 对于公式,您需要将工作簿的名称添加到字符串中。 You can't reference the variable wb while it is part of the string. 当变量wb是字符串的一部分时,您不能引用它。 You also need to reference the workbook and the sheet name. 您还需要引用工作簿和工作表名称。 完全合格的名称

So I'd suggest something like the following to get those names: 因此,我建议使用类似以下内容的名称:

ActiveCell.FormulaR1C1 = "=VLOOKUP(""Sugar (g)"",[" & wb.name & "]" & wb.Worksheet(1).Name & "!R18C1:R28C6,2,FALSE)"

What I do in these kinds of situations is keep removing parts of code until it works or gives a different error message or something. 在这种情况下,我要做的是不断删除部分代码,直到它起作用或给出不同的错误消息或其他内容为止。 That way you can narrow down to where the error is. 这样,您可以缩小到错误所在的位置。

The Do While .. Loop looks fine, I think you're missing an End If Do While .. Loop看起来不错,我认为您错过了End If

If Right(wb.Name, 5) = "h.xls" Then
  wb.Worksheets(1).Range("B16").Select
  ActiveCell.Value = Left(wb.Name, 9)
End If

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

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