Excel精英培训网

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

[已解决]请求把两段VBA代码糅合在一个工作表里使用

[复制链接]
发表于 2013-3-15 15:31 | 显示全部楼层 |阅读模式
首先请问的是外部导入网上数据能否实现在一个工作表里同时使用2个网上链接或多个网上链接?如果可以,那么www.17500.cn/getData/3d.TXThttp://www.17500.cn/getData/p5.TXT如何实现在一个工作表里工作?目前我可以实现的是分别在2个单独的工作表里能够工作,我想使其能在一个工作表里一次性导入上面两个链接的数据。请老师出手调整下,谢谢!!!
附代码,一:
Private Sub CommandButton1_Click()

Range("A3:Q8000").Clear

k3dshijihao = "http://www.17500.cn/getData/3d.TXT"

d3s = "WData3D_All"

    Cells(2, 1) = "开奖期号"
    Cells(2, 2) = "开奖日期"
    Cells(2, 3) = "开"
    Cells(2, 4) = "奖"
    Cells(2, 5) = "号"

    Cells(2, 6) = "试"
    Cells(2, 7) = "机"
    Cells(2, 8) = "号"

    Cells(2, 9) = "机"
    Cells(2, 10) = "球"

    Cells(2, 11) = "投注总额"

    Cells(2, 12) = "单选注数"
    Cells(2, 13) = "金额"

    Cells(2, 14) = "组三注数"
    Cells(2, 15) = "金额"

    Cells(2, 16) = "组六注数"
    Cells(2, 17) = "金额"



    cz = k3dshijihao: czmc = d3s


     With ActiveSheet.QueryTables.Add(Connection:= _
         "TEXT;" & cz, Destination:=Range("A3"))
        .Name = czmc
        .FieldNames = True
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .TextFilePromptOnRefresh = False
        .TextFilePlatform = xlWindows
        .TextFileStartRow = 1
        .TextFileParseType = xlDelimited
        .TextFileTextQualifier = xlTextQualifierDoubleQuote
        .TextFileConsecutiveDelimiter = True
        .TextFileTabDelimiter = False
        .TextFileSemicolonDelimiter = False
        .TextFileCommaDelimiter = False
        .TextFileSpaceDelimiter = True
        .TextFileColumnDataTypes = Array(1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
        .TextFileTrailingMinusNumbers = True
        .Refresh BackgroundQuery:=False
    End With
Range("A" & (Application.Count(Range("a1:a8000")))).Select
End





End Sub

附代码,二:
Private Sub CommandButton1_Click()

Range("Z3:AP8000").Clear

k3dshijihao = "http://www.17500.cn/getData/p5.TXT"

d3s = "WData3D_All"

    Cells(2, 1) = "开奖期号"
    Cells(2, 2) = "开奖日期"
    Cells(2, 3) = "开"
    Cells(2, 4) = "奖"
    Cells(2, 5) = "号"
    Cells(2, 6) = " "
    Cells(2, 7) = " "
    Cells(2, 8) = "投注总额"

    Cells(2, 9) = "中奖注数"
    Cells(2, 10) = "单注奖金"

    cz = k3dshijihao: czmc = d3s


     With ActiveSheet.QueryTables.Add(Connection:= _
         "TEXT;" & cz, Destination:=Range("Z3"))
        .Name = czmc
        .FieldNames = True
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .TextFilePromptOnRefresh = False
        .TextFilePlatform = xlWindows
        .TextFileStartRow = 1
        .TextFileParseType = xlDelimited
        .TextFileTextQualifier = xlTextQualifierDoubleQuote
        .TextFileConsecutiveDelimiter = True
        .TextFileTabDelimiter = False
        .TextFileSemicolonDelimiter = False
        .TextFileCommaDelimiter = False
        .TextFileSpaceDelimiter = True
        .TextFileColumnDataTypes = Array(1, 2, 1, 1, 1, 1, 1, 1, 1, 1)
        .TextFileTrailingMinusNumbers = True
        .Refresh BackgroundQuery:=False
    End With
Range("A" & (Application.Count(Range("Z1:Z8000")))).Select
End





End Sub

最佳答案
2013-3-15 16:15
  1. Sub tset()
  2.     k3dshijihao = "http://www.17500.cn/getData/3d.TXT"

  3.     d3s = "WData3D_All"

  4.     Cells(2, 1) = "开奖期号"
  5.     Cells(2, 2) = "开奖日期"
  6.     Cells(2, 3) = "开"
  7.     Cells(2, 4) = "奖"
  8.     Cells(2, 5) = "号"

  9.     Cells(2, 6) = "试"
  10.     Cells(2, 7) = "机"
  11.     Cells(2, 8) = "号"

  12.     Cells(2, 9) = "机"
  13.     Cells(2, 10) = "球"

  14.     Cells(2, 11) = "投注总额"

  15.     Cells(2, 12) = "单选注数"
  16.     Cells(2, 13) = "金额"

  17.     Cells(2, 14) = "组三注数"
  18.     Cells(2, 15) = "金额"

  19.     Cells(2, 16) = "组六注数"
  20.     Cells(2, 17) = "金额"

  21.     cz = k3dshijihao: czmc = d3s

  22.     With ActiveSheet.QueryTables.Add(Connection:= _
  23.                                      "TEXT;" & cz, Destination:=Range("A3"))
  24.         .Name = czmc
  25.         .FieldNames = True
  26.         .RowNumbers = False
  27.         .FillAdjacentFormulas = False
  28.         .PreserveFormatting = True
  29.         .RefreshOnFileOpen = False
  30.         .RefreshStyle = xlInsertDeleteCells
  31.         .SavePassword = False
  32.         .SaveData = True
  33.         .AdjustColumnWidth = True
  34.         .RefreshPeriod = 0
  35.         .TextFilePromptOnRefresh = False
  36.         .TextFilePlatform = xlWindows
  37.         .TextFileStartRow = 1
  38.         .TextFileParseType = xlDelimited
  39.         .TextFileTextQualifier = xlTextQualifierDoubleQuote
  40.         .TextFileConsecutiveDelimiter = True
  41.         .TextFileTabDelimiter = False
  42.         .TextFileSemicolonDelimiter = False
  43.         .TextFileCommaDelimiter = False
  44.         .TextFileSpaceDelimiter = True
  45.         .TextFileColumnDataTypes = Array(1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
  46.         .TextFileTrailingMinusNumbers = True
  47.         .Refresh BackgroundQuery:=False
  48.     End With

  49.     Range("Z3:AP8000").Clear

  50.     k3dshijihao = "http://www.17500.cn/getData/p5.TXT"

  51.     d3s = "WData3D_All"

  52.     Cells(2, 1) = "开奖期号"
  53.     Cells(2, 2) = "开奖日期"
  54.     Cells(2, 3) = "开"
  55.     Cells(2, 4) = "奖"
  56.     Cells(2, 5) = "号"
  57.     Cells(2, 6) = " "
  58.     Cells(2, 7) = " "
  59.     Cells(2, 8) = "投注总额"

  60.     Cells(2, 9) = "中奖注数"
  61.     Cells(2, 10) = "单注奖金"

  62.     cz = k3dshijihao: czmc = d3s


  63.     With ActiveSheet.QueryTables.Add(Connection:= _
  64.                                      "TEXT;" & cz, Destination:=Range("Z3"))
  65.         .Name = czmc
  66.         .FieldNames = True
  67.         .RowNumbers = False
  68.         .FillAdjacentFormulas = False
  69.         .PreserveFormatting = True
  70.         .RefreshOnFileOpen = False
  71.         .RefreshStyle = xlInsertDeleteCells
  72.         .SavePassword = False
  73.         .SaveData = True
  74.         .AdjustColumnWidth = True
  75.         .RefreshPeriod = 0
  76.         .TextFilePromptOnRefresh = False
  77.         .TextFilePlatform = xlWindows
  78.         .TextFileStartRow = 1
  79.         .TextFileParseType = xlDelimited
  80.         .TextFileTextQualifier = xlTextQualifierDoubleQuote
  81.         .TextFileConsecutiveDelimiter = True
  82.         .TextFileTabDelimiter = False
  83.         .TextFileSemicolonDelimiter = False
  84.         .TextFileCommaDelimiter = False
  85.         .TextFileSpaceDelimiter = True
  86.         .TextFileColumnDataTypes = Array(1, 2, 1, 1, 1, 1, 1, 1, 1, 1)
  87.         .TextFileTrailingMinusNumbers = True
  88.         .Refresh BackgroundQuery:=False
  89.     End With
  90. End Sub
复制代码

请把两个工作表的导入网上数据功能合并在一个工作表里.rar

271.47 KB, 下载次数: 16

excel精英培训的微信平台,每天都会发送excel学习教程和资料。扫一扫明天就可以收到新教程
发表于 2013-3-15 16:08 | 显示全部楼层
两个过程都无误的话,你就先调用就成了。

跟写入几个工作表无关,只要注意写入的区域。
回复

使用道具 举报

发表于 2013-3-15 16:15 | 显示全部楼层    本楼为最佳答案   
  1. Sub tset()
  2.     k3dshijihao = "http://www.17500.cn/getData/3d.TXT"

  3.     d3s = "WData3D_All"

  4.     Cells(2, 1) = "开奖期号"
  5.     Cells(2, 2) = "开奖日期"
  6.     Cells(2, 3) = "开"
  7.     Cells(2, 4) = "奖"
  8.     Cells(2, 5) = "号"

  9.     Cells(2, 6) = "试"
  10.     Cells(2, 7) = "机"
  11.     Cells(2, 8) = "号"

  12.     Cells(2, 9) = "机"
  13.     Cells(2, 10) = "球"

  14.     Cells(2, 11) = "投注总额"

  15.     Cells(2, 12) = "单选注数"
  16.     Cells(2, 13) = "金额"

  17.     Cells(2, 14) = "组三注数"
  18.     Cells(2, 15) = "金额"

  19.     Cells(2, 16) = "组六注数"
  20.     Cells(2, 17) = "金额"

  21.     cz = k3dshijihao: czmc = d3s

  22.     With ActiveSheet.QueryTables.Add(Connection:= _
  23.                                      "TEXT;" & cz, Destination:=Range("A3"))
  24.         .Name = czmc
  25.         .FieldNames = True
  26.         .RowNumbers = False
  27.         .FillAdjacentFormulas = False
  28.         .PreserveFormatting = True
  29.         .RefreshOnFileOpen = False
  30.         .RefreshStyle = xlInsertDeleteCells
  31.         .SavePassword = False
  32.         .SaveData = True
  33.         .AdjustColumnWidth = True
  34.         .RefreshPeriod = 0
  35.         .TextFilePromptOnRefresh = False
  36.         .TextFilePlatform = xlWindows
  37.         .TextFileStartRow = 1
  38.         .TextFileParseType = xlDelimited
  39.         .TextFileTextQualifier = xlTextQualifierDoubleQuote
  40.         .TextFileConsecutiveDelimiter = True
  41.         .TextFileTabDelimiter = False
  42.         .TextFileSemicolonDelimiter = False
  43.         .TextFileCommaDelimiter = False
  44.         .TextFileSpaceDelimiter = True
  45.         .TextFileColumnDataTypes = Array(1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
  46.         .TextFileTrailingMinusNumbers = True
  47.         .Refresh BackgroundQuery:=False
  48.     End With

  49.     Range("Z3:AP8000").Clear

  50.     k3dshijihao = "http://www.17500.cn/getData/p5.TXT"

  51.     d3s = "WData3D_All"

  52.     Cells(2, 1) = "开奖期号"
  53.     Cells(2, 2) = "开奖日期"
  54.     Cells(2, 3) = "开"
  55.     Cells(2, 4) = "奖"
  56.     Cells(2, 5) = "号"
  57.     Cells(2, 6) = " "
  58.     Cells(2, 7) = " "
  59.     Cells(2, 8) = "投注总额"

  60.     Cells(2, 9) = "中奖注数"
  61.     Cells(2, 10) = "单注奖金"

  62.     cz = k3dshijihao: czmc = d3s


  63.     With ActiveSheet.QueryTables.Add(Connection:= _
  64.                                      "TEXT;" & cz, Destination:=Range("Z3"))
  65.         .Name = czmc
  66.         .FieldNames = True
  67.         .RowNumbers = False
  68.         .FillAdjacentFormulas = False
  69.         .PreserveFormatting = True
  70.         .RefreshOnFileOpen = False
  71.         .RefreshStyle = xlInsertDeleteCells
  72.         .SavePassword = False
  73.         .SaveData = True
  74.         .AdjustColumnWidth = True
  75.         .RefreshPeriod = 0
  76.         .TextFilePromptOnRefresh = False
  77.         .TextFilePlatform = xlWindows
  78.         .TextFileStartRow = 1
  79.         .TextFileParseType = xlDelimited
  80.         .TextFileTextQualifier = xlTextQualifierDoubleQuote
  81.         .TextFileConsecutiveDelimiter = True
  82.         .TextFileTabDelimiter = False
  83.         .TextFileSemicolonDelimiter = False
  84.         .TextFileCommaDelimiter = False
  85.         .TextFileSpaceDelimiter = True
  86.         .TextFileColumnDataTypes = Array(1, 2, 1, 1, 1, 1, 1, 1, 1, 1)
  87.         .TextFileTrailingMinusNumbers = True
  88.         .Refresh BackgroundQuery:=False
  89.     End With
  90. End Sub
复制代码
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-27 04:02 , Processed in 0.325704 second(s), 11 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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