改变 “{”com.microsoft.Excel.xls”“}到 “{”“com.microsoft.excel.xls”“public.逗号分隔值文本”“}” '例如,如果要筛选xls和csv文件。 MyScript=_ 将applescript的文本
改变 “{”com.microsoft.Excel.xls”“}到 “{”“com.microsoft.excel.xls”“public.逗号分隔值文本”“}” '例如,如果要筛选xls和csv文件。 MyScript=_ 将applescript的文本项分隔符设置为“”。&vbNewLine&_ “将文件设置为(选择文件类型”&”_ {“com.microsoft.Excel.xls”“org.openxmlformats.spreadsheetml.sheet”“}”和_ 提示“请选择一个或多个文件”“默认位置别名”“&”_ MyPath&“允许多选为true)作为字符串(&vbNewLine&_ 将applescript的文本项分隔符设置为“”&vbNewLine&_ “返回文件” MyFiles=MacScript(MyScript) 错误转到0 MySplit=False'假定没有文件=取消 如果我的文件是“”,那么 应用 .ScreenUpdate=False .EnableEvents=False 以 MySplit=Split(MyFiles,“,”) 应用 .ScreenUpdate=True .EnableEvents=True 以 如果结束 选择\u File\u或\u Files\u Mac=MySplit 端函数 函数bIsBookOpen(ByRef szBookName作为字符串)作为布尔值 ”罗伯·博维撰稿 出错时继续下一步 bIsBookOpen=Not(Application.Workbooks(szBookName)为Nothing) 端函数改变 “{”com.microsoft.Excel.xls”“}到 “{”“com.microsoft.excel.xls”“public.逗号分隔值文本”“}” '例如,如果要筛选xls和csv文件。 MyScript=_ 将applescript的文本,excel,vba,Excel,Vba,改变 “{”com.microsoft.Excel.xls”“}到 “{”“com.microsoft.excel.xls”“public.逗号分隔值文本”“}” '例如,如果要筛选xls和csv文件。 MyScript=_ 将applescript的文本项分隔符设置为“”。&vbNewLine&_ “将文件设置为(选择文件类型”&”_ {“com.microsoft.Excel.xls”“org.openxmlformats.spreadsheetml.sheet”“}”和_ 提示“请选择一个
有很多代码需要查看-您的代码在哪里不起作用?全部还是部分?嗨,布鲁斯韦恩。代码似乎在工作,我不再得到任何错误消息,它似乎运行,但没有数据通过输出表。该过程仅以打开两个原始工作表而结束,没有新的合并输出工作表。我怀疑问题在于复制和粘贴所选列的循环过程。到目前为止,我创建的是一个基于两种不同方法组合而成的代码,可以在这里找到:需要查看的代码太多了-您的代码哪里不起作用?全部还是部分?嗨,布鲁斯韦恩。代码似乎在工作,我不再得到任何错误消息,它似乎运行,但没有数据通过输出表。该过程仅以打开两个原始工作表而结束,没有新的合并输出工作表。我怀疑问题在于复制和粘贴选定列的循环过程。到目前为止,我创建的是一个基于两种不同方法组合的代码,可以在此处找到:
Private Sub AddFilesButton_Click()
Dim arrFiles As Variant
On Error GoTo ErrMsg
'Let the user choose the files they want to merge
#If Mac Then
arrFiles = Select_File_Or_Files_Mac()
#Else
arrFiles = Application.GetOpenFilename("Excel Files (*.xls; *.xlsx), *.xls;*.xlsx", 1, "Choose Excel Files", "Select", True)
#End If
If IsNull(arrFiles) Or UBound(arrFiles) = -1 Then
MsgBox "Please choose at least one Excel file"
Else
For Each file In arrFiles
FilesListBox.AddItem file
Next file
MergeButton.Enabled = True
End If
ErrMsg:
If Err.Number <> 0 Then
MsgBox "There was an error. Please try again. [" & Err.Description & "]"
End If
End Sub
Private Sub CancelButton_Click()
Unload Me
End Sub
Private Sub MergeButton_Click()
Dim fileName As Variant
Dim wb As Workbook
Dim s As Sheet1
Dim thisSheet As Sheet1
Dim lastUsedRow As Range
Dim columnMap As Collection
Dim filePath As Variant
Dim dataRange As Range
Dim insertAtRowNum As Integer
Dim outColName As String
Dim colName As String
Dim fromRange As String
Dim fromRangeToCopy As Range
Dim toRange As String
On Error GoTo ErrMsg
Application.ScreenUpdating = False
Set thisSheet = ThisWorkbook.ActiveSheet
For i = 0 To FilesListBox.ListCount - 1
fileName = FilesListBox.List(i, 0)
'Get the map of columns for this file
Set columnMap = MapColumns(fileName)
'Open the spreadsheet in ReadOnly mode
Set wb = Application.Workbooks.Open(fileName, ReadOnly:=True)
For Each sourceSheet In wb.Sheets
'Get the used range (i.e. cells with data) from the opened spreadsheet
If firstRowHeaders And i > 0 Then 'Only include headers from the first spreadsheet
Dim mr As Integer
mr = wb.ActiveSheet.UsedRange.Rows.Count
Set dataRange = wb.ActiveSheet.UsedRange.Offset(1, 0).Resize(mr - 1)
Else
Set dataRange = wb.ActiveSheet.UsedRange
End If
For Each col In dataRange.Columns
'Get corresponding output column. Empty string means no mapping
colName = GetColName(col.Column)
outColName = GetOutputColumn(columnMap, colName)
If outColName <> "" Then
fromRange = colName & 1 & ":" & colName & dataRange.Rows.Count
Set fromRangeToCopy = dataRange.Range(fromRange)
fromRangeToCopy.Copy
toRange = outColName & insertAtRowNum & ":" & outColName & (insertAtRowNum + fromRangeToCopy.Rows.Count - 1)
thisSheet.Range(toRange).PasteSpecial
End If
Next col
insertAtRowNum = insertAtRowNum + dataRange.Rows.Count
Next sourceSheet
Application.CutCopyMode = False
Next i
ThisWorkbook.Save
Set wb = Nothing
#If Mac Then
'Do nothing. Closing workbooks fails on Mac for some reason
#Else
'Close the workbooks except this one
Dim file As String
For i = 0 To FilesListBox.ListCount - 1
file = FilesListBox.List(i, 0)
file = Right(file, Len(file) - InStrRev(file, Application.PathSeparator, , 1))
Workbooks(file).Close SaveChanges:=False
Next i
#End If
Application.ScreenUpdating = True
Unload Me
ErrMsg:
If Err.Number <> 0 Then
MsgBox "There was an error. Please try again. [" & Err.Description & "]"
End If
End Sub
Function MapColumns(fileName As Variant) As Object
Dim colMap As New Collection
Select Case fileName
Case "ExcelFile1.xlsx"
colMap.Add Key:="C", Item:="A"
colMap.Add Key:="D", Item:="B"
colMap.Add Key:="E", Item:="C"
colMap.Add Key:="I", Item:="D"
Case "ExcelFile2.xlsx"
colMap.Add Key:="B", Item:="F"
colMap.Add Key:="J", Item:="G"
colMap.Add Key:="H", Item:="H"
colMap.Add Key:="C", Item:="I"
End Select
Set MapColumns = colMap
End Function
Function GetOutputColumn(columnMap As Collection, col As String) As String
Dim outCol As String
outCol = ""
If columnMap.Count > 0 Then
outCol = columnMap.Item(col)
End If
GetOutputColumn = outCol
End Function
'From: http://www.mrexcel.com/forum/excel-questions/16444-getting-column-name-given-column-number.html
Function GetColName(ColumnNumber)
FuncRange = Cells(1, ColumnNumber).AddressLocal(False, False) 'Creates Range (defaults Row to 1) and retuns Range in xlA1 format
FuncColLength = Len(FuncRange) 'finds length of range reference
GetColName = Left(FuncRange, FuncColLength - 1) 'row always "1" therefore take 1 away from string length and you are left with column ref
End Function
'From: http://msdn.microsoft.com/en-us/library/office/hh710200%28v=office.14%29.aspx#odc_xl4_ta_ProgrammaticallySelectFileforMac_DifferencesWindowsandMac
Function Select_File_Or_Files_Mac() As Variant
Dim MyPath As String
Dim MyScript As String
Dim MyFiles As String
Dim MySplit As Variant
Dim N As Long
Dim Fname As String
Dim mybook As Workbook
On Error Resume Next
MyPath = MacScript("return (path to documents folder) as String")
'Or use MyPath = "Macintosh HD:Users:Ron:Desktop:TestFolder:"
' In the following statement, change true to false in the line "multiple
' selections allowed true" if you do not want to be able to select more
' than one file. Additionally, if you want to filter for multiple files, change
' {""com.microsoft.Excel.xls""} to
' {""com.microsoft.excel.xls"",""public.comma-separated-values-text""}
' if you want to filter on xls and csv files, for example.
MyScript = _
"set applescript's text item delimiters to "","" " & vbNewLine & _
"set theFiles to (choose file of type " & _
" {""com.microsoft.Excel.xls"",""org.openxmlformats.spreadsheetml.sheet""} " & _
"with prompt ""Please select a file or files"" default location alias """ & _
MyPath & """ multiple selections allowed true) as string" & vbNewLine & _
"set applescript's text item delimiters to """" " & vbNewLine & _
"return theFiles"
MyFiles = MacScript(MyScript)
On Error GoTo 0
MySplit = False 'Assume no files = cancel
If MyFiles <> "" Then
With Application
.ScreenUpdating = False
.EnableEvents = False
End With
MySplit = Split(MyFiles, ",")
With Application
.ScreenUpdating = True
.EnableEvents = True
End With
End If
Select_File_Or_Files_Mac = MySplit
End Function
Function bIsBookOpen(ByRef szBookName As String) As Boolean
' Contributed by Rob Bovey
On Error Resume Next
bIsBookOpen = Not (Application.Workbooks(szBookName) Is Nothing)
End Function