如何获得多个值(如何获得第二个匹配数据)

时间:2015-10-08 20:15:48

标签: vba

我正在处理以下代码

我尝试从另一个工作簿sheet1获取数据 我的问题是工作簿sheet1中有两个具有不同值的“财务深度” 我怎么能得到他们两个不同的细胞

例如

财务深度= 10.000

财务深度= 24.000

此代码获得第一个值。(财务深度= 10.000)......

Sub Balance()
Dim I As Integer
Dim myfile As String

Pathname = “E:\test\”
I = 1
myfile = Dir(Pathname & “*.xls”)
Cells(1, I) = myfile
StartingPoint:
I = I + 1
myfile = Dir
Cells(I, 1) = myfile
If myfile “” Then GoTo StartingPoint
I = I – 1

For K = 1 To I
‘On Error Resume Next
‘ *** workbook name***
Filename = Cells(K, 1)
Workbooks.Open (Pathname & Filename)
‘ *** company name ***
Cells(K, 2) = Workbooks(Filename).Worksheets(“Sheet1″).Cells(1, 1)
‘ *** Financial Depth***
Cells(K, 3) = ” Financial Depth”
For g = 1 To 220
If Workbooks(Filename).Worksheets(“Sheet1″).Cells(g, 1) = ” Financial Depth” Then
Cells(K, 4) = Workbooks(Filename).Worksheets(“Sheet1″).Cells(g, 3)
Exit For
End If
Next g




Workbooks(Filename).Saved = True
Workbooks(Filename).Close




Next k
End Sub

1 个答案:

答案 0 :(得分:0)

这应该对你有用

Sub Balance()
Dim I As Integer
Dim myfile As String
Dim m as Integer
m = 4
Pathname = “E:\test\”
I = 1
myfile = Dir(Pathname & “*.xls”)
Cells(1, I) = myfile
StartingPoint:
I = I + 1
myfile = Dir
Cells(I, 1) = myfile
If myfile “” Then GoTo StartingPoint
I = I – 1

For K = 1 To I
‘On Error Resume Next
‘ *** workbook name***
Filename = Cells(K, 1)
Workbooks.Open (Pathname & Filename) 
‘ *** company name ***
Cells(K, 2) = Workbooks(Filename).Worksheets(“Sheet1″).Cells(1, 1)
‘ *** Financial Depth***
Cells(K, 3) = ” Financial Depth”
For g = 1 To 220
If Workbooks(Filename).Worksheets(“Sheet1″).Cells(g, 1) = ” Financial Depth”         Then
Cells(K, m) = Workbooks(Filename).Worksheets(“Sheet1″).Cells(g, 3)
m = m +1
End If
Next g