乐筑天下

搜索
欢迎各位开发者和用户入驻本平台 尊重版权,从我做起,拒绝盗版,拒绝倒卖 签到、发布资源、邀请好友注册,可以获得银币 请注意保管好自己的密码,避免账户资金被盗
查看: 137|回复: 1

[编程交流] 从中保存为问题

[复制链接]

11

主题

46

帖子

36

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
55
发表于 2022-7-6 22:29:28 | 显示全部楼层 |阅读模式
我试图将我所做的工作另存为一个不同的文件名,我不断得到“脚本超出范围”有人能扔我一根骨头吗?
 
  1. ' Declare Working Directory
  2.    Global Const WrkDir = "C:\Documents and Settings\moorerb\Desktop\asset worksheets"
  3. ' Declare Excel Workbook name
  4.    Global Const Master_WorkBook = WrkDir & "bptags.xls"
  5.    Global Const Secondary_WorkBook = WrkDir & "mytemp.xls"
  6. ' Declare Excel Worksheet name
  7.    Global Const Master_WorkSheet = "ccu3"
  8.    Global Const Secondary_WorkSheet = "mytemp"
  9.    
  10. Global workbooks As Object
  11. Global ExcelVer As Integer
  12. Global ExcelServer As Object
  13. Global ObjWorksheet As Object
  14. 'Global SecWorksheet As Object
  15. 'Global ObjWorkbook As Object
  16. Global FileSaveName As String
  17. ' end of global variables
  18. Sub RetrieveEXC()
  19.    Set ExcelServer = CreateObject("Excel.Application.11")
  20.        Set workbooks = ExcelServer.workbooks
  21.        workbooks.Add ("C:\Documents and Settings\moorerb\Desktop\asset worksheets\mytemp.xls")
  22.        workbooks.Open (Master_WorkBook)
  23.       
  24.    Set ObjWorksheet = ExcelServer.ActiveWorkbook.worksheets(Master_WorkSheet)
  25.        'ExcelServer.WindowState = -4140
  26.        ExcelServer.Visible = True
  27.       
  28.        FileSaveName = WrkDir & Dwg_Name & ".xls"
  29.        'Set SecWorksheet = ExcelServer.ActiveWorkbook.worksheets(Secondary_WorkSheet)
  30.                    [color="red"]the line below is where my issue lyes.[/color]
  31.        workbooks(Master_WorkBook).Sheets(Secondary_WorkSheet).Move After:=workbooks(Secondary_WorkBook).Sheets(1)
  32.        workbooks(Secondary_WorkBook).SaveAs FileSaveName, fileformat:=56
  33.       
  34.        set_to_null
  35. '----------------------------------------------
  36.        ActiveWorkbook.SaveAs FileName:= _
  37.        FileSaveName, fileformat:= _
  38.        xlNormal, Password:="", WriteResPassword:="", ReadOnlyRecommended:=False _
  39.        , CreateBackup:=False
  40. '----------------------------------------------        
  41. End Sub

 
这些是我的手表
 
回复

使用道具 举报

11

主题

46

帖子

36

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
55
发表于 2022-7-6 23:50:12 | 显示全部楼层
只是为了更新,这是工作代码
 
  1. '
  2. ' work done in excel
  3. '
  4. Sub excelwork()
  5.    Dim i As Integer, n As Long, c As Long
  6.        c = 8
  7.    Dim CurrentItem As String
  8.    Dim pause As Boolean
  9.    Dim match As Variant
  10.    Dim NewSheetName As String
  11.        NewSheetName = Dwg_Name & "assets"
  12.       
  13.    'set a count for items in collection
  14.    For i = 1 To EOC
  15.    Cur_TxtSTR = text_coll(i)
  16.    MasWorksheet.Activate
  17.    MasWorksheet.cells(1, 3).Activate
  18.    
  19.        'set a count for items in excel spreadsheet
  20.        For n = 1 To 876
  21.        CurrentItem = MasWorksheet.cells(n, 3).Value
  22.            
  23.            'compare collection item to each excel item, until we find our match
  24.            If Cur_TxtSTR = CurrentItem Then
  25.                MasWorksheet.Rows(n).Select                ' select the row
  26.                MasWorksheet.Rows(n).Copy                  ' copy the row
  27.                secWorksheet.Activate
  28.                secWorksheet.cells(c, 1).Activate
  29.                secWorksheet.paste                         ' paste the row in the new sheet
  30.                secWorksheet.cells(c, 10).Value = Dwg_Name ' insert the drawing name in new sheet
  31.                c = c + 1
  32.            End If
  33.    
  34.        Next n
  35.       
  36.    Next i
  37.    
  38.    secWorksheet.Copy ' copy the new sheet
  39.    ExcelServer.activeworkbook.sheets("template").Name = NewSheetName ' set the new sheet name
  40.    FileSaveName = ExcelServer.Application.GetSaveAsFilename _
  41.    (InitialFileName:=Dwg_Name & ".xls", Title:="Save As")            ' choose where we're going to save it
  42.    
  43.    If FileSaveName = "False" Then                         ' error handleing
  44.        MsgBox "File not Saved, Actions Cancelled."
  45.        Exit Sub
  46.    Else
  47.        ExcelServer.activeworkbook.SaveAs FileSaveName ' save it
  48.        ExcelServer.activeworkbook.Close               ' close it
  49.    End If
  50.    
  51.    ExcelServer.Application.DisplayAlerts = False ' hide unwanted alerts
  52.    ExcelServer.workbooks("bptags.xls").Close     ' close the work book
  53.    ExcelServer.Quit                              ' quit excel
  54.    set_to_nil                                    ' function to reset object variables to nothing
  55.    
  56. End Sub

 
这是我的博客,可以随意访问并查看或更正
http://showyourcode.blogspot.com/
回复

使用道具 举报

发表回复

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

本版积分规则

  • 微信公众平台

  • 扫描访问手机版

  • 点击图片下载手机App

QQ|关于我们|小黑屋|乐筑天下 繁体中文

GMT+8, 2025-1-31 20:49 , Processed in 0.217855 second(s), 56 queries .

© 2020-2025 乐筑天下

联系客服 关注微信 帮助中心 下载APP 返回顶部 返回列表