从MC访问表单到Excel文件的导出列表框值维护相同数量的列

发布于 2025-01-23 09:27:39 字数 1123 浏览 2 评论 0原文

因此,我设法创建了一个代码,以将和粘贴列表框值复制到新创建的Excel文件。 问题是,我将其全部与逗号相连和分开。它可以正常工作,但是由于它的导出方式,因此我必须将Excel文本使用到列功能上的功能,才能像我想要的那样放置数据。

这是代码:

Private Sub button_Export_AMAT_Click()

Dim i As Integer
Dim n As Integer

Dim strLine As String
Set fs = CreateObject("Scripting.FileSystemObject")
Set a = fs.CreateTextFile("\\yvavm301\Commun\CF_GDC\base de dados a trabalhar\AM_AT.csv", True)

    For i = 0 To Me.List_AM_AT.ListCount - 1

            For n = 0 To Me.List_AM_AT.ColumnCount - 1

             strLine = strLine & """" & Me.List_AM_AT.Column(n, i) & ""","
           
            Next n

        strLine = Left(strLine, Len(strLine) - 1)
        a.writeline (strLine)
        strLine = ""

    Next i

    MsgBox "Your file is exported"
    
End Sub

我的问题是:是否有可能出现类似的表格,即。具有相同数量的列并将其填充有正确的值?

更改必须在此处进行(请参见下文),对吗?

strLine = strLine & """" & Me.List_AM_AT.Column(n, i) & ""","
a.writeline (strLine)

我尝试过没有运气以下操作:

strLine = Me.List_AM_AT.Column(n, i)    
a.cells(i,n).writeline (strLine)

有人知道该怎么办吗?

So I managed to create a code to copy and paste listbox values to a newly created excel file.
The thing is, I have it all concatenated and separated by a comma. It works fine but because of how it is exported, then I have to use Excel text to columns functionality to put the data like I want.

Here's the code:

Private Sub button_Export_AMAT_Click()

Dim i As Integer
Dim n As Integer

Dim strLine As String
Set fs = CreateObject("Scripting.FileSystemObject")
Set a = fs.CreateTextFile("\\yvavm301\Commun\CF_GDC\base de dados a trabalhar\AM_AT.csv", True)

    For i = 0 To Me.List_AM_AT.ListCount - 1

            For n = 0 To Me.List_AM_AT.ColumnCount - 1

             strLine = strLine & """" & Me.List_AM_AT.Column(n, i) & ""","
           
            Next n

        strLine = Left(strLine, Len(strLine) - 1)
        a.writeline (strLine)
        strLine = ""

    Next i

    MsgBox "Your file is exported"
    
End Sub

My question is: is it possible to export a like for like table, ie. having the same number of columns and having them populated with right values?

The change has to be made here (see below), right?

strLine = strLine & """" & Me.List_AM_AT.Column(n, i) & ""","
a.writeline (strLine)

I've tried without luck the following:

strLine = Me.List_AM_AT.Column(n, i)    
a.cells(i,n).writeline (strLine)

Does anyone have an idea of what to do?

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

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

发布评论

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

评论(1

執念 2025-01-30 09:27:39

如我在我的评论中所述,您可以在代码中创建一个Excel文件并将值写入该文件。现在,您创建一个带有代码的文本文件,该文件导致您在帖子(文本助手等)中描述的问题

Private Sub button_Export_AMAT_Click()

    Dim i As Integer
    Dim n As Integer
    
    ' You might need to add a reference to Excel if your host application is Access
    ' Extra/Reference and select Microsoft Excel Object Library
    Dim xl As Excel.Application
    Set xl = New Excel.Application

    Dim wkb As Workbook
    Set wkb = xl.Workbooks.Add
    
    Dim wks As Worksheet
    Set wks = wkb.Sheets(1)

    'Dim strLine As String
    'Set fs = CreateObject("Scripting.FileSystemObject")
    'Set a = fs.CreateTextFile("\\yvavm301\Commun\CF_GDC\base de dados a trabalhar\AM_AT.csv", True)

    For i = 0 To Me.List_AM_AT.ListCount - 1

        For n = 0 To Me.List_AM_AT.ColumnCount - 1
            
            wks.Cells(i + 1, n + 1).Value = Me.List_AM_AT.Column(n, i)
            'strLine = strLine & """" & Me.List_AM_AT.Column(n, i) & ""","
        Next n
'
'        strLine = Left(strLine, Len(strLine) - 1)
'        a.writeline (strLine)
'        strLine = ""

    Next i
    
    wkb.SaveAs "D:\TMP\EXPORT.XLSX"  ' Adjust accordingly
    wkb.Close False
    xl.Quit
    MsgBox "Your file is exported"
    
End Sub

As said in my comment you could create an Excel file in your code and write the values to that file. Right now you create a text file with your code which leads to the issues you describe in your post (text assistant etc.)

Private Sub button_Export_AMAT_Click()

    Dim i As Integer
    Dim n As Integer
    
    ' You might need to add a reference to Excel if your host application is Access
    ' Extra/Reference and select Microsoft Excel Object Library
    Dim xl As Excel.Application
    Set xl = New Excel.Application

    Dim wkb As Workbook
    Set wkb = xl.Workbooks.Add
    
    Dim wks As Worksheet
    Set wks = wkb.Sheets(1)

    'Dim strLine As String
    'Set fs = CreateObject("Scripting.FileSystemObject")
    'Set a = fs.CreateTextFile("\\yvavm301\Commun\CF_GDC\base de dados a trabalhar\AM_AT.csv", True)

    For i = 0 To Me.List_AM_AT.ListCount - 1

        For n = 0 To Me.List_AM_AT.ColumnCount - 1
            
            wks.Cells(i + 1, n + 1).Value = Me.List_AM_AT.Column(n, i)
            'strLine = strLine & """" & Me.List_AM_AT.Column(n, i) & ""","
        Next n
'
'        strLine = Left(strLine, Len(strLine) - 1)
'        a.writeline (strLine)
'        strLine = ""

    Next i
    
    wkb.SaveAs "D:\TMP\EXPORT.XLSX"  ' Adjust accordingly
    wkb.Close False
    xl.Quit
    MsgBox "Your file is exported"
    
End Sub
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文