当我不在Excel中时,如何定义一个范围对象?

发布于 2025-01-28 12:02:33 字数 666 浏览 2 评论 0 原文

我正在研究MS Project中的VBA脚本,其中涉及创建工作簿。但是我在定义一个范围时做错了什么,而且它的工作方式似乎有所不同,因为我不在Excel中。

这是我当前的代码,归结为有问题的部分:

Dim myExcel As Object
Dim myWb As Object
Dim myRange as Object 'Not sure if I should declare this one as an object or a range
Dim myRangeString as String

Set myExcel = CreateObject("Excel.Application")
Set myWb = myExcel.Workbooks.Add

myRangeString = "$D$20, $C$23" 'For simplicity. Obviously this is not hard coded.

Set myRange = Range(myRangeString)

此代码在Excel中正常工作。但是在项目中,它停止并返回“运行时错误'1004':方法'对象'_global'失败。”。也许是因为在这种情况下“全局”是项目,而不是出色。因此,我尝试了myexcel.range(myrangestring)的不同变体,但是后来我得到了“应用程序定义或对象定义的错误”。反而。

谁能帮忙?

I'm working on a VBA script in MS Project, which involves the creation of a workbook. But I'm doing something wrong when defining a range, and it seems like it works differently because I'm not in Excel.

This is my current code, boiled down to the problematic part:

Dim myExcel As Object
Dim myWb As Object
Dim myRange as Object 'Not sure if I should declare this one as an object or a range
Dim myRangeString as String

Set myExcel = CreateObject("Excel.Application")
Set myWb = myExcel.Workbooks.Add

myRangeString = "$D$20, $C$23" 'For simplicity. Obviously this is not hard coded.

Set myRange = Range(myRangeString)

This code works fine in Excel. But in Project it stops and throws back "Run-time error '1004': Method 'Range' of object '_Global' failed.". Maybe that's because "Global" in this context is Project and not Excel. So I have tried different variations of MyExcel.Range(myRangeString), but then I get "Application-defined or object-defined error." instead.

Can anyone help?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

枫林﹌晚霞¤ 2025-02-04 12:02:33

我似乎已经或多或少地解决了它的答案, @orange_guy和@rachel。解决方案是使用分号(不是结肠,而不是逗号)。反正非常感谢!

I seem to have solved it more or less at the same as you posted your answers, @orange_guy and @Rachel. The solution was to use semicolon (not colon, and not comma). Thanks a lot anyways!

过期情话 2025-02-04 12:02:33

您的代码使用较晚的绑定,这意味着对象在运行时是未知的,并且没有 intelliSense 。使用后期绑定的优点是,该代码适用于具有较旧版本的Office的用户。否则,它比用户早期绑定更可取。 (请参阅。)

通过设置对Excel对象库的引用(在VB编辑器,工具:参考,向下滚动并选中Microsoft Excel对象库)的框来使用早期绑定。

这是一个工作的MS Project示例使用后期绑定:使用阵列在块中脱颖而出的代码

Sub ExportToExcelLateBindingCellByCell()

Dim myExcel As Object
Dim myWb As Object

Set myExcel = CreateObject("Excel.Application")
myExcel.Visible = True
Set myWb = myExcel.Workbooks.Add

myWb.Worksheets(1).Range("$D$20") = ActiveCell.Task.Name
myWb.Worksheets(1).Range("$C$23") = ActiveCell.Task.ID

End Sub

是一个好主意 - 也许这就是“ $ d $ d $ 20,$ c $ 23”的意图。但是,正确的范围语法是使用结肠,但是D20:C23尴尬,使用C20:D23。

这是一个早期结合示例,该示例在块中写入数据,对于大量数据而言,它明显更快。

Sub ExportToExcelEarlyBindingArray()

Dim myExcel As Excel.Application
Dim myWb As Excel.Workbook

Set myExcel = CreateObject("Excel.Application")
myExcel.Visible = True
Set myWb = myExcel.Workbooks.Add

Dim TaskInfo(4, 2) As Variant
Dim i As Integer
For i = 1 To 4
    TaskInfo(i - 1, 0) = ActiveProject.Tasks(i).ID
    TaskInfo(i - 1, 1) = ActiveProject.Tasks(i).Name
Next i

myWb.Worksheets(1).Range("$D$20:$C$23") = TaskInfo

End Sub

You code is using late binding which means that the objects are unknown until run-time and there is no IntelliSense. The advantage to using late binding is that the code will run fine for users with older versions of Office. Otherwise it's preferable to user early binding. (See Early and Late Binding.)

Use early binding by setting a reference to the Excel Object Library (in the VB Editor, Tools: References, scroll down and check the box for the Microsoft Excel Object Library)

Here's a working MS Project example of your code using late binding:

Sub ExportToExcelLateBindingCellByCell()

Dim myExcel As Object
Dim myWb As Object

Set myExcel = CreateObject("Excel.Application")
myExcel.Visible = True
Set myWb = myExcel.Workbooks.Add

myWb.Worksheets(1).Range("$D$20") = ActiveCell.Task.Name
myWb.Worksheets(1).Range("$C$23") = ActiveCell.Task.ID

End Sub

Writing to Excel in chunks using arrays is a great idea--perhaps that's what was intended by "$D$20, $C$23". However, the correct range syntax would be to use a colon, but D20:C23 is awkward, use C20:D23 instead.

Here is an early binding example that writes data in chunks, which is noticeably faster for large amounts of data.

Sub ExportToExcelEarlyBindingArray()

Dim myExcel As Excel.Application
Dim myWb As Excel.Workbook

Set myExcel = CreateObject("Excel.Application")
myExcel.Visible = True
Set myWb = myExcel.Workbooks.Add

Dim TaskInfo(4, 2) As Variant
Dim i As Integer
For i = 1 To 4
    TaskInfo(i - 1, 0) = ActiveProject.Tasks(i).ID
    TaskInfo(i - 1, 1) = ActiveProject.Tasks(i).Name
Next i

myWb.Worksheets(1).Range("$D$20:$C$23") = TaskInfo

End Sub
初吻给了烟 2025-02-04 12:02:33

就像@bigben和@darren一样,我也没有MS项目,但是我在Excel和Outlook中也有类似的交叉应用vba脚本,并在对我来说非常有效。这是片段:

Public Sub XPTO()

Dim xExcelFile As String
Dim xExcelApp As Excel.Application
Dim xWb As Excel.Workbook
Dim xWs As Excel.Worksheet
Dim xExcelRange As Excel.Range
   
'Get the dimension table
xExcelFile = "C:\path\toMyFile\File.xlsm"
Set xExcelApp = CreateObject("Excel.Application")
Set xWb = xExcelApp.Workbooks.Open(xExcelFile)
Set WKB = xExcelApp.ActiveWorkbook
Set xExcelRange = xWb.Sheets("Sheet1").Range("myTable").ListObject

因此,基本上我在任何变量之前都提及该应用程序。另外,可能有效的一件事是 set 您提到的变量(“ myrangestring”)作为对象。我不知道为什么,但是碰巧对我有用。因此,代替:

myRangeString = "$D$20, $C$23"

您会放一些东西:

set myRangeString = myWb.Sheet(1).Range("$D$20, $C$23")

Just like @BigBen and @Darren, I don't have MS Project either, but I have a similar crossed-application VBA Script among Excel and Outlook and putting the "app" before worked quite well for me. Here's the snippet:

Public Sub XPTO()

Dim xExcelFile As String
Dim xExcelApp As Excel.Application
Dim xWb As Excel.Workbook
Dim xWs As Excel.Worksheet
Dim xExcelRange As Excel.Range
   
'Get the dimension table
xExcelFile = "C:\path\toMyFile\File.xlsm"
Set xExcelApp = CreateObject("Excel.Application")
Set xWb = xExcelApp.Workbooks.Open(xExcelFile)
Set WKB = xExcelApp.ActiveWorkbook
Set xExcelRange = xWb.Sheets("Sheet1").Range("myTable").ListObject

So basically I mention the App before any variable. Also, one thing that might work is to set the variable you mentioned ("myRangeString") as an object. I don't know exactly why, but it happenned to work for me. So instead of:

myRangeString = "$D$20, $C$23"

You would put something alike:

set myRangeString = myWb.Sheet(1).Range("$D$20, $C$23")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文