【问题标题】:VBA reading two columns inside Excel through loopVBA通过循环读取Excel中的两列
【发布时间】:2016-05-16 13:38:19
【问题描述】:

我有这个代码:

Dim cityList() 

Set objExcel = CreateObject("Excel.Application") 

Set objWorkbook = objExcel.Workbooks.Open("C:\Users\xyz\Desktop\Test\Cities.xlsx") 
objExcel.Visible = True 

i = 1 
x = 0 

Do Until objExcel.Cells(i, 1).Value = "" 
ReDim Preserve cityList(x) 
cityList(x) = objExcel.Cells(i, 1).Value 
i = i + 1 
x = x + 1 

Loop 

objExcel.Quit

我在用两个条件制作这个类似数组时遇到问题。我的excel包含两列: 城市日期

Mumbai  22.04.2016
Delhi   23.05.2016
Goa     24.06.2016

我已经设法阅读了城市栏并一一阅读,但我还需要阅读日期和条件如下:

对于 City = "Mumbai" 和 Date = "22.04.2016" 做一些事情......

我对VBA不是很熟悉,但是必须写脚本。

有人可以帮我在里面添加日期,以便他可以阅读这两列吗?

先谢谢

【问题讨论】:

  • 如果您只有一个城市的一个实例,您可能会更好地使用Dictionary 对象而不是数组。如果您可以有多个版本,您可能需要考虑使用二维数组?此外,这是 vbscript 而不是 VBA(显示您必须创建 excel 应用程序对象的位置)
  • 问题,我通过 Excel 使用它,因为我不知道我的数组会有多少成员。我想要一个有两个成员的空数组,因为我的城市和日期列表每周都会改变你能给我一些有用的例子吗?我已经阅读了文档,但不是很清楚,它们都具有预定义的大小。
  • 如果您认为需要进一步更新,请查看我发布的答案并发表评论?

标签: vba excel vbscript


【解决方案1】:

以下是对现有代码的快速重写,以实现我认为您所追求的目标:

Dim cityList(1,0)
Set objExcel = CreateObject("Excel.Application") 
Set objWorkbook = objExcel.Workbooks.Open("C:\Users\xyz\Desktop\Test\Cities.xlsx") 
objExcel.Visible = True 
Set oSheet = objWorkbook.Worksheets("Sheet1")   ' set the correct sheet name here
iLastRow = oSheet.Cells(oSheet.Rows.Count, 1).End(xlUp).Row ' determine the last row to look at

For iRow = 1 To iLastRow
    ReDim Preserve cityList(1,iRow-1)                       ' within the loop, extend the array by 1
    cityList(0,UBound(cityList)) = oSheet.Cells(iRow,1)     ' Set the city value
    cityList(1,UBound(cityList)) = oSheet.Cells(iRow,2)     ' Set the date value
Next

' Now you have all the data you can iterate over it like so:
For iLoop = 0 To UBound(cityList,2)
    If cityList(0, iLoop) = "Mumbai" And cityList(1, iLoop) = "22.04.2016" Then
        ' Do whatever you needed to do
    End If
Next

objExcel.Quit

【讨论】:

  • 我不应该输入任何实际值,因为我永远不会知道我的 Excel 将包含哪个城市和日期:(
  • 我根据您的 OP 添加了这些作为示例。大概在您运行脚本时,您确实知道您所追求的城市/日期组合,因此您需要在脚本中包含该信息并将“孟买”替换为 cityName 和“22.04.2016”替换为 reqDate 或类似易于理解的定义变量...
【解决方案2】:

为什么所有这些循环和redim

Dim cityList as variant, lastRow as Long
lastRow = range("A" & rows.count).End(xlUp).Row
cityList = range("A1:B" & lastrow)

编写和执行速度更快

【讨论】:

  • 我已经尝试过了,但它显示了我的错误,比如语句结束...... :( 也许我做得不好
【解决方案3】:
' in your code you can capture date values using offset property

ReDim Preserve cityList(x) 
cityList(x) = objExcel.Cells(i, 1).Value 
' = objExcel.Cells(i, 1).Offset(0, 1).Value  ' offset property to capture date 
' As Dave said either you can use Dictionary or 2D arrays to store date. . .
i = i + 1

【讨论】:

  • 嗨,我试过了,但是在我只是赶上日期,城市被跳过了?
【解决方案4】:

我找到了解决办法:

Dim cityList() 

Set objExcel = CreateObject("Excel.Application") 
Set objWorkbook = objExcel.Workbooks.Open("C:\Users\xyz\Desktop\Test\Cities.xlsx") 
objExcel.Visible = True 
i = 1
x = 0 
y = 0

Do Until objExcel.Cells(i, 1).Value = "" 
ReDim Preserve cityList(x)
ReDim Preserve cityDate(y)
cityList(x) = objExcel.Cells(i, 1).Value
cityDate(y) = objExcel.Cells(i, 2).Value

i = i + 1 

x = x + 1 
y = y + 1

Loop 

objExcel.Quit

j = 0

For Each city in cityList

tab = "City"
        datetime = cityDate(j)
        j = j + 1
        count = count + 1
        .....
Next

也许不是最好的解决方案,但正在工作!谢谢大家的建议和帮助!

【讨论】:

    猜你喜欢
    • 2018-04-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多