我有一张桌子
date number
01/14 1324e3
01/15 134d32
01/14 15233t
01/13 1s3634
01/14 133624
我需要得到一个与String
完全相同的'1324e3','15233t','133624'
,我。即对应于01/14
Public Function getString (myDate As Variant) As String
Dim ws2 As Worksheet
Set ws2 = This.Workbook.Worksheets("Sheet1")
getString = Application.Vlookup(myDate, ws2.Range("A:B"),2,false)
End Function
工作正常但只返回一个值我该如何纠正呢?
答案 0 :(得分:1)
您似乎错误地从SQL数据库中提取数据,因为日期和数字列的格式通常不是标准格式。对于数字列(我不确定应该是字符串还是数字),您可能会遇到像“1234e3”这样的数字问题,因为excel会将其识别为数字而不是字符串。除非您将日期列保留为字符串,否则日期列也将转换为Date:DD-MMM格式。忽略这些问题,下面的函数应该构建你的字符串。
此外,Vlookup只会返回一个值,所以因为你想要返回所有匹配,我使用了一个循环。
Public Function GetString(myDate As Variant) As String
Dim ws2 As Worksheet
Dim i As Long, Srow As Long, Erow As Long
Dim BuildString As String
Set ws2 = ThisWorkbook.Worksheets("Sheet1")
Srow = 2
Erow = ws2.Cells(2, 1).End(xlDown).Row
BuildString = ""
For i = Srow To Erow
If ws2.Cells(i, 1) = myDate Then
If BuildString = "" Then
BuildString = "'" & ws2.Cells(i, 2) & "'"
Else
BuildString = BuildString & ",'" & ws2.Cells(i, 2) & "'"
End If
End If
Next i
GetString = BuildString
End Function