运行循环时,无论有多少行,最后一个值总是被传输两次。
我尝试过.offset
和+/-来排行。
Public MySession As Reflection.Session
Option Explicit
Sub getData()
Dim ws As Worksheet
Dim lastrow As Long
Dim i As Long
Dim question As String
Dim answer As String
Set ws = ThisWorkbook.Sheets("Sheet1")
Set MySession = New Reflection.Session
With ws
lastrow = getlastrow
With MySession
On Error GoTo qHandle
For i = 2 To lastrow
question = ws.Cells(i, 1).Value
On Error GoTo aHandle
If question = "" Then
'do nothing
Else
.TransmitANSI question
.TransmitTerminalKey rcIBMEnterKey
ws.Cells(i, 2).Value = MySession.GetDisplayText(1, 2, 3)
End If
On Error GoTo 0
Next i
On Error GoTo 0
End With
End With
Exit Sub
qHandle:
MsgBox "There was a problem with the question, " & Err.Description
Exit Sub
aHandle:
MsgBox "There was a problem with the answer, " & Err.Description
Exit Sub
End Sub
最终结果应该是将值打印到与输入行相对应的图纸上。一切正常,没有错误,但是最后一个值被迭代了两次,从而在第2列而不是第1列中增加了一行。
这是我获取lastrow的功能。
'Return Value of Last Row
Public Function get_LastRow() As Long
Dim LastRow As Long
With ActiveSheet
If Application.WorksheetFunction.CountA(.Cells) <> 0 Then
LastRow = .Cells.Find(What:="*", _
After:=.Range("A1"), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).row
Else
LastRow = 1
End If
get_LastRow = LastRow
End With
End Function
答案 0 :(得分:1)
也许无法解决您的问题,但是在这里:
Set ws = ThisWorkbook.Sheets("Sheet1")
Set MySession = New Reflection.Session
With ws
lastrow = getlastrow
... ws
和lastrow
之间没有任何联系,除非ws
恰好是活动表(并依靠它来使您的代码容易出现问题)
这样会更好:
lastrow = getlastrow(ws)
和
Public Function get_LastRow(ws As Worksheet) As Long
Dim LastRow As Long
With ws
If Application.WorksheetFunction.CountA(.Cells) <> 0 Then
LastRow = .Cells.Find(What:="*", _