本文介绍了VBA访问 - 环Excel工作簿的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的VBA打开一个Excel工作簿检查,看其是否在只读模式,如果是则等待,直到读/写处于活动状态,然后运行code。简单

我的问题是,我有很多的Excel文件,如:C:\ TEST \ TEST.xlsb,C:\ TEST \ TEST2.xlsb,C:\ TEST \ TEST3.xlsb,C:\ TEST \ TEST4.xlsb等

我如何获得VBA运行通过每个工作簿的名称code

救我一次复制/粘贴code多次为每个工作簿的名称。

 功能测试()

昏暗的XL作为对象

设置XL =的CreateObject(Excel.Application)

xl.Workbooks.Open(C:\ TEST \ Test.xlsb)

难道直到xl.ActiveWorkbook.ReadOnly = FALSE

MSGBOX(工作簿中的使用,等到读/写处于活动状态)
呼叫暂停(5)
xl.Quit
xl.Workbooks.Open(C:\ TEST \ Test.xlsb)
如果xl.ActiveWorkbook.ReadOnly = false,那么退出待办事项
循环


如果xl.ActiveWorkbook.ReadOnly = false,那么

MSGBOX(读/写主动)

运行code
xl.Sheets(工作表Sheet1)。范围(G2)。值= 2222
xl.ActiveWorkbook.Save
xl.Quit

结束如果

xl.Quit
设置XL =什么

端功能
 

解决方案

你需要一个循环来遍历文件和调用该函数的每个文件。如果你的文件名的模式是这样的:C:\ TEST \ TEST.xlsb,C:\ TEST \ TEST2.xlsb,C:\ TEST \ TEST3.xlsb,C:\ TEST \ TEST4.xlsb再例如,你可以实现另外的功能打造的文件名作为字符串,并将其设置为你的函数的放慢参数测试

 功能goThroughFiles()
 昏暗的正面为String
 暗淡的结局为String
 昏暗我作为整型
 昏暗strPath的为String

 前面= C:\ TEST \ TEST
 结束= .xlsb
 I = 1

 //启动与文件名中有没有号码
 strPath的=前与放大器;结束

 //检查文件是否存在
 难道直到莱恩(迪尔(strPath中))= 0
    通话测试(strPath中)
    I = I + 1
    strPath的=前与放大器; I和结束
 循环
 

您需要改变你原有的功能,所以它接受一个参数:

 功能测试(strPath中为String)
 

和替代使用的路径与你的输入变量的每一行

  xl.Workbooks.Open(C:\ TEST \ Test.xlsb)
 

变为

  xl.Workbooks.Open(strPath中)
 

The following vba opens an excel workbook checks to see if its in read-only mode, if it is then waits till read/write is active then runs code. Simple

My Questions is that I have a lot of excel files e.g C:\TEST\TEST.xlsb, C:\TEST\TEST2.xlsb ,C:\TEST\TEST3.xlsb ,C:\TEST\TEST4.xlsb etc

How do I get the VBA to run code through each workbook name

Save me time copy/pasting code several times for each workbook name.

Function test()

Dim xl As Object

Set xl = CreateObject("Excel.Application")

xl.Workbooks.Open ("C:\TEST\Test.xlsb")

Do Until xl.ActiveWorkbook.ReadOnly = False

MsgBox ("Workbook in use, waiting till read/write is active")
Call Pause(5)
xl.Quit
xl.Workbooks.Open ("C:\TEST\Test.xlsb")
If xl.ActiveWorkbook.ReadOnly = False Then Exit Do
Loop


If xl.ActiveWorkbook.ReadOnly = False Then

MsgBox ("read/write active")

'Run code
xl.Sheets("Sheet1").Range("G2").Value = 2222
xl.ActiveWorkbook.Save
xl.Quit

End If

xl.Quit
Set xl = Nothing

End Function
解决方案

you need a loop to go through your files and call the function for every file.if the pattern of your filenames is something like this C:\TEST\TEST.xlsb, C:\TEST\TEST2.xlsb ,C:\TEST\TEST3.xlsb ,C:\TEST\TEST4.xlsbthen you could for example implement another function to build the filename as a string and set it as a paramter of your function "test"

 Function goThroughFiles()
 Dim front as String
 Dim ending as String
 Dim i as Integer
 Dim strPath as String

 front = C:\TEST\TEST
 ending = .xlsb
 i = 1

 // start with filename that has no number in it
 strPath = front & ending

 // check if file exists
 Do Until Len(Dir(strPath)) = 0
    call test(strPath)
    i = i+1
    strPath = front & i & ending
 Loop

you need to change your original function, so it accepts a parameter:

  Function test(strPath as String)

and substitute every line that uses the path with your input variable

  xl.Workbooks.Open ("C:\TEST\Test.xlsb")

becomes

  xl.Workbooks.Open (strPath)

这篇关于VBA访问 - 环Excel工作簿的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 00:25