使用数组值分配新的Excel文件

问题描述 投票:0回答:3

我的VBA脚本基本上是从第三方转换我们的公司格式。到目前为止我做了什么,我转换数据和存储字符串数组。现在我想打开新的excel文件并存储字符串数组。如何使用我的阵列分配新的excel文件?如果有人能帮助我,我真的很感激。

谢谢

    Sub Convert()

    Dim cell As Range

    Dim sCode As String      'Securtiy code
    Dim symbol As String     'Symbol
    Dim receiveDate As Date  'Date
    Dim bidNum As Double    'Bid
    Dim askNum As Double    'Ask
    Dim closeNum As Double  'Close
    Dim sDesc As String      'Security Desc
    Dim cusip As String      'Cusip
    Dim cur As String        'Currency
    Dim pFactor As String    'Principal Factor


    Dim result As String     'Final result

    delim = "++^||"

    NumRows = Range("A2", Range("A2").End(xlDown)).Rows.Count

    Dim arrResult() As Variant
    ReDim arrResult(Range("A2", Range("A2").End(xlDown)).Rows.Count)

    Range("A2").Select
    c = 2

    For x = 1 To NumRows

        '============ c is row ==========

        sCode = Cells(c, 1)
        symbol = Cells(c, 2)
        receiveDate = Cells(c, 3)
        bidNum = Cells(c, 4)
        askNum = Cells(c, 5)
        closeNum = Cells(c, 6)
        sDesc = Cells(c, 7)
        cusip = Cells(c, 8)
        cur = Cells(c, 9)
        pFactor = Cells(c, 10)

        c = c + 1

        result = storeFun(sCode, symbol, receiveDate, bidNum, askNum, closeNum, sDesc, cusip, cur, pFactor)
        arrResult(x - 1) = result

        ActiveCell.Offset(1, 0).Select

    Next

cell(12, 1) = arrResult(2)

End Sub

Function storeFun(sCode, symbol, receiveDate, bidNum, askNum, closeNum, sDesc, cusip, cur, pFactor) As String

Dim strDate As String
Dim strbidNum As String
Dim straskNum As String
Dim strcloseNum As String
Dim delim As String      '"++^||"

strDate = Format(receiveDate, "dd-mmm-yyyy")
strbidNum = Format(bidNum, "00.00000000")
straskNum = Format(askNum, "00.00000000")
strcloseNum = Format(closeNum, "00.00000000")

delim = "++^||"
storeFun = sCode + delim + symbol + delim + strDate + delim + strbidNum + delim + straskNum + _
            delim + strcloseNum + delim + sDesc + delim + cusip + delim + cur + delim + pFactor + delim

End Function
excel vba
3个回答
0
投票

这是我认为你可以做的,在你打开新工作簿的子程序中传递你的数组。

Public Sub Test()
    Dim a(10) As Variant
    For i = 0 To UBound(arry, 1)
        a(i) = i * 2
    Next

    ParseArray a
  End Sub


Public Sub ParseArray(arrValues() As Variant)

    Dim wb As Workbook
    Set wb = Workbooks.Add

    For i = 0 To UBound(arrValues, 1)
        ActiveSheet.Cells(i + 1, 1) = arrValues(i)
    Next
End Sub

0
投票

您可以将整个数组分配给一个范围(它们必须匹配大小),但是一维数组是一行高多列,其中2 + D数组是行相关的。这应该将转置的数组写为1列,数组中的行数

Dim RNG As Range
Set RNG = ActiveWorkSheet.Range("A1")
Set RNG = Destination.Resize(UBound(arrResult), 1)
RNG.Value = Application.Transpose(arrResult)

0
投票

下面是一些代码,演示如何创建数组,然后将其分配给新工作簿。您应该能够修改代码以使用它

请注意,创建的数组是2D

  • 第一个维度表示行
  • 第二个维度代表列

然后将此创建的数组传递给第二个Sub,它将添加一个新的工作簿(基于您的默认模板,尽管您可以更改它)

  • 请注意,目标范围的大小应与传递的数组相同
  • 请注意,该数组只需一步即可写入工作表。

Option Explicit
Sub CreateTestArray()
    Dim myArr() As Variant
    Dim I As Long, J As Long

ReDim myArr(1 To 10, 1 To 2)

For I = 1 To 10
    For J = 1 To 2
        myArr(I, J) = Int((999 - 100 + 1) * Rnd + 100)
    Next J
Next I

WriteToNewWB myArr    
End Sub

'-------------------------------------

Sub WriteToNewWB(arr)
    Dim WB As Workbook
    Dim R As Range

Set WB = Workbooks.Add

Set R = WB.Worksheets(1).Cells(1, 1)
Set R = R.Resize(rowsize:=UBound(arr, 1), columnsize:=UBound(arr, 2))

With R
    .Value = arr
    .EntireColumn.AutoFit
End With
End Sub
© www.soinside.com 2019 - 2024. All rights reserved.