Excel精英培训网

 找回密码
 注册
数据透视表40+个常用小技巧,让你一次学会!
查看: 3487|回复: 9

[已解决]求助VBA查找引用,谢谢

[复制链接]
发表于 2010-8-15 18:02 | 显示全部楼层 |阅读模式
32N7qoeR.rar (67.21 KB, 下载次数: 70)
excel精英培训的微信平台,每天都会发送excel学习教程和资料。扫一扫明天就可以收到新教程
 楼主| 发表于 2010-8-15 18:03 | 显示全部楼层
回复

使用道具 举报

 楼主| 发表于 2010-8-15 19:27 | 显示全部楼层

来人啊,快给答案啊,再不给答案就要出人命啦
回复

使用道具 举报

 楼主| 发表于 2010-8-15 19:48 | 显示全部楼层

Private Sub CommandButton1_Click()
Sheets("回迁二期入住明细").Select
Dim xyid As String
Dim hang As Integer
Dim tempmsgbox As VbMsgBoxResult
xyid = InputBox("需要查询的ID", "查询回迁户协议号")
For hang = 1 To 1000
  If Sheets("回迁二期入住明细").Cells(1, hang) = xyid Then  (应用程序或定义对象错误)
     tempmsgbox = MsgBox("协议号" & Cells(1, hang) & Chr(13) & "姓名" & Cells(2, hang))
     Workbooks("入户结算单(新)").Sheets("入户结算通知单模板").Range("f3") = Workbooks("入户结算单(新)").Sheets("回迁二期入住明细").Cells(2, hang)
     Workbooks("入户结算单(新)").Sheets("入户结算通知单模板").Range("r3") = Workbooks("入户结算单(新)").Sheets("回迁二期入住明细").Cells(3, hang)
     Workbooks("入户结算单(新)").Sheets("入户结算通知单模板").Range("AO3") = Workbooks("入户结算单(新)").Sheets("回迁二期入住明细").Cells(4, hang)
     Exit For
  End If
Next hang
 tempmsgbox = MsgBox("未查到该回迁户")
End Sub
回复

使用道具 举报

发表于 2010-8-15 20:00 | 显示全部楼层

If Sheets("回迁二期入住明细").Cells(hang, 1) = xyid Then 
回复

使用道具 举报

 楼主| 发表于 2010-8-15 20:12 | 显示全部楼层

 tempmsgbox = MsgBox("协议号" & Cells(1, hang) & Chr(13) & "姓名" & Cells(2, hang))(只显示了"协议号"字段与"姓名"字段,具体内容没有)
     Workbooks("入户结算单(新)").Sheets("入户结算通知单模板").Range("f3") = Workbooks("入户结算单(新)").Sheets("回迁二期入住明细").Cells(hang, 2)(这行提示下标越界)
回复

使用道具 举报

 楼主| 发表于 2010-8-15 20:25 | 显示全部楼层

调试好啦,谢谢

回复

使用道具 举报

发表于 2010-8-15 20:36 | 显示全部楼层    本楼为最佳答案   

看你的代码就累,给你整好看点

Private Sub CommandButton1_Click()

    Dim xyid As String
    Dim hang As Integer
    Dim sh1 As Worksheet
    Dim sh2 As Worksheet

    xyid = InputBox("需要查询的ID", "查询回迁户协议号")
    Set sh1 = ThisWorkbook.Sheets("入户结算通知单模板")
    Set sh2 = ThisWorkbook.Sheets("回迁二期入住明细")
    sh2.Activate

    For hang = 2 To 1000
        If sh2.Cells(hang, 1).Value = xyid Then
            MsgBox ("协议号" & sh2.Cells(hang, 1) & Chr(13) & "姓名" & sh2.Cells(hang, 2))
            s = sh1.Range("A1:A2")
            sh1.Range("f3") = sh2.Cells(hang, 2)
            sh1.Range("r3") = sh2.Cells(hang, 3)
            sh1.Range("AO3") = sh2.Cells(hang, 4)
            Exit For
        End If
    Next hang

    If hang = 1001 Then MsgBox ("未查到该回迁户")
    sh1.Activate
End Sub

回复

使用道具 举报

 楼主| 发表于 2010-8-15 21:04 | 显示全部楼层

切.
回复

使用道具 举报

 楼主| 发表于 2010-8-15 21:12 | 显示全部楼层

QUOTE:
以下是引用uranus1997在2010-8-15 20:36:00的发言:

看你的代码就累,给你整好看点

Private Sub CommandButton1_Click()

    Dim xyid As String
    Dim hang As Integer
    Dim sh1 As Worksheet
    Dim sh2 As Worksheet

    xyid = InputBox("需要查询的ID", "查询回迁户协议号")
    Set sh1 = ThisWorkbook.Sheets("入户结算通知单模板")
    Set sh2 = ThisWorkbook.Sheets("回迁二期入住明细")
    sh2.Activate

    For hang = 2 To 1000
        If sh2.Cells(hang, 1).Value = xyid Then
            MsgBox ("协议号" & sh2.Cells(hang, 1) & Chr(13) & "姓名" & sh2.Cells(hang, 2))
            s = sh1.Range("A1:A2")
            sh1.Range("f3") = sh2.Cells(hang, 2)
            sh1.Range("r3") = sh2.Cells(hang, 3)
            sh1.Range("AO3") = sh2.Cells(hang, 4)
            Exit For
        End If
    Next hang

    If hang = 1001 Then MsgBox ("未查到该回迁户")
    sh1.Activate
End Sub

s = sh1.Range("A1:A2")

上边这个等式是什么意思呀.看不明白,好像没用上吧

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|Archiver|Excel精英培训 ( 豫ICP备11015029号 )

GMT+8, 2024-5-16 05:49 , Processed in 0.184226 second(s), 10 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表