2016-05-16 55 views
0

我有這樣的代碼:VBA讀取Excel內部兩列通過循環

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 

我已成功地讀取列城和閱讀它們一個接一個,但我也需要閱讀的日期和條件是這樣的:

城市=「孟買「和日期=」22.04.2016「做點什麼......

我對VBA不是很熟悉,但是必須寫腳本。

任何人都可以幫助我如何添加日期裏面,所以他可以閱讀兩列?

由於前期

+0

如果你只有一個城市的一個實例,你可能會更好地使用'Dictionary'對象而不是數組。如果你可以有多個版本,你可能想要考慮一個2維數組呢?此外,這是vbscript而不是VBA(顯示你必須創建excel應用程序對象的地方) – Dave

+0

這個問題,我通過Excel使用這個,導致我不知道我的數組有多少成員。我想要有一個空數組,它有兩個成員,導致我的城市和日期列表每週都會改變。你能給我一些有用的例子嗎?我已經閱讀過文檔,但不是很清楚,它們都是預定義的大小。 – marija

+0

如果您認爲需要更新,請查看我發佈的答案並對其發表評論。 – Dave

回答

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 
+0

我不應該輸入任何真正的值,因爲我永遠不會知道我的excel將包含哪個城市和日期:( – marija

+0

我將這些添加爲基於您的OP的示例。假設在您運行腳本時您知道什麼城市/因此您需要將該信息包含在腳本中,並用cityName和「22.04.2016」替換爲「reqDate」或類似定義的易於理解的變量......「 – Dave

0
' 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 
+0

嗨,我試過了這但在我只趕上日期,城市被跳過? – marija

1

爲什麼所有這些循環和redim

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

快得多寫和執行

+0

我已經嘗試過這一點,但它顯示我的錯誤像聲明結束...... :(也許我不是做不好的好 – marija

0

我已經找到解決方案:

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 

也許不是最好的解決辦法,但工作!謝謝大家的建議和幫助!