I'm trying to make a For loop that can pull data from specific cells but from mulitple different work books

huangapple go评论115阅读模式
英文:

I'm trying to make a For loop that can pull data from specific cells but from mulitple different work books

问题

FYI,我完全不知道我在VBA中在做什么。我对编程不是很陌生,但我的知识有限,再加上VBA对我来说是全新的。

所以我尝试的是创建一个脚本,可以访问特定文件夹中的多个不同工作簿,并从每个工作簿中提取预定单元格中的数据。所有工作簿都是测试日志,它们的格式相同,我使用的单元格ID也相同。

我知道你可以使用公式='[文件名]Sheet1'!R1C1来从特定文件的单元格A1中提取日期。我还找到了一个可以检索特定文件夹中所有文件名称的基本脚本。

Dim FSO As Object
Dim Folder As Object
Dim File As Object
Dim i As Integer

Set FSO = CreateObject("Scripting.FileSystemObject")

Set Folder = FSO.GetFolder("C:\Users\Callum\Documents\Test Logs")

For Each File In Folder.Files

    Sheets("Data").Select

    Cells(i + 1, 1) = File.Name

    i = i + 1

Next File

End Sub

所以计划是将这两者结合起来,使脚本能够检索文件名,将其保存为变量,然后使用文件名变量打印方程式。理论上,这应该打印出请求单元格中来自源工作簿的值,找到文件夹中的下一个文件,将其设置为文件名变量,然后重复打印过程,制作包含文件夹中每个工作簿中单元格A1中所有值的列表。

Dim FSO As Object
Dim Folder As Object
Dim File As Object
Dim Name As String
Dim i As Integer

Set FSO = CreateObject("Scripting.FileSystemObject")

Set Folder = FSO.GetFolder("C:\Users\Callum\Documents\Test Logs")

For Each File In Folder.Files

    Name = File.Name

    Sheets("Data").Select

    Cells(i + 1, 1) = "='[" & Name & "]Sheet1'!R4C2"

    i = i + 1

Next File

End Sub

然而,实际上似乎没有注册保存文件名的变量,因此每次运行脚本时都会要求手动选择要使用的文件。

也许我处理它的方式完全错误,也可能有更简单的方法,但再次强调,对于VBA,我是一名初学者,如果有任何帮助,将不胜感激。

英文:

FYI, I have no idea what I’m doing in VBA. I’m not new to coding in general but my knowlage isn't that great and to add to it VBA is completely new to me.


So what I’m trying to do is create a script that can access several different workbooks in a specific folder and pull data from a pre-determined cell on each workbook. All of the workbooks are test logs so their format is identical and so are the cell ID I am using.

I know you can use the formula ='[File_Name]Sheet1'!R1C1 to pull the date out of cell A1 on that specific file. And I found a basic script that could retrieve the names of all files in a specific folder.

Dim Folder As Object
Dim File As Object
Dim i As Integer

Set FSO = CreateObject("Scripting.FileSystemObject")

Set Folder = FSO.GetFolder("C:\Users\Callum\Documents\Test Logs")

For Each File In Folder.Files

    Sheets("Data").Select

    Cells(i + 1, 1) = File.Name

    i = i + 1

Next File

End Sub

So the plan was to combine the two so that the script would retrieve the file name, save it as a variable, and then print the equation using the file name variable. This should have in theory printed the value from the requested cell on the source workbook, found the next file in the folder, set it to the fine name variable and repeated the printing process making a list of all values in cell A1 from every workbook in the folder.

Dim Folder As Object
Dim File As Object
Dim Name As String
Dim i As Integer

Set FSO = CreateObject("Scripting.FileSystemObject")

Set Folder = FSO.GetFolder("C:\Users\Callum\Documents\Test Logs")

For Each File In Folder.Files

    Name = File.Name

    Sheets("Data").Select

    Cells(i + 1, 1) =  "='[Name]Sheet1'!R4C2"

    i = i + 1

Next File

End Sub

Instead, it didn’t appear that the variable holding the file name was registered when printing the formula so every time you ran the script it would ask you to manually select the file you wanted to use.

Perhaps the way I'm going about it is compleatly wrong and there is a much simpler method but again I'm a novice among novices when it comes to VBA so any assistance would be appreciated.

答案1

得分: 2

问题:

  • VBA不支持变量扩展。一种常见的方法是连接字符串
  • 你需要完整的文件路径,而不仅仅是文件名。
  • 考虑使用一个与Name不同的变量名,因为它会遮蔽Name语句。但在这里甚至不需要这个变量。
  • 在这里明确说明你正在编写一个R1C1样式的公式是一个好习惯:
Dim path As String
path = "C:\Users\Callum\Documents\Test Logs\" ' 注意末尾的反斜杠

Set Folder = FSO.GetFolder(path)
For Each File In Folder.Files
    Dim f As String
    f = "=''" & path & "[" & File.Name & "]Sheet1''!R4C2"

    ThisWorkbook.Worksheets("Data").Cells(i + 1, 1).FormulaR1C1 = f
    i = i + 1
Next File
英文:

Issues:

  • VBA doesn't do variable expansion. A common approach is to concatenate.
  • You need the full filepath, not just the filename.
  • Consider using a different variable name than Name since that shadows the Name statement. But you don't even need the variable here.
  • It's good to be explicit that you're writing a R1C1-style formula here:
Dim path As String
path = "C:\Users\Callum\Documents\Test Logs\" ' note the ending backslash

Set Folder = FSO.GetFolder(path)
For Each File In Folder.Files
    Dim f As String
    f = "='" & path & "[" & File.Name & "]Sheet1'!R4C2"

    ThisWorkbook.Worksheets("Data").Cells(i + 1, 1).FormulaR1C1 = f
    i = i + 1
Next File

huangapple
  • 本文由 发表于 2023年8月11日 03:43:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76878873.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定