Excel VBA 将工作表保存到具有唯一名称的多个文件夹

2024-05-05

感谢您的所有意见。下面的代码是收到的输入的最终结果。我已经对这些错误进行了评论,这些错误直接关系到保存到数组中定义的文件夹中的总体预期结果。

Option Explicit
Public EngName As String, TeamNum As Variant
Public x As Integer
Option Base 1

'### From David Zemens ###
Function secfol(i As Long)
secfol = Array("", _
"Section 1 Jobs Released Last Week (excludes NRT Jobs)", _
"Section 2 Jobs Created Last Week (excludes NRT Jobs)", _
"Section 3 Late Jobs", _
"Section 4 Unnegotiated Jobs", _
"Section 5 Jobs To Go (Excludes NRT Jobs)", _
"Section 6 Jobs To Go (NRT Jobs)")(i)
End Function


Sub ADMS_Processing()

Application.ScreenUpdating = False

'Opens files and copies worksheets to one workbook and names each worksheet
Dim strFilePath As String
Dim Name As String

Workbooks.Open Filename:= _
"\\MARNV006\BM\Master Scheduling\DSC 2.3.4 Engineering Job Release Metrics\EDW Crystal Reports (Automation)\ePortfolio1.xls"
Sheets(1).Name = "Section 1"

'=======================================================================
' Save file to "Schedule Update Requests" folder & Closes Excel
'=======================================================================

Name = "\\MARNV006\BM\Master Scheduling\DSC 2.3.4 Engineering Job Release Metrics\"
Name = Name & "EDW Crystal Reports (Automation)\Test files\ADMS Combined File"
Name = Name & Format(Date, "_mm-d-yy") & ".xls"

'Deletes file if it already exists
On Error Resume Next
Kill (Name)

ActiveWorkbook.SaveAs Filename:=Name, FileFormat:=xlNormal, Password:="", WriteResPassword:="", _
ReadOnlyRecommended:=False, CreateBackup:=False
Name = "ADMS Combined File" & Format(Date, "_mm-d-yy") & ".xls"

'This gets the downloaded reports "ePortfolio" 1-6 and Saves indivdiual files for each Section, Section 1-6, which are the Sheets of the combined file
'###The Sections (Sheets) are not currently being saved as individual files. There should be 7 files; one for each sheet and a combined file.

'Opens moves the worksheet and closes files for sections 2 through 6
For x = 2 To 6
strFilePath = "\\MARNV006\BM\Master Scheduling\DSC 2.3.4 Engineering Job Release Metrics\"
strFilePath = strFilePath & "EDW Crystal Reports (Automation)\ePortfolio"
strFilePath = strFilePath & x & ".xls"
Workbooks.Open Filename:=strFilePath
Sheets(1).Copy After:=Workbooks(Name).Sheets(x - 1)
ActiveSheet.Name = "Section " & x
Workbooks(Right(strFilePath, 15)).Close SaveChanges:=False

Next x


'###The Combined file is being saved correctly, but the individual sheet files are not currently saving
Next x

Call ScrubSheets
Call SaveWS_to_file
End Sub

保存文件

Sub SaveWS_to_file()

Dim i As Long, Name1 As String, Name2 As String, Name3 As String, fName As String, DateString As String, _
sec1fol As String, sec2fol As String, sec3fol As String, sec4fol As String, sec5fol As String, sec6fol As String

For i = 1 To 6

 ' ### OTHER STUFF IN YOUR CODE... from David Zemens
Name1 = "\\MARNV006\BM\Master Scheduling\DSC 2.3.4 Engineering Job Release Metrics\"
Name1 = Name1 & "EDW Crystal Reports (Automation)\Test files\Section "
Name1 = Name1 & i & ".xls"
Sheets("Section " & x).Copy
ChDir "\\MARNV006\BM\Master Scheduling\DSC 2.3.4 Engineering Job Release Metrics\EDW Crystal Reports (Automation)\Test files"

'### These are only being saved for the first Sheet, Section 1

Name2 = "\\insitefs\www\htdocs\c130\comm\metrics\blue\deck_reports\"
Name2 = Name2 & "Section" & i
Name2 = Name2 & ".xls"
Sheets("Section " & i).Copy
ChDir "\\insitefs\www\htdocs\c130\comm\metrics\blue\deck_reports\"

 '### This file is currently only being saved in the folder path below as DateString ###
 fName = "\\marnv006\Bm\Master Scheduling\DSC 2.3.4 Engineering Job Release Metrics\Blue Deck\Blue Deck "
 '### Added backslash for testing to correct file path ###
fName = fName & Year(Date) & "\"
 '### This should be like \\marnv006\#marnv006\Bm\Master Scheduling\DSC 2.3.4 Engineering Job Release Metrics\Blue Deck\Blue Deck 2016\

'Then the array function to get the folder gets the destination folder
'The file path for the first sheet would be like:
'"\\marnv006\#marnv006\Bm\Master Scheduling\DSC 2.3.4 Engineering Job Release Metrics\Blue Deck\Blue Deck 2016\_
'Section 1 Jobs Released Last Week (excludes NRT Jobs)\Section 1_12_19_2016.xls"

 DateString = Format(Now, "mm_dd_yyyy")

'Deletes file if it already exists
 On Error Resume Next
 Kill (Name1)
 Kill (Name2)

  'from David Zemens
' ### Save the sheet at this loop iteration:
   With Sheets("Section " & i)

'Should save each sheet as separate file in corresponding folder from the array function

'### Nothing is currently being saved here 

 .SaveAs Filename:=fName & "\" & secfol(i) & "_" & DateString, _
       FileFormat:=.Parent.FileFormat, _
       Password:="", WriteResPassword:="", _
       ReadOnlyRecommended:=False, CreateBackup:=False

 'Save file in first location
  ActiveWorkbook.SaveAs Filename:=Name1, _
    FileFormat:=xlNormal, Password:="", WriteResPassword:="", _
    ReadOnlyRecommended:=False, CreateBackup:=False

  'Save file in second location
  ActiveWorkbook.SaveAs Filename:=Name2, _
    FileFormat:=xlNormal, Password:="", WriteResPassword:="", _
    ReadOnlyRecommended:=False, CreateBackup:=False

   End With

  Next i

 End Sub


Sub ScrubSheets()

Dim lastRow As Long
Dim myRow As Long
Dim US As String
US = "UTILITIES & SUBSYSTEMS"



'Find last row in column A
lastRow = Cells(Rows.Count, "A").End(xlUp).Row

'Loop for all cells in column A from rows 2 to last row
 For myRow = 2 To lastRow
'First check value of column G
    If Cells(myRow, "G") = "PROPULSION" Then
        Cells(myRow, "G") = US
    Else
'Then check column H
        If Cells(myRow, "H") = "Q3S2531" Then
            Cells(myRow, "G") = "FUNCTIONAL TEST"
        Else
' Check four character prefixes
            Select Case Left(Cells(myRow, "A"), 4)
                Case "32EB", "35EB", "32EF", "35EF"
                    Cells(myRow, "G") = "AVIONICS"
                Case Else
'Check 3 character prefixes
                    Select Case Left(Cells(myRow, "A"), 3)
                        Case "35W"
                            Cells(myRow, "G") = "WIRING"
                        Case "34S"
                            Cells(myRow, "G") = "SOFTWARE"

                        Case Else
'Check 2 character prefixes
                            Select Case Left(Cells(myRow, "A"), 2)
                                Case "10", "11", "12", "13", "14", "15"
                                    Cells(myRow, "G") = "AIRFRAME"
                                Case "21", "23"
                                    Cells(myRow, "G") = US '"UTLITLIES & SUBSYSTEMS"
                                Case "24", "25"
                                    Cells(myRow, "G") = US '"UTLITLIES & SUBSYSTEMS"
                            End Select
                    End Select
            End Select
        End If
    End If
Next myRow
Application.ScreenUpdating = True

End Sub


不确定我完全理解你想要实现的目标,但要在里面编写代码With循环工作,这里有一个提示。

您可以首先在数组中初始化文件夹名称,如下所示:

 secfol = Array("", _
      "Section 1 Jobs Released Last Week (excludes NRT Jobs)", _
      "Section 2 Jobs Created Last Week (excludes NRT Jobs)", _
      "Section 3 Late Jobs", _
      "Section 4 Unnegotiated Jobs", _
      "Section 5 Jobs To Go (Excludes NRT Jobs)", _
      "Section 6 Jobs To Go (NRT Jobs)")

然后引用相应的文件夹名称为secfol(x), 如下:

 For i = 1 to 6
       Sheets("Section " & x).copy
       ActiveWorkbook.SaveAs Filename:=fName & secfol(x) & "_" & DateString & ".xls", _
           FileFormat:=xlNormal, Password:="", WriteResPassword:="", _
           ReadOnlyRecommended:=False, CreateBackup:=False
 Next i
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Excel VBA 将工作表保存到具有唯一名称的多个文件夹 的相关文章

  • 选择在 Excel 宏(VBA 中的范围对象)中具有值的列

    如何修改 VBA 中的这一行以仅选择具有值的列 Set rng Range A1 Range A65536 End xlUp SpecialCells xlCellTypeVisible 我不认为我做的事情是正确的CountLarge财产是
  • 如何在不滚动的情况下截取整个电子邮件正文?

    我正在使用 OL2010 想要制作整个电子邮件的屏幕截图 不仅仅是 屏幕 可以用VBA或者外部程序来完成吗 有一个类似的问题 https stackoverflow com questions 4176340关于如何使用 C 实现这一点 注
  • Excel VBA 过滤和复制粘贴数据

    给定一个数据集 假设有 10 列 在 A 列中我有日期 在 B 列中我有 我想仅过滤 A 列 2014 年的数据 B 列 ActiveSheet Range A 1 AR 1617 AutoFilter Field 5 Operator x
  • 如何在字符串vba中包含引号

    我想存储以下文本 Test1 Monday Test Abcdef 全部在字符串中包含引号 我知道要在字符串中包含引号 我必须包含 之前 但在这里这不是一个很好的解决方案 因为我在文本中有太多这样的解决方案 知道如何一次完成这一切吗 您有两
  • 字典、集合和数组的比较

    我正在尝试找出字典与集合和数组相比的相对优点和功能 我发现了一篇很棒的文章here http www experts exchange com articles 3391 Using the Dictionary Class in VBA
  • Confluence:使用 VBA 更新现有页面

    我尝试使用 VBA 更新 Confluence 页面 我的想法是使用REST API加载页面内容 修改内容然后上传修改后的版本 这是我的代码 Private Sub TestRESTApi Dim uname As String uname
  • 无法将 Excel 值的类型“double”转换为“string”

    我正在加载 Excel 文件 如网络上许多地方所示 OpenFileDialog chooseFile new OpenFileDialog chooseFile Filter Excel files xls xlsl xls xlsx i
  • 使用 pythoncom 在 Python 进程之间编组 COM 对象

    我希望有人可以帮助我从 Python 进行编组跨进程调用到 Excel 我有一个通过 Python 启动的 Excel 会话 我知道当需要从单独的 Python 进程访问它时 该会话将会启动并运行 我已经使用编组让一切按预期工作CoMars
  • 复制一张工作表上的静态范围,然后根据单元格中的单个值粘贴到另一张工作表中的动态范围

    我对这个问题分为三个部分 我在 Sheet1 A1 中有一个带有周数的单元格 我在 Sheet1 B1 F1 中有一个需要复制的静态范围 然后 我需要将该值粘贴到 Sheet2 中的动态范围中 偏移量为行的周数 这是我正在为我经常使用的工作
  • Outlook 无法识别一个或多个姓名

    我有以下 vba 代码 它读取邮箱并向任何发送无效代码作为邮箱回复的用户发送回复 但有时会收到运行时错误 Outlook 无法识别一个或多个名称 我的问题是 创建新的 MAPI 配置文件是否可以解决该问题 或者我是否需要添加一个代码来解析地
  • EPPlus Excel 行高不一致

    我已经使用 EPPlus 生成了一个 excel 文件 在 MS Office 2007 中一切似乎都很完美 但客户端使用的是 MS Office 2010 2013 并且在第 29 行之后未设置行高 这是一个非常奇怪的问题 我已经尝试了
  • 根据单元格值向用户窗体添加复选框

    我对 VBA 很陌生 只有 3 天 但我发现它非常有用且易于使用 但现在我面临一个问题 我需要制作一个具有不同复选框的用户窗体 但我需要根据工作表某一列中使用的信息自动添加它们 我相信我可以使用 For Each Next 但我真的不知道如
  • 如何使用VBA根据条件删除Excel中的行?

    我目前正在构建一个宏来格式化数据表并删除不适用的数据行 具体来说 我希望删除列 L ABC 的行以及删除列 AA DEF 的行 到目前为止 我已经实现了第一个目标 但还没有实现第二个目标 现有代码是 Dim LastRow As Integ
  • 在组合框中显示可见工作表

    您好 我有以下代码来在组合框中显示工作表 创建工作表后 工作表会自动添加到列表中 我不希望隐藏的工作表在保管箱中可见 我怎么做 Option Explicit Private Sub ComboBox1 Change If ComboBox
  • 将html表格保存到excel中[关闭]

    很难说出这里问的是什么 这个问题是含糊的 模糊的 不完整的 过于宽泛的或修辞性的 无法以目前的形式得到合理的回答 如需帮助澄清此问题以便重新打开 访问帮助中心 help reopen questions 我必须编写一个程序 定期读取网页并将
  • 在 Node.js 中解析 Json(带有数组和对象)并将数据导出到 Excel 文件中

    我是 Node js 新手 我的要求是 我需要解析 JSON 并将数据导出到 Excel 文件中 其中包含 JSON 中的所有字段 我的 JSON 如下 id 1255 title The Brain and Nervous System
  • 如何将 MySQL 查询输出保存到 Excel 或 .txt 文件? [复制]

    这个问题在这里已经有答案了 如何将 MySQL 查询的输出保存到 MS Excel 工作表 即使只能将数据存储在 txt文件 就可以了 From 将 MySQL 查询结果保存到文本或 CSV 文件中 http www tech recipe
  • SpreadsheetML 文件扩展名被 IE 和 FF 更改 - 内容类型错误?

    我正在 PHP 中生成 SpreadsheetML 文件 当用户下载文件并保存时 默认情况下文件会另存为 Report xml 并在 Excel 中打开 但是 如果选择在 Excel 中打开文件而不是保存文件 则文件名将更改为 Report
  • Excel VBA - 添加自定义数字格式

    我有一个在 Excel 外部生成的文件 其中包含许多百分比 所有这些百分比都有一位小数 当导入到 Excel 中时 Excel 会在百分比中添加第二位小数 这似乎是 Excel 中百分比的某种默认格式 它只是添加了一个 0 我想将所有两位小
  • 如何在 VBA 中声明接受 XlfOper (LPXLOPER) 类型参数的函数?

    我在之前的回答里发现了问题 https stackoverflow com q 19325258 159684一种无需注册即可调用 C xll 中定义的函数的方法 我之前使用 XLW 提供的注册基础结构 并且使用 XlfOper 类型在 V

随机推荐