24小时热门版块排行榜    

查看: 2515  |  回复: 12

bongb

银虫 (小有名气)

[求助] 求急救:VBA中range对单元格不能依次读取数据,提示:“作用于对象Globle时失败”

Excel中用VBA写了一段,
程序代码如下(省略了一部分)
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" + startcolMod26 - 1)
If endcol \ 26 > 0 Then
x3 = Chr(Asc("A" + endcol \ 26 - 1)
Else: x3 = ""
End If
x4  = Chr(Asc("A" + endcolMod26 - 1)
Set r = Range(x1 & x2 & i & ":" & x3 & x4 & i)

在执行时出现提示:“作用于对象Globle时失败”

在网上搜索,有个类似的,但也没能解决。
请教各位达人!
救急!谢谢!
回复此楼

» 本帖已获得的红花(最新10朵)

» 猜你喜欢

已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖
回帖支持 ( 显示支持度最高的前 50 名 )

michaelwlin

木虫 (正式写手)

【答案】应助回帖

引用回帖:
5楼: Originally posted by bongb at 2013-06-20 22:28:22
感谢michaelwlin的解答,
之前有申明的,
public i as integer
public const XMS as integer = 4

具体的明天贴出来,代码的目的是依次读取数据单元格区域的数据。...

申明 是 申明,赋值 是 赋值,

如果 未赋值,那么 x1 & x2 & i & ":" & x3 & x4 & i 不能表达一个 合法 的Range 参数,导致 错误。
7楼2013-06-21 08:44:41
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

michaelwlin

木虫 (正式写手)

【答案】应助回帖

MOD运算注意加空格

endcolMod26 应为 endcol Mod 26

这样 到 range() 函数 那里,应该没有错误

» 本帖已获得的红花(最新10朵)

10楼2013-06-21 11:14:40
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖
普通回帖

bongb

银虫 (小有名气)

各位达人注意: 代码中(Asc("A" 的字符A后面的符号 " 和 ) 被自动替换成表情符号了
2楼2013-06-19 18:34:04
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

bongb

银虫 (小有名气)

怎么没人回复呢?
3楼2013-06-20 11:16:10
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

michaelwlin

木虫 (正式写手)

【答案】应助回帖


感谢参与,应助指数 +1
ben_ladeng: 金币+1, 谢谢指教 2013-06-20 21:49:59
大概知道 楼主的代码 想 实现 什么 功能

就 这段 代码 来看,命令Set r = Range(x1 & x2 & i & ":" & x3 & x4 & i)在执行前,未对变量 i 赋值~~~~

你贴的代码 不完全,目前 只能 帮到 这里了。

» 本帖已获得的红花(最新10朵)

4楼2013-06-20 11:21:54
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

bongb

银虫 (小有名气)

送红花一朵
引用回帖:
4楼: Originally posted by michaelwlin at 2013-06-20 11:21:54
大概知道 楼主的代码 想 实现 什么 功能

就 这段 代码 来看,命令Set r = Range(x1 & x2 & i & ":" & x3 & x4 & i)在执行前,未对变量 i 赋值~~~~

你贴的代码 不完全,目前 ...

感谢michaelwlin的解答,
之前有申明的,
public i as integer
public const XMS as integer = 4

具体的明天贴出来,代码的目的是依次读取数据单元格区域的数据。
5楼2013-06-20 22:28:22
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

ausir

木虫 (正式写手)

【答案】应助回帖

感谢参与,应助指数 +1
Public i As Integer
Public Const XMS As Integer = 4

Sub test()
Dim r As Range
Dim startcol As Integer, endcol As Integer
Dim x1, x2, x3, x4

Dim s As String

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)
'Debug.Print Asc(x1)
Debug.Print Asc(x2)

If endcol \ 26 > 0 Then
    x3 = Chr(Asc("A" + endcol \ 26 - 1)
Else
    x3 = ""
End If
x4 = Chr(Asc("A" + endcol Mod 26 - 1)
'Debug.Print Asc(x3)
Debug.Print Asc(x4)

Debug.Print i

If i = 0 Then i = 1
s = x1 & x2 & i & ":" & x3 & x4 & i
Debug.Print s

Set r = Sheets(1).Range(s)

r.Value = 5

End Sub

注意:i 不能等于 0, 否则 A00 是没有意义的。

» 本帖已获得的红花(最新10朵)

6楼2013-06-21 07:14:09
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

bongb

银虫 (小有名气)

送红花一朵
感谢各位达人回答,
现在遇到的问题有两个:
1.'循环语句Set r = Range(x1 & x2 & i & ":" & x3 & x4 & i)总是无法执行,提示“作用于对象Globle时失败”,而语句'Set r = Range(i & ":" & i)仅能执行当前行

2.怎样让程序自动点击“确定保存”,就是能自动上传并保存全部数据到网页?



我这边的完整代码如下:【参考一篇文献来的,但验证时发现文献中有错误,修改了下;百度了下,网上有类似的疑问,但给出的解答不能解决问题】

Public mWindow As Object
Public mDocument As Object
Public STOPRUN As Integer
Public Const XMS As Integer = 4

Public xm As String '姓名
Public xb As Integer '性别
Public nl As Integer '年龄
Public zc As Integer '职称类别

Public Sub mComGetIEWindows(ByVal IETitle As String, Optional ByVal WaitTime As Integer = 0)
Dim mshellWindow As New SHDocVw.ShellWindows
Dim mIndex As Long
For mIndex = 0 To mshellWindow.Count - 1
If VBA.TypeName(mshellWindow.Item(mIndex).document) = "HTMLDocument" Then

If mshellWindow.Item(mIndex).document.Title = IETitle Then
If WaitTime = 1 Then
Do While mshellWindow.Item(mIndex).Busy
Application.Wait (Now + TimeValue("0:00:01")
DoEvents
Loop
End If

Set mDocument = mshellWindow.Item(mIndex).document
Set mWindow = mshellWindow.Item(mIndex)

mshellWindow.Item(mIndex).Visible = True
Exit Sub
End If
End If
Next mIndex

End Sub

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
If i < 2 Then
MsgBox "不可处理第一行", vbOKOnly, "错误"
STOPRUN = 1
Exit Sub
End If
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" + startcolMod26 - 1)
If endcol \ 26 > 0 Then
x3 = Chr(Asc("A" + endcol \ 26 - 1)
Else: x3 = ""
End If
x4 = Chr(Asc("A" + endcolMod26 - 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 i = 1 To XMS Step 1
Select Case i
Case 1
xm = Trim(r.Cells(1, i).Value) '姓名
Case 2
nl = r.Cells(1, i).Value '年龄
Case 3
If Trim(r.Cells(1, i).Value) = "男" Then
xb = 1
ElseIf Trim(r.Cells(1, i).Value) = "女" Then
xb = 2
Else
MsgBox "性别字段错误", vbOKOnly, "错误"
STOPRUN = 1
Exit Sub
End If
Case 4
Select Case Trim(r.Cells(1, i).Value)
Case "初级"
zc = 1
Case "中级"
zc = 2
Case "高级"
zc = 3
Case Else
MsgBox "职称字段错误", vbOKOnly, "错误"
STOPRUN = 1
Exit Sub
End Select
End Select
Next i

End Sub

Public Sub AutoInputContext()


STOPRUN = 0
Call GetData
If STOPRUN = 1 Then
Exit Sub
End If

mComGetIEWindows ("测试文档"
If mDocument Is Nothing Then
MsgBox "没有找到指定的窗必须先打开才可以自动填表", vbOKOnly, "错误"
Exit Sub
End If


Do While mWindow.Busy
DoEvents
Loop


With mDocument.forms(0)
.Item("textfield1".Value = xm
.Item("textfield2".Value = nl
.Item("rbgroup".Item(xb - 1).Checked = True
.Item("select1".Item(zc - 1).Selected = True
.Item("Submit1".Click
End With

'释放对象
Set mDocument = Nothing
Set mWindow = Nothing
End Sub
8楼2013-06-21 09:09:40
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

bongb

银虫 (小有名气)

送红花一朵
引用回帖:
6楼: Originally posted by ausir at 2013-06-21 07:14:09
Public i As Integer
Public Const XMS As Integer = 4

Sub test()
Dim r As Range
Dim startcol As Integer, endcol As Integer
Dim x1, x2, x3, x4

Dim s As String

startcol = 1
endcol = startco ...

谢谢,刚贴出我这边的完整代码,能帮忙看下解决问题不?
9楼2013-06-21 09:11:01
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖
相关版块跳转 我要订阅楼主 bongb 的主题更新
信息提示
请填处理意见