使用 split VBA 类型不匹配
Type mismatch using split VBA
我有以下代码,它给我代码行 «Split_dt_2 = Split(Split_dt_1, ",")» 的错误类型不匹配。我无法使用 F8 运行 通过代码,因为它会立即给我错误,所以我无法给出 «Split_dt_1» 的确切值,但它始终是具有该形式的日期: [11/1/2019,12/1/2019].
我的目标是获得:
y_Dest = 2019 和 m_Des = 11
Sub import_Redeem_Spread()
Workbooks.Open "C:\Users6400\OneDrive\Documents\FTT\CDOPT_AB.xlsm"
Dim wksSource As Worksheet, wksDest As Worksheet
Set wksSource = Workbooks("CDOPT_AB.xlsm").Sheets(2)
Set wksDest = ThisWorkbook.Sheets(2)
Dim Split_dt_1() As String
Dim Split_dt_2() As String
Dim Split_dt_3() As String
Dim Split_dt_4() As String
nbRows = wksSource.Cells(Rows.Count, 1).End(xlUp).Row
nbDates = wksDest.Cells(Rows.Count, 1).End(xlUp).Row
For i = 2 To nbRows
If wksSource.Cells(i, 16) = "CPG Taux Fixe" Then
For m = 7 To nbDates
Split_dt_1 = wksDest.Cells(m, 2)
Split_dt_2 = Split(Split_dt_1, ",")
Split_dt_3 = Split_dt_2(0)
Split_dt_4 = Split(Split_dt_3, "[")
y_Dest = Right(Split_dt_4(1), 4)
m_Dest = Left(Split_dt_4(1), 2)
y_source = Left(Cells(I, 3), 4)
m_Source = Right(Cells(I, 3), 2)
If y_Dest = m_Dest & y_Source = m_Source Then
For n = 4 To 15
wksDest.Cells(m, n) = wksSource.Cells(i, n)
Next n
End If
Next m
End If
Next i
End Sub
我试过 «Dim Split_dt_2() As Variant» 但它没有解决问题
我试过了
Split_dt_1 = wksDest.Cells(m, 2).value
Split_dt_2 = Split(Split_dt_1, ",")
还是不行
提前致谢!
在使用 Split
创建数组时使用 Variant
而不是 Dim
将其作为 String
数组。
当您使用 returns 和 Array
. 函数时,A Variant
将具有 Array
的属性
Dim Split_dt_1 As Variant
Split_dt_1 = Split(wksDest.Cells(m, 2), ",")
我会完全放弃分配数组和所有中间步骤:
y_Dest = Year(Split(Split(wksDest.Cells(m, 2), ",")(0), "[")(0))
有时这些中间步骤会有所帮助,但 IMO,这不是其中之一。
我有以下代码,它给我代码行 «Split_dt_2 = Split(Split_dt_1, ",")» 的错误类型不匹配。我无法使用 F8 运行 通过代码,因为它会立即给我错误,所以我无法给出 «Split_dt_1» 的确切值,但它始终是具有该形式的日期: [11/1/2019,12/1/2019].
我的目标是获得: y_Dest = 2019 和 m_Des = 11
Sub import_Redeem_Spread()
Workbooks.Open "C:\Users6400\OneDrive\Documents\FTT\CDOPT_AB.xlsm"
Dim wksSource As Worksheet, wksDest As Worksheet
Set wksSource = Workbooks("CDOPT_AB.xlsm").Sheets(2)
Set wksDest = ThisWorkbook.Sheets(2)
Dim Split_dt_1() As String
Dim Split_dt_2() As String
Dim Split_dt_3() As String
Dim Split_dt_4() As String
nbRows = wksSource.Cells(Rows.Count, 1).End(xlUp).Row
nbDates = wksDest.Cells(Rows.Count, 1).End(xlUp).Row
For i = 2 To nbRows
If wksSource.Cells(i, 16) = "CPG Taux Fixe" Then
For m = 7 To nbDates
Split_dt_1 = wksDest.Cells(m, 2)
Split_dt_2 = Split(Split_dt_1, ",")
Split_dt_3 = Split_dt_2(0)
Split_dt_4 = Split(Split_dt_3, "[")
y_Dest = Right(Split_dt_4(1), 4)
m_Dest = Left(Split_dt_4(1), 2)
y_source = Left(Cells(I, 3), 4)
m_Source = Right(Cells(I, 3), 2)
If y_Dest = m_Dest & y_Source = m_Source Then
For n = 4 To 15
wksDest.Cells(m, n) = wksSource.Cells(i, n)
Next n
End If
Next m
End If
Next i
End Sub
我试过 «Dim Split_dt_2() As Variant» 但它没有解决问题
我试过了
Split_dt_1 = wksDest.Cells(m, 2).value
Split_dt_2 = Split(Split_dt_1, ",")
还是不行
提前致谢!
在使用 Split
创建数组时使用 Variant
而不是 Dim
将其作为 String
数组。
Array
. 函数时,A Variant
将具有 Array
的属性
Dim Split_dt_1 As Variant
Split_dt_1 = Split(wksDest.Cells(m, 2), ",")
我会完全放弃分配数组和所有中间步骤:
y_Dest = Year(Split(Split(wksDest.Cells(m, 2), ",")(0), "[")(0))
有时这些中间步骤会有所帮助,但 IMO,这不是其中之一。