|
|
【答案】应助回帖
数据表中的数据如下:
A B C D
姓名 年龄 性别 职称
AAA 1 男 初级
BBB 2 女 中级
CCC 3 男 高级
DDD 4 女 初级
EEE 5 男 中级
FFF 6 女 高级
GGG 7 男 初级
HHH 8 女 中级
运行函数 GetDataAutoInputContent(), 结果正常。没有出现问题。
注:前面的共用部分省略。
Public Sub GetDataAutoInputContext()
Dim i As Integer
Dim r As Range
Dim x1 As String, x2 As String
Dim x3 As String, x4 As String
Dim startcol As Integer, endcol As Integer
If Selection Is Nothing Then
MsgBox "请先选中处理行", vbOKOnly, "错误"
STOPRUN = 1
Exit Sub
End If
'i = Selection.Row
i = 2
If i < 2 Then
MsgBox "不可处理第一行", vbOKOnly, "错误"
STOPRUN = 1
Exit Sub
End If
For i = 2 To 8
startcol = 1
endcol = startcol + XMS - 1
If startcol \ 26 > 0 Then
x1 = Chr(Asc("A" + startcol \ 26 - 1)
Else
x1 = ""
End If
x2 = Chr(Asc("A" + startcol Mod 26 - 1)
If endcol \ 26 > 0 Then
x3 = Chr(Asc("A" + endcol \ 26 - 1)
Else
x3 = ""
End If
x4 = Chr(Asc("A" + endcol Mod 26 - 1)
Set r = Range(x1 & x2 & i & ":" & x3 & x4 & i)
'循环语句Set r = Range(x1 & x2 & i & ":" & x3 & x4 & i)总是无法执行,提示“作用于对象Globle时失败”,而语句'Set r = Range(i & ":" & i)仅能执行当前行
For j = 1 To XMS Step 1
Select Case j
Case 1
xm = Trim(r.Cells(1, j).Value) '姓名
Case 2
nl = r.Cells(1, j).Value '年龄
Case 3
If Trim(r.Cells(1, j).Value) = "男" Then
xb = 1
ElseIf Trim(r.Cells(1, j).Value) = "女" Then
xb = 2
Else
MsgBox "性别字段错误", vbOKOnly, "错误"
STOPRUN = 1
Exit Sub
End If
Case 4
Select Case Trim(r.Cells(1, j).Value)
Case "初级"
zc = 1
Case "中级"
zc = 2
Case "高级"
zc = 3
Case Else
MsgBox "职称字段错误", vbOKOnly, "错误"
STOPRUN = 1
Exit Sub
End Select
End Select
Next j
MsgBox CStr(i - 1) & ":" & xm & " " & CStr(nl) & " " & IIf(xb = 1, "男", "女" & " " & IIf(zc = 1, "初级", IIf(zc = 2, "中级", "高级" )
Next i
End Sub
上面的例子中,从第2行循环到第8行,全部正常。 |
|