打开文件夹中的每个子文件夹,打开xls文件并插入图像

时间:2018-05-22 13:04:09

标签: excel vba excel-vba

我需要一些VBA / Excel代码的帮助。我有一个包含10个子文件夹的文件夹:

  • 10
  • 15
  • 20
  • 25 ...

每个子文件夹都有5个文件:

  • inserthere.xls
  • front.png
  • back.png
  • left.png
  • right.png

我需要在所有子文件夹中的所有.xls中插入4张图像。

我认为这样的事情,但我不懂语法知识:

read variable path from a cell or
initial_path = "C:/something/";
counter = 10;
i = 1;
for i=1:10
path = initial_path + counter;
open path
open inserthere.xls

insert image 1
insert image 2
insert image 3
insert image 4

i = i + 1;
counter = counter + 5;
end;

提前感谢您的帮助:) 祝你今日愉快!

1 个答案:

答案 0 :(得分:0)

漂亮的伪代码 - 非常接近真实的东西。以下是我认为您正在寻找的内容 - 请注意,这并不是在任何特定的地方插入图片,如果它们的大小相同,它们可能会叠加在一起。同样。

仅部分测试:

Sub Test()

Dim initial_path As String, new_path As String
Dim i As Long, counter As Long

initial_path = "C:\something\"
counter = 10

For i = 1 To 10
    new_path = initial_path & counter

    Workbooks.Open Filename:=new_path & "\inserthere.xls"

    ActiveSheet.Pictures.Insert (new_path & "\image1.png")
    ActiveSheet.Pictures.Insert (new_path & "\image2.png")
    ActiveSheet.Pictures.Insert (new_path & "\image3.png")
    ActiveSheet.Pictures.Insert (new_path & "\image4.png")

    'Close and save changes
    Workbooks("inserthere.xls").Close True

    i = i + 1
    counter = counter + 5
Next i

End Sub

编辑:如果将所有图片插入一个文件:

Sub Test()

Dim initial_path As String, new_path As String
Dim i As Long, counter As Long

initial_path = "C:\something\"
counter = 10

Workbooks.Open Filename:=new_path & "\inserthere.xls"

For i = 1 To 10
    new_path = initial_path & counter

    ActiveSheet.Pictures.Insert (new_path & "\image1.png")
    ActiveSheet.Pictures.Insert (new_path & "\image2.png")
    ActiveSheet.Pictures.Insert (new_path & "\image3.png")
    ActiveSheet.Pictures.Insert (new_path & "\image4.png")

    i = i + 1
    counter = counter + 5
Next i

'Close and save changes
Workbooks("inserthere.xls").Close True

End Sub

这是假设您的inserthere.xls文件位于C:\something\10\文件夹位置。