Excel精英培训网

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

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

[复制链接]
发表于 2013-11-3 09:37 | 显示全部楼层 |阅读模式
本帖最后由 过江龙 于 2013-11-4 08:59 编辑

请老师帮忙,看了几天的封装代码的几个列子,自己学将代码封装,怎么来出错。有哪位老师能否指点下,最好将VB6.0的源码放出来学习研究哈,谢谢!
最佳答案
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
复制代码

封装代码.zip

27.52 KB, 下载次数: 36

 楼主| 发表于 2013-11-3 09:54 | 显示全部楼层
问题可能出在将VBA代码转换VB代码上。
回复

使用道具 举报

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

使用道具 举报

 楼主| 发表于 2013-11-3 10:55 | 显示全部楼层
是哪集哟?
回复

使用道具 举报

发表于 2013-11-3 11:06 | 显示全部楼层
楼主把在工作簿中原来没封装的VBA代码上传看看。

回复

使用道具 举报

发表于 2013-11-3 11:17 | 显示全部楼层
把要导入的数据测试文件也发上来一份。
回复

使用道具 举报

发表于 2013-11-3 11:17 | 显示全部楼层
你先保证未封装之前的VBA代码能正常工作。
回复

使用道具 举报

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

使用道具 举报

 楼主| 发表于 2013-11-3 11:44 | 显示全部楼层
hwc2ycy 发表于 2013-11-3 11:17
你先保证未封装之前的VBA代码能正常工作。

这个能保证,已经在2003中运行了很多遍了,正常。请问老师,你能发一个校长这方面的视频吗?
回复

使用道具 举报

 楼主| 发表于 2013-11-3 11:48 | 显示全部楼层
hwc2ycy 发表于 2013-11-3 11:06
楼主把在工作簿中原来没封装的VBA代码上传看看。

VBA代码:
Sub JL()
Dim x%, y%
Application.ScreenUpdating = False
With Application.FileDialog(msoFileDialogOpen)
        .Title = "请选择要导入的Excel文件"
        If .Show = -1 Then
          Workbooks.Open .SelectedItems(1)
         Else
          Exit Sub
        End If
    End With
   
    Name = ActiveWorkbook.Name
    Sheets(1).Select
    Sheets(1).Copy Before:=Workbooks("预算拨款申请表.xls").Sheets(1)
    For Each wb In Workbooks
        If wb.Name = Name Then
            wb.Close False
        End If
    Next
    Sheets(1).Select
    Sheets(1).Name = "temp"
   
Sheets("temp").Select
With Sheet1
i = .[A65536].End(xlUp).Row - 2
.Range("A5:J" & i).ClearContents
.Range("H" & i + 1).ClearContents
i = Sheet2.[A65536].End(xlUp).Row - 2
Sheet2.Range("A5:J" & i).ClearContents
Sheet2.Range("H" & i + 1).ClearContents
danwei = Application.InputBox("请输入申请单位:", "提示", "宜宾市翠屏区宗场镇中心小学校", , , , , 2)
If danwei = False Then
删除temp表
Application.ScreenUpdating = True
Exit Sub
End If
bianma = Application.InputBox("请输入单位编码:", "提示", 113757, , , , , 2)
If bianma = False Then
删除temp表
Application.ScreenUpdating = True
Exit Sub
End If
.Range("A2") = "申请单位(盖章):" & danwei & "           单位编码:" & bianma & "           " & Application.WorksheetFunction.Text(Now, "yyyy""年""m""月""d""日""") & "            金额单位:元"
Sheet2.Range("A2") = "申请单位(盖章):" & danwei & "           单位编码:" & bianma & "           " & Application.WorksheetFunction.Text(Now, "yyyy""年""m""月""d""日""") & "            金额单位:元"

y = 5
f = 5
    For Each c In Range("D3:D" & [D65536].End(xlUp).Row)
        If Left(c, Application.Search("-", c) - 1) <> "12" Then 'Left第一个起,Right最后一个起,Len文本个数
            x = c.Row
             .Cells(y, 1) = Left(Cells(x, 5), Application.Search("-", Cells(x, 5)) - 1)
             .Cells(y, 2) = Right(Cells(x, 5), Len(Cells(x, 5)) - Application.Search("-", Cells(x, 5)))
             .Cells(y, 3) = Left(Cells(x, 6), Application.Search("-", Cells(x, 6)) - 1)
             .Cells(y, 4) = Right(Cells(x, 6), Len(Cells(x, 6)) - Application.Search("-", Cells(x, 6)))
             .Cells(y, 5) = Right(Cells(x, 4), Len(Cells(x, 4)) - Application.Search("-", Cells(x, 4)))
             .Cells(y, 6) = Right(Cells(x, 11), Len(Cells(x, 11)) - Application.Search("-", Cells(x, 11)))
             .Cells(y, 7) = .Cells(y, 4)
             .Cells(y, 8) = Cells(x, 7)
            y = y + 1
            If .Cells(y, 1) = "合            计" Then
            .Cells(y, 1).EntireRow.Insert
            End If
          Else
            x = c.Row
             Sheet2.Cells(f, 1) = Left(Cells(x, 5), Application.Search("-", Cells(x, 5)) - 1)
             Sheet2.Cells(f, 2) = Right(Cells(x, 5), Len(Cells(x, 5)) - Application.Search("-", Cells(x, 5)))
             Sheet2.Cells(f, 3) = Left(Cells(x, 6), Application.Search("-", Cells(x, 6)) - 1)
             Sheet2.Cells(f, 4) = Right(Cells(x, 6), Len(Cells(x, 6)) - Application.Search("-", Cells(x, 6)))
             Sheet2.Cells(f, 5) = Right(Cells(x, 4), Len(Cells(x, 4)) - Application.Search("-", Cells(x, 4)))
             Sheet2.Cells(f, 6) = Right(Cells(x, 11), Len(Cells(x, 11)) - Application.Search("-", Cells(x, 11)))
             Sheet2.Cells(f, 7) = Sheet2.Cells(f, 4)
             Sheet2.Cells(f, 8) = Cells(x, 7)
            f = f + 1
            If Sheet2.Cells(f, 1) = "合            计" Then
            Sheet2.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) = Application.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 Sheet2.Range("A5:A" & Sheet2.[A65536].End(xlUp).Row)
        If t = "合            计" Then
            k = t.Row
            Sheet2.Cells(k, 8) = Application.Sum(Sheet2.Range("H5:H" & k - 1))
            If k > 16 Then
             Sheet2.Range("A16:A" & k).SpecialCells(4).Delete (3)
            End If
        End If
    Next t
End With
删除temp表
Application.ScreenUpdating = True
End Sub


Sub 生成temp表()
    With Application.FileDialog(msoFileDialogOpen)
        .Title = "请选择要导入的Excel文件"
        If .Show = -1 Then
          Workbooks.Open .SelectedItems(1)
         Else
          Exit Sub
        End If
    End With
   
    Name = ActiveWorkbook.Name
    Sheets(1).Select
    Sheets(1).Copy Before:=Workbooks("预算拨款申请表.xls").Sheets(1)
    For Each wb In Workbooks
        If wb.Name = Name Then
            wb.Close False
        End If
    Next
    Sheets(1).Select
    Sheets(1).Name = "temp"
End Sub

Sub 删除temp表()
    Application.DisplayAlerts = False '屏弊删除提示
      Sheets("temp").Delete
    Application.DisplayAlerts = True
    Sheet1.Select
   
End Sub
要导入的文件:

第4季度申报计划.rar

2.51 KB, 下载次数: 31

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-24 17:42 , Processed in 0.354448 second(s), 12 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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