update,
This commit is contained in:
79
jimmycheung93/task4/1/ReadAgentSalesWorkbook.bas
Normal file
79
jimmycheung93/task4/1/ReadAgentSalesWorkbook.bas
Normal file
@@ -0,0 +1,79 @@
|
||||
Attribute VB_Name = "ReadAgentSalesWorkbook"
|
||||
|
||||
Function Run(ByRef sPath As String) As Variant
|
||||
Dim wb As Workbook
|
||||
Dim arrResults(999, 20) As Variant
|
||||
Dim row() As Variant
|
||||
Dim i As Integer
|
||||
Dim row_count As Integer
|
||||
Dim firstRow As Boolean
|
||||
Dim rw As Range
|
||||
|
||||
' Try to open the workbook
|
||||
On Error Resume Next
|
||||
Set wb = Workbooks.Open(sPath)
|
||||
On Error GoTo 0
|
||||
|
||||
' Check if the opening was successful
|
||||
If wb Is Nothing Then
|
||||
MsgBox "Could not open workbook at " & sPath, vbCritical, "Error"
|
||||
Exit Function
|
||||
End If
|
||||
|
||||
row_count = 0
|
||||
If Not wb.Sheets("Sheet1") Is Nothing Then
|
||||
' Read data from Sheet1
|
||||
For Each rw In wb.Sheets("Sheet1").UsedRange.Rows
|
||||
' Check if first cell is empty
|
||||
If Trim(rw.Cells(1)) <> "" Then
|
||||
' Print value of first column of current row to immediate window
|
||||
row_count = row_count + 1
|
||||
Else
|
||||
' First cell in current row is empty, so break the loop
|
||||
Exit For
|
||||
End If
|
||||
|
||||
Next rw
|
||||
End If
|
||||
|
||||
' read table content
|
||||
i = 1
|
||||
firstRow = True
|
||||
|
||||
' Look for Sheet1 and stop execution if it doesn't exist
|
||||
If Not wb.Sheets("Sheet1") Is Nothing Then
|
||||
' Read data from Sheet1
|
||||
For Each rw In wb.Sheets("Sheet1").UsedRange.Rows
|
||||
' Check if first cell is empty
|
||||
If (firstRow = False) Then
|
||||
If (Trim(rw.Cells(1)) <> "") Then
|
||||
' Print value of first column of current row to immediate window
|
||||
arrResults(i, 1) = rw.Cells(1).Value
|
||||
arrResults(i, 2) = rw.Cells(2).Value
|
||||
arrResults(i, 3) = rw.Cells(3).Value
|
||||
arrResults(i, 4) = rw.Cells(4).Value
|
||||
arrResults(i, 5) = rw.Cells(5).Value
|
||||
arrResults(i, 6) = rw.Cells(6).Value
|
||||
|
||||
i = i + 1
|
||||
Else
|
||||
' First cell in current row is empty, so break the loop
|
||||
Exit For
|
||||
End If
|
||||
End If
|
||||
firstRow = False
|
||||
|
||||
Next rw
|
||||
|
||||
Else
|
||||
MsgBox """Sheet1"" does not exist in workbook at " & sPath, vbExclamation, "Warning"
|
||||
Exit Function
|
||||
End If
|
||||
|
||||
wb.Close savechanges:=False
|
||||
|
||||
Run = Array(arrResults, row_count - 1)
|
||||
End Function
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user