| 查看: 2543 | 回复: 12 | ||
[求助]
求急救: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朵)
» 猜你喜欢
面上项目申报
已经有3人回复
有时候真觉得大城市人没有县城人甚至个体户幸福
已经有5人回复
酰胺脱乙酰基
已经有9人回复
CSC & MSCA 博洛尼亚大学能源材料课题组博士/博士后招生|MSCA经费充足、排名优
已经有5人回复
有70后还继续奋斗在职场上的吗?
已经有6人回复
博士延得我,科研能力直往上蹿
已经有7人回复
退学或坚持读
已经有27人回复
面上基金申报没有其他的参与者成吗
已经有5人回复
遇见不省心的家人很难过
已经有22人回复
michaelwlin
木虫 (正式写手)
- 应助: 2 (幼儿园)
- 金币: 5229.8
- 红花: 1
- 帖子: 653
- 在线: 47.1小时
- 虫号: 1070278
- 注册: 2010-08-06
- 性别: GG
- 专业: 结构工程
7楼2013-06-21 08:44:41
michaelwlin
木虫 (正式写手)
- 应助: 2 (幼儿园)
- 金币: 5229.8
- 红花: 1
- 帖子: 653
- 在线: 47.1小时
- 虫号: 1070278
- 注册: 2010-08-06
- 性别: GG
- 专业: 结构工程
10楼2013-06-21 11:14:40
2楼2013-06-19 18:34:04
3楼2013-06-20 11:16:10
michaelwlin
木虫 (正式写手)
- 应助: 2 (幼儿园)
- 金币: 5229.8
- 红花: 1
- 帖子: 653
- 在线: 47.1小时
- 虫号: 1070278
- 注册: 2010-08-06
- 性别: GG
- 专业: 结构工程
4楼2013-06-20 11:21:54
5楼2013-06-20 22:28:22
ausir
木虫 (正式写手)
- 应助: 18 (小学生)
- 金币: 2008.2
- 红花: 4
- 帖子: 500
- 在线: 178.5小时
- 虫号: 1939287
- 注册: 2012-08-15
- 性别: GG
- 专业: 集成电路制造与封装
【答案】应助回帖
感谢参与,应助指数 +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, 否则 A0 0 是没有意义的。 |
» 本帖已获得的红花(最新10朵)
6楼2013-06-21 07:14:09
送红花一朵 |
感谢各位达人回答, 现在遇到的问题有两个: 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" .ClickEnd With '释放对象 Set mDocument = Nothing Set mWindow = Nothing End Sub |
8楼2013-06-21 09:09:40
9楼2013-06-21 09:11:01













+ startcol \ 26 - 1)
回复此楼
0 是没有意义的。