如果数据以特定数字开头,则传输数据

时间:2018-05-18 15:25:17

标签: vba excel-vba excel

我有这个代码,如果匹配条件,则将数据传输到另一个工作表。我的问题是代码的这一部分:

If (wsIn.Cells(lCurrentInputRow, 12) = "262015" Then

我想传输数据,但只有当它以262015开头时,我知道你可以使用" left",但是当我尝试在我的代码中实现它时,我不断收到错误。我希望有人可以帮助我。

Sub transferstudent()
Dim wsIn As Worksheet
Dim wsOut As Worksheet


Set wsIn = ThisWorkbook.Worksheets("Base")
Set wsOut = ThisWorkbook.Worksheets("Students")


wsOut.Cells(1, 1) = wsIn.Cells(1, 1) 
wsOut.Cells(1, 2) = wsIn.Cells(1, 8) 
wsOut.Cells(1, 3) = wsIn.Cells(1, 9) 

Dim lLastInputRow As Long
Dim lCurrentInputRow As Long
Dim lCurrentOutputRow As Long

lLastInputRow = wsIn.Cells(wsIn.Rows.Count, 1).End(xlUp).Row
lCurrentOutputRow = 2

For lCurrentInputRow = lLastInputRow To 2 Step -1

If (wsIn.Cells(lCurrentInputRow, 12) = "262015" Then

wsOut.Cells(lCurrentOutputRow, 1) = wsIn.Cells(lCurrentInputRow, 1) 
wsOut.Cells(lCurrentOutputRow, 2) = wsIn.Cells(lCurrentInputRow, 8) 
wsOut.Cells(lCurrentOutputRow, 3) = wsIn.Cells(lCurrentInputRow, 9) 
lCurrentOutputRow = lCurrentOutputRow + 1


End If
Next lCurrentInputRow


wsIn.Select
Set wsIn = Nothing
Set wsOut = Nothing

End Sub

1 个答案:

答案 0 :(得分:1)

你的代码丢失了“)”这里是:

If (Left(wsIn.Cells(lCurrentInputRow, 12), 6)) = "262015" Then
    MsgBox "hi"
End If