【问题标题】:Easier way of Importing data into excel - collections?将数据导入 excel 的更简单方法 - 集合?
【发布时间】:2016-06-02 19:05:44
【问题描述】:

有没有更简单的方法可以将数据导入到 excel 数组或其他数据结构中?我尝试过研究收藏,但我发现文档难以理解。 http://www.functionx.com/vbaexcel/objects/Lesson6.htm

https://msdn.microsoft.com/en-us/library/f26wd2e5%28v=vs.100%29.aspx

我下面的代码打开一个选择文件并搜索列标题,然后根据标题和行变量循环存储数据的每一行,过去我已经为许多宏做过这种方法,但现在我处理许多列,我正在寻找更高级的方法?

Sub Import_NAVRec()

MyPath = Range("b2")                                'Defines cell that contains path to source file
Workbooks.Open (MyPath)                             'Opens file
Set tempbook = ActiveWorkbook                       'Names workbook
LR = Range("A65000").End(xlUp).Row                  'finds last row in sourcefile



ReDim aNavRec(1 To LR, 1 To 4)                      'Defines NAV Rec array
nRow = 0


 cName = "Accounting Basis"
 CA = Cells.Find(What:=UCase(cName), After:=ActiveCell, LookIn:=xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext).Column
 cName = "Accounting Date"
 cB = Cells.Find(What:=UCase(cName), After:=ActiveCell, LookIn:=xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext).Column
 cName = "Asset Currency"
 cC = Cells.Find(What:=UCase(cName), After:=ActiveCell, LookIn:=xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext).Column

     For r = 2 To LR
        'If Cells(r, cB) = "Trading Gain Loss" Then
         nRow = nRow + 1
         aNavRec(nRow, 1) = Cells(r, CA) 'Fund Number
         aNavRec(nRow, 2) = Cells(r, cB) 'Ledger
         aNavRec(nRow, 3) = Cells(r, cC) 'Balance change
        'End If

     Next r


tempbook.Close
End Sub

Sub Print_output()

Sheets("Output").Select
Set Destination = Range("a2")
Destination.Resize(UBound(aNavRec, 1) + 1, UBound(aNavRec, 2)).Value = aNavRec


End Sub

【问题讨论】:

  • 看看使用 ADO 连接 Excel 和使用 SQL,所以不管列在哪里,只要它在那里,你就可以使用类似 "select column_name from [Sheet1$]" 的东西使用记录集,以及相关的属性和方法来探索
  • 一旦你建立了你想导入的范围,你可以在一个步骤中做一个Variant变量。例如:V = Range(Cells(1,1), Cells(LastRow,LastCol) V 将是一个基于 1 的二维数组,其中第一个维度表示行,第二个维度表示列。
  • 如果您的工作代码只需要改进,那么您可能在这篇文章的错误位置。 Code Review 是他们处理现有/工作代码的地方,并在速度、安全性、可持续性和寿命方面尽最大努力改进它。试试看。他们很好!与此同时,您可能要考虑使用这个:Dim aNavRec as Variant 然后aNavRec = Range("A2:D" & LR )

标签: arrays vba excel data-structures


【解决方案1】:

我们唯一可以帮助您消除代码中间的for 循环。其余的似乎是必要的。

Option Explicit

Sub Import_NAVRec()

Dim LR As Long
Dim MyPath As String
Dim aNavRec As Variant                              'Defines NAV Rec array
Dim tempbook As Workbook
Dim CA As Long, cB As Long, cC As Long

MyPath = Range("B2")                                'Defines cell that contains path to source file
Workbooks.Open (MyPath)                             'Opens file
Set tempbook = ActiveWorkbook                       'Names workbook
LR = Range("A65000").End(xlUp).Row                  'finds last row in sourcefile

cName = "Accounting Basis"
CA = Cells.Find(What:=UCase(cName), After:=ActiveCell, LookIn:=xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext).Column
cName = "Accounting Date"
cB = Cells.Find(What:=UCase(cName), After:=ActiveCell, LookIn:=xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext).Column
cName = "Asset Currency"
cC = Cells.Find(What:=UCase(cName), After:=ActiveCell, LookIn:=xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext).Column

aNavRec = Application.Index(Range("A:AZ"), Application.Evaluate("Row(1:" & LR & ")"), Array(CA, cB, cC))

tempbook.Close
End Sub

对于Option Explicit,需要更多Dim(我在上述解决方案中包含了这些)。

注意:刚刚在这里找到了这个解决方案:use variable for row_num application.index

【讨论】:

    猜你喜欢
    • 2020-11-17
    • 2010-10-06
    • 1970-01-01
    • 1970-01-01
    • 2010-09-23
    • 1970-01-01
    • 2012-11-23
    • 1970-01-01
    • 2011-09-02
    相关资源
    最近更新 更多