简体   繁体   English

MS Access VBA 格式 Excel

[英]MS Access VBA format Excel

Hi I am trying to format an excel spread sheet created by my MS access macro.嗨,我正在尝试格式化由我的 MS 访问宏创建的 Excel 电子表格。 I wanted to select rows with only values in it.我想选择只有值的行。 So for example I want to select the first row and text wrap it所以例如我想选择第一行并用文字包裹它

I thought this logic would work, but gives me error 1004 (Application-defined or Object defined Error)我认为这个逻辑会起作用,但给了我错误 1004(应用程序定义或对象定义错误)

Dim my_xl_app As Object
Dim my_xl_workbook As Object

Set my_xl_app = CreateObject("Excel.Application")
Set my_xl_workbook = my_xl_app.Workbooks.Open(C:\PATH)

For x = 1 To 23
my_xl_workbook.sheets(x).Range("A1",my_xl_workbook.sheets(x).Range("A1").End(xlToright)).WrapText = True
Next x

my_xl_workbook.Sheets(x).Range("A1", my_xl_workbook.Sheets(x).Range("A1").End(xlToRight)).WrapTex‌​t = True is what is being highlighted when I press debug my_xl_workbook.Sheets(x).Range("A1", my_xl_workbook.Sheets(x).Range("A1").End(xlToRight)).WrapTex‌​t = True是我按下调试键时突出显示的内容

Thanks in advance提前致谢

You are probably not closing properly the file, thus it stays opened and unvisible.您可能没有正确关闭文件,因此它保持打开状态且不可见。 Check in your task manager how many excel files do you have opened.在您的任务管理器中检查您打开了多少个 excel 文件。 Try to close them all.尝试将它们全部关闭。 Furthermore, you refer to xlToRight , which is member of the MS Excel Object Library , which is not present in your application.此外,您引用了xlToRight ,它是MS Excel Object Library成员,它不存在于您的应用程序中。

Thus, try the following:因此,请尝试以下操作:

Public Sub TestMe()

    Dim x               As Long
    Dim my_xl_app       As Object
    Dim my_xl_workbook  As Object

    Set my_xl_app = CreateObject("Excel.Application")
    Set my_xl_workbook = my_xl_app.Workbooks.Open("C:\Users\v.doynov\Desktop\file.xlsx")

    my_xl_app.Visible = True

    For x = 1 To my_xl_workbook.Sheets.Count
        With my_xl_workbook.Sheets(x)
            .Range("A1", .Range("A1").End(xlToRight)).WrapText = True

            Debug.Print "Wrapping " & .Range("A1", .Range("A1").End(-4161)).Address & _
            " From " & .Range("A1", .Range("A1").End(-4161)).Parent.Name

        End With
    Next x

    my_xl_workbook.Save
    my_xl_workbook.Close (True)

End Sub

This is how I found -4161 .这就是我找到-4161 Add a reference to MS Excel 14.0 Object Library in the Visual Basic Editor.在 Visual Basic 编辑器中添加对MS Excel 14.0 Object Library的引用。

在此处输入图片说明

Then in the immediate window write ?xlToRight .然后在立即窗口中写入?xlToRight Thats quite enough.这已经足够了。

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

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