Excel精英培训网

 找回密码
 注册
数据透视表40+个常用小技巧,让你一次学会!
楼主: 过江龙

[已解决]用VB6.0封装代码的问题

[复制链接]
 楼主| 发表于 2013-11-3 15:29 | 显示全部楼层
学习了一上午,终于整好了,VB6.0源代码放出,请老师们优化。
Sub hongtong()
   Dim XLAPP As Object
   Set XLAPP = GetObject(, "Excel.Application")
XLAPP.ScreenUpdating = False
   Dim x%, y%
With XLAPP.FileDialog(msoFileDialogOpen)
        .Title = "请选择从大平台中导出的Excel文件申报计划"
        If .Show = -1 Then
           XLAPP.Workbooks.Open .SelectedItems(1)
         Else
          XLAPP.ScreenUpdating = True
          Exit Sub
        End If
    End With
   
    Name = XLAPP.ActiveWorkbook.Name
    XLAPP.Sheets(1).Select
   XLAPP.Sheets(1).Copy Before:=XLAPP.Workbooks("预算拨款申请表.xls").Sheets(1)
    For Each wb In XLAPP.Workbooks
        If wb.Name = Name Then
            wb.Close False
        End If
    Next
    XLAPP.Sheets(1).Select
    XLAPP.Sheets(1).Name = "temp"
   
XLAPP.Sheets("temp").Select
With XLAPP.Sheets("预内资金")
i = .[A65536].End(xlUp).Row - 2
.Range("A5:J" & i).ClearContents
.Range("H" & i + 1).ClearContents
i = XLAPP.Sheets("专户资金").[A65536].End(xlUp).Row - 2
XLAPP.Sheets("专户资金").Range("A5:J" & i).ClearContents
XLAPP.Sheets("专户资金").Range("H" & i + 1).ClearContents

danwei = XLAPP.InputBox("请输入申请单位:", "提示", "宜宾市翠屏区宗场镇中心小学校", , , , , 2)
If danwei = False Then
    XLAPP.DisplayAlerts = False '屏弊删除提示
      XLAPP.Sheets("temp").Delete
    XLAPP.DisplayAlerts = True
    XLAPP.Sheets("预内资金").Select
    XLAPP.ScreenUpdating = True
Exit Sub
End If
bianma = XLAPP.InputBox("请输入单位编码:", "提示", 113757, , , , , 2)
If bianma = False Then
    XLAPP.DisplayAlerts = False '屏弊删除提示
      XLAPP.Sheets("temp").Delete
    XLAPP.DisplayAlerts = True
    XLAPP.Sheets("预内资金").Select
    XLAPP.ScreenUpdating = True
Exit Sub
End If
.Range("A2") = "申请单位(盖章):" & danwei & "           单位编码:" & bianma & "           " & XLAPP.WorksheetFunction.Text(Now, "yyyy""年""m""月""d""日""") & "            金额单位:元"
XLAPP.Sheets("专户资金").Range("A2") = "申请单位(盖章):" & danwei & "           单位编码:" & bianma & "           " & XLAPP.WorksheetFunction.Text(Now, "yyyy""年""m""月""d""日""") & "            金额单位:元"



y = 5
f = 5
    For Each c In XLAPP.Sheets("temp").Range("D3:D" & XLAPP.Sheets("temp").[D65536].End(xlUp).Row)
        If Left(c, XLAPP.Search("-", c) - 1) <> "12" Then 'Left第一个起,Right最后一个起,Len文本个数
            x = c.Row
             .Cells(y, 1) = Left(XLAPP.Sheets("temp").Cells(x, 5), XLAPP.Search("-", XLAPP.Sheets("temp").Cells(x, 5)) - 1)
             .Cells(y, 2) = Right(XLAPP.Sheets("temp").Cells(x, 5), Len(XLAPP.Sheets("temp").Cells(x, 5)) - XLAPP.Search("-", XLAPP.Sheets("temp").Cells(x, 5)))
             .Cells(y, 3) = Left(XLAPP.Sheets("temp").Cells(x, 6), XLAPP.Search("-", XLAPP.Sheets("temp").Cells(x, 6)) - 1)
             .Cells(y, 4) = Right(XLAPP.Sheets("temp").Cells(x, 6), Len(XLAPP.Sheets("temp").Cells(x, 6)) - XLAPP.Search("-", XLAPP.Sheets("temp").Cells(x, 6)))
             .Cells(y, 5) = Right(XLAPP.Sheets("temp").Cells(x, 4), Len(XLAPP.Sheets("temp").Cells(x, 4)) - XLAPP.Search("-", XLAPP.Sheets("temp").Cells(x, 4)))
             .Cells(y, 6) = Right(XLAPP.Sheets("temp").Cells(x, 11), Len(XLAPP.Sheets("temp").Cells(x, 11)) - XLAPP.Search("-", XLAPP.Sheets("temp").Cells(x, 11)))
             .Cells(y, 7) = .Cells(y, 4)
             .Cells(y, 8) = XLAPP.Cells(x, 7)
            y = y + 1
            If .Cells(y, 1) = "合            计" Then
            .Cells(y, 1).EntireRow.Insert
            End If
          Else
            x = c.Row
             XLAPP.Sheets("专户资金").Cells(f, 1) = Left(XLAPP.Cells(x, 5), XLAPP.Search("-", XLAPP.Cells(x, 5)) - 1)
             XLAPP.Sheets("专户资金").Cells(f, 2) = Right(XLAPP.Cells(x, 5), Len(XLAPP.Cells(x, 5)) - XLAPP.Search("-", XLAPP.Cells(x, 5)))
             XLAPP.Sheets("专户资金").Cells(f, 3) = Left(XLAPP.Cells(x, 6), Application.Search("-", XLAPP.Cells(x, 6)) - 1)
             XLAPP.Sheets("专户资金").Cells(f, 4) = Right(XLAPP.Cells(x, 6), Len(XLAPP.Cells(x, 6)) - XLAPP.Search("-", XLAPP.Cells(x, 6)))
             XLAPP.Sheets("专户资金").Cells(f, 5) = Right(XLAPP.Cells(x, 4), Len(XLAPP.Cells(x, 4)) - XLAPP.Search("-", XLAPP.Cells(x, 4)))
             XLAPP.Sheets("专户资金").Cells(f, 6) = Right(XLAPP.Cells(x, 11), Len(XLAPP.Cells(x, 11)) - XLAPP.Search("-", XLAPP.Cells(x, 11)))
             XLAPP.Sheets("专户资金").Cells(f, 7) = XLAPP.Sheets("专户资金").Cells(f, 4)
             XLAPP.Sheets("专户资金").Cells(f, 8) = XLAPP.Cells(x, 7)
            f = f + 1
            If XLAPP.Sheets("专户资金").Cells(f, 1) = "合            计" Then
            XLAPP.Sheets("专户资金").Cells(f, 1).EntireRow.Insert
            End If
        End If
    Next c
    For Each d In .Range("A5:A" & .[A65536].End(xlUp).Row)
        If d = "合            计" Then
            j = d.Row
            .Cells(j, 8) = XLAPP.Sum(.Range("H5:H" & j - 1))
            If j > 16 Then
             .Range("A16:A" & j).SpecialCells(4).Delete (3)
            End If
        End If
    Next d
   
    For Each t In XLAPP.Sheets("专户资金").Range("A5:A" & XLAPP.Sheets("专户资金").[A65536].End(xlUp).Row)
        If t = "合            计" Then
            k = t.Row
            XLAPP.Sheets("专户资金").Cells(k, 8) = XLAPP.Sum(XLAPP.Sheets("专户资金").Range("H5:H" & k - 1))
            If k > 16 Then
             XLAPP.Sheets("专户资金").Range("A16:A" & k).SpecialCells(4).Delete (3)
            End If
        End If
    Next t
End With
    XLAPP.DisplayAlerts = False '屏弊删除提示
      XLAPP.Sheets("temp").Delete
   XLAPP.DisplayAlerts = True
    XLAPP.Sheets("预内资金").Select
    XLAPP.ScreenUpdating = True
End Sub
excel精英培训的微信平台,每天都会发送excel学习教程和资料。扫一扫明天就可以收到新教程
回复

使用道具 举报

发表于 2013-11-3 18:21 | 显示全部楼层    本楼为最佳答案   
  1. Dim xlApp As Excel.Application

  2. Public Sub JL()
  3.     Dim x%, y%
  4.     Dim Name$, WBname$
  5.     Dim i%, j%, k%
  6.     Dim Danwei, Bianma, c, d, t
  7.     Dim f As Long

  8.     Set xlApp = GetObject(, "Excel.application")

  9.     With xlApp
  10.         WBname = .ActiveWorkbook.Name
  11.         .Application.ScreenUpdating = False
  12.         With .Application.FileDialog(1)
  13.             .Title = "请选择要导入的Excel文件"
  14.             If .Show = -1 Then
  15.                 xlApp.Workbooks.Open .SelectedItems(1)
  16.             Else
  17.                 Exit Sub
  18.             End If
  19.         End With

  20.         Name = .ActiveWorkbook.Name
  21.         With .ActiveWorkbook.Worksheets(1)
  22.             .Select
  23.             .Copy Before:=xlApp.Workbooks(WBname).Worksheets(1)
  24.         End With
  25.         .Workbooks(Name).Close False

  26.         With .Sheets(1)
  27.             .Select
  28.             .Name = "temp"
  29.         End With

  30.         With .Worksheets("预内资金")    'with sheet1
  31.             i = .[A65536].End(xlUp).Row - 2
  32.             .Range("A5:J" & i).ClearContents
  33.             .Range("H" & i + 1).ClearContents
  34.         End With

  35.         With xlApp.Worksheets("专户资金")
  36.             i = .[A65536].End(xlUp).Row - 2
  37.             .Range("A5:J" & i).ClearContents
  38.             .Range("H" & i + 1).ClearContents
  39.         End With

  40.         Danwei = .Application.InputBox("请输入申请单位:", "提示", "宜宾市翠屏区宗场镇中心小学校", , , , , 2)
  41.         If Danwei = False Then
  42.             删除temp表
  43.             .Application.ScreenUpdating = True
  44.             Exit Sub
  45.         End If
  46.         Bianma = .Application.InputBox("请输入单位编码:", "提示", 113757, , , , , 2)
  47.         If Bianma = False Then
  48.             删除temp表
  49.             .Application.ScreenUpdating = True
  50.             Exit Sub
  51.         End If

  52.         With .Worksheets("预内资金")
  53.             .Range("A2") = "申请单位(盖章):" & Danwei & "           单位编码:" & Bianma & "           " & Format(Now, "yyyy""年""m""月""d""日""") & "            金额单位:元"
  54.             xlApp.Worksheets("专户资金").Range("A2") = "申请单位(盖章):" & Danwei & "           单位编码:" & Bianma & "           " & Format(Now, "yyyy""年""m""月""d""日""") & "            金额单位:元"

  55.             y = 5
  56.             f = 5
  57.             For Each c In xlApp.Range("D3:D" & xlApp.Range("D65536").End(xlUp).Row)
  58.                 If Left(c, xlApp.Application.Search("-", c) - 1) <> "12" Then    'Left第一个起,Right最后一个起,Len文本个数
  59.                     x = c.Row
  60.                     .Cells(y, 1) = Left(xlApp.Cells(x, 5), xlApp.Application.Search("-", xlApp.Cells(x, 5)) - 1)
  61.                     .Cells(y, 2) = Right(xlApp.Cells(x, 5), Len(xlApp.Cells(x, 5)) - xlApp.Application.Search("-", xlApp.Cells(x, 5)))
  62.                     .Cells(y, 3) = Left(xlApp.Cells(x, 6), xlApp.Application.Search("-", xlApp.Cells(x, 6)) - 1)
  63.                     .Cells(y, 4) = Right(xlApp.Cells(x, 6), Len(xlApp.Cells(x, 6)) - xlApp.Application.Search("-", xlApp.Cells(x, 6)))
  64.                     .Cells(y, 5) = Right(xlApp.Cells(x, 4), Len(xlApp.Cells(x, 4)) - xlApp.Application.Search("-", xlApp.Cells(x, 4)))
  65.                     .Cells(y, 6) = Right(xlApp.Cells(x, 11), Len(xlApp.Cells(x, 11)) - xlApp.Application.Search("-", xlApp.Cells(x, 11)))
  66.                     .Cells(y, 7) = .Cells(y, 4)
  67.                     .Cells(y, 8) = xlApp.Cells(x, 7)
  68.                     y = y + 1
  69.                     If .Cells(y, 1) = "合            计" Then
  70.                         .Cells(y, 1).EntireRow.Insert
  71.                     End If
  72.                 Else
  73.                     x = c.Row
  74.                     xlApp.Worksheets("专户资金").Cells(f, 1) = Left(xlApp.Cells(x, 5), xlApp.Application.Search("-", xlApp.Cells(x, 5)) - 1)
  75.                     xlApp.Worksheets("专户资金").Cells(f, 2) = Right(xlApp.Cells(x, 5), Len(xlApp.Cells(x, 5)) - xlApp.Application.Search("-", xlApp.Cells(x, 5)))
  76.                     xlApp.Worksheets("专户资金").Cells(f, 3) = Left(xlApp.Cells(x, 6), xlApp.Application.Search("-", xlApp.Cells(x, 6)) - 1)
  77.                     xlApp.Worksheets("专户资金").Cells(f, 4) = Right(xlApp.Cells(x, 6), Len(xlApp.Cells(x, 6)) - xlApp.Application.Search("-", xlApp.Cells(x, 6)))
  78.                     xlApp.Worksheets("专户资金").Cells(f, 5) = Right(xlApp.Cells(x, 4), Len(xlApp.Cells(x, 4)) - xlApp.Application.Search("-", xlApp.Cells(x, 4)))
  79.                     xlApp.Worksheets("专户资金").Cells(f, 6) = Right(xlApp.Cells(x, 11), Len(xlApp.Cells(x, 11)) - xlApp.Application.Search("-", xlApp.Cells(x, 11)))
  80.                     xlApp.Worksheets("专户资金").Cells(f, 7) = xlApp.Worksheets("专户资金").Cells(f, 4)
  81.                     xlApp.Worksheets("专户资金").Cells(f, 8) = xlApp.Cells(x, 7)
  82.                     f = f + 1
  83.                     If xlApp.Worksheets("专户资金").Cells(f, 1) = "合            计" Then
  84.                         xlApp.Worksheets("专户资金").Cells(f, 1).EntireRow.Insert
  85.                     End If
  86.                 End If
  87.             Next c
  88.             For Each d In .Range("A5:A" & .[A65536].End(xlUp).Row)
  89.                 If d = "合            计" Then
  90.                     j = d.Row
  91.                     .Cells(j, 8) = Application.Sum(.Range("H5:H" & j - 1))
  92.                     If j > 16 Then
  93.                         .Range("A16:A" & j).SpecialCells(4).Delete (3)
  94.                     End If
  95.                 End If
  96.             Next d

  97.             For Each t In xlApp.Worksheets("专户资金").Range("A5:A" & xlApp.Worksheets("专户资金").[A65536].End(xlUp).Row)
  98.                 If t = "合            计" Then
  99.                     k = t.Row
  100.                     xlApp.Worksheets("专户资金").Cells(k, 8) = Application.Sum(xlApp.Worksheets("专户资金").Range("H5:H" & k - 1))
  101.                     If k > 16 Then
  102.                         xlApp.Worksheets("专户资金").Range("A16:A" & k).SpecialCells(4).Delete (3)
  103.                     End If
  104.                 End If
  105.             Next t
  106.         End With
  107.         删除temp表
  108.         Application.ScreenUpdating = True
  109.     End With
  110.     MsgBox "导入完成", vbInformation + vbOKOnly

  111. End Sub


  112. Private Sub 删除temp表()
  113.     With xlApp
  114.         .Application.DisplayAlerts = False    '屏弊删除提示
  115.         .Sheets("temp").Delete
  116.         .Application.DisplayAlerts = True
  117.         .Worksheets("预内资金").Select
  118.     End With
  119. End Sub
复制代码
回复

使用道具 举报

发表于 2013-11-3 18:22 | 显示全部楼层
回复

使用道具 举报

发表于 2013-11-3 18:23 | 显示全部楼层
另外,能用VBA函数的情况下尽量用VBA的函数,象search就可以用instr来代替,text用format
回复

使用道具 举报

发表于 2013-11-3 19:14 | 显示全部楼层
在VB6里面,打开你的工程,

然后照下面的操作进行调试

运行你的工程,在弹出来的 工程属性窗口中》调试 》启动工程时》选择等待创建部件
点确定

其实按了F5 弹出来的窗口直接点确定就行了

接下来,在你的工作薄里面写代码
你可以引用(每次按下F5都需要重新引用) ,也可以创建

以你附件里的来说吧,你是引用的,过程写好以后,就可以直接用F8进行测试了,
会自动跳转到VB里面去的,你就和平时在工作薄里那样调试找问题就行了
回复

使用道具 举报

 楼主| 发表于 2013-11-4 08:44 | 显示全部楼层
hwc2ycy 发表于 2013-11-3 18:22
最好用数组吧,一次读取。

数组当然好,能提高运行速度。但我对数组不熟悉,请老师帮我优化,也好学习下,感谢!!
回复

使用道具 举报

发表于 2013-11-4 09:05 | 显示全部楼层
过江龙 发表于 2013-11-4 08:44
数组当然好,能提高运行速度。但我对数组不熟悉,请老师帮我优化,也好学习下,感谢!!

数组又不难,比封装学起来容易多了。

【VBA字典数组201301班】第一讲 课件
http://www.excelpx.com/thread-312398-1-1.html

这是数组的课件,你自己学习下嘛。
回复

使用道具 举报

发表于 2013-11-4 09:07 | 显示全部楼层
用了数组,复制工作表,重命名TEMP,删除这些操作都不需要了。
回复

使用道具 举报

发表于 2013-11-4 09:39 | 显示全部楼层
过江龙 发表于 2013-11-3 11:48
VBA代码:
Sub JL()
Dim x%, y%

太牛了,学习下
回复

使用道具 举报

发表于 2013-11-4 13:24 | 显示全部楼层
不错,整上封装了,不过这个也只是玩玩而以,没大的必要哦!
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-6 17:33 , Processed in 0.334702 second(s), 8 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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