【问题标题】:vba + write values to dynamic one-dimensional array and write to csv filevba + 将值写入动态一维数组并写入 csv 文件
【发布时间】:2015-02-01 20:55:07
【问题描述】:

我在将数组写入 .csv 文件时遇到问题。我找到了一种创建数组并将其写入 .csv 文件的方法,但它给出了错误 @(arrayNumber(i) = mainWorkBook.Sheets("Sheet1").Range("A" & i).Value) .我找不到为什么我不能将这些值写入数组。由于这个问题,我也无法测试写入 .Csv 文件是否是正确的方法。我希望有人能指出我正确的方向。提前致谢。

Sub Numbering()

Dim arrayNumber()
Dim arrayName()

Dim pathName As String

If ActiveSheet.Name = "OVERVIEW" Or ActiveSheet.Name = "Template" Or ActiveSheet.Name = "Develop" Or ActiveSheet.Name = "Schedule" Or ActiveSheet.Name = "Information" Or ActiveSheet.Name = "Announcements" Or ActiveSheet.Name = "Database" Then
    MsgBox ("You can't extract the number and name to a csv file.")

Else

    pathName = "C:" & "\textfile.csv"
    Open pathName For Output As #1
    Print #1, "Number   Name"

    ActiveSheet.Select
    LastRow = ActiveSheet.Range("K1048555").End(xlUp).Row
    ReDim Preserve arrayNumber(1 To LastRow)
    ReDim Preserve arrayName(1 To LastRow)

    j = 1
    For i = 13 To LastRow

            If ActiveSheet.Range("K" & i).Value = "1" Then
                arrayNumber(i) = mainWorkBook.Sheets("Sheet1").Range("A" & i).Value
                arrayName(i) = ActiveSheet.Range("Q" & i).Value
                Print #1, " arrayNumber & "; " & arrayName"
                j = j + 1
            End If
    Next
    Close #1
    MsgBox ("Done")

End If

End Sub

【问题讨论】:

    标签: arrays vba excel csv


    【解决方案1】:

    经过一番尝试,我找到了解决方案(下面的代码)。

    Sub Numbering()
    
    Dim arrayNumber()
    Dim arrayName()
    
    Dim pathName As String
    
    If ActiveSheet.Name = "OVERVIEW" Or ActiveSheet.Name = "Template" Or ActiveSheet.Name = "Develop" Or ActiveSheet.Name = "Schedule" Or ActiveSheet.Name = "Information" Or ActiveSheet.Name = "Announcements" Or ActiveSheet.Name = "Database" Then
        MsgBox ("You can't extract the number and name to a csv file.")
    
    Else
    
        pathName = "C:" & "\textfile.csv"
        Open pathName For Output As #1
        Print #1, "Number   Name"
    
        ActiveSheet.Select
        LastRow = ActiveSheet.Range("K1048555").End(xlUp).Row
        ReDim Preserve arrayNumber(1 To LastRow)
        ReDim Preserve arrayName(1 To LastRow)
    
        j = 1
        For i = 13 To LastRow
    
                If ActiveSheet.Range("K" & i).Value = "1" Then
                   arrayNumber(j) = ActiveSheet.Range("A" & i).Value
                    arrayName(j) = ActiveSheet.Range("Q" & i).Value
    
                    MsgBox (arrayNumber(j))
                    MsgBox (arrayName(j))
                    Print #1, arrayNumber(j) & "; " & arrayName(j)
                    j = j + 1
                End If
        Next
        Close #1
        MsgBox ("Done")
    
    End If
    
    End Sub
    

    【讨论】:

      【解决方案2】:

      你可以考虑:

      Sub M_snb()
        createobject("scripting.filesystemobject").createtextfile("C:\textfile.csv").write join([transpose(if(K13:K2000="","",A13:A2000&";"&K13:K2000))],vbcrlf)
      end sub
      

      【讨论】:

        猜你喜欢
        • 2019-01-11
        • 2018-03-08
        • 2018-07-09
        • 2016-09-27
        • 2021-01-09
        • 2014-08-20
        • 2013-10-29
        • 2017-06-27
        • 1970-01-01
        相关资源
        最近更新 更多