[英]VBA; exporting folder with CSV files into one excel file
我有一个包含.csv文件的文件夹,但我正在尝试将文件中的所有数据导入到一个Excel工作表中。 另外,我试图水平组织我的数据,它只会复制一次变量,然后再复制数据。
Sub ImportCSVsWithReferenceI()
'UpdatebyKutoolsforExcel20151214
'Dim xSht As Worksheet
Dim xSht As Workbook
Dim xWb As Workbook
Dim xStrPath As String
Dim xFileDialog As FileDialog
Dim xFile As String
Dim xCount As Long
On Error GoTo ErrHandler
With Application
.Calculation = xlCalculationManual
.EnableEvents = False
.ScreenUpdating = False
End With
Set xFileDialog = Application.FileDialog(msoFileDialogFolderPicker)
xFileDialog.AllowMultiSelect = False
xFileDialog.Title = "Select a folder [Kutools for Excel]"
If xFileDialog.Show = -1 Then
xStrPath = xFileDialog.SelectedItems(1)
End If
If xStrPath = "" Then Exit Sub
Set xSht = ThisWorkbook.ActiveSheet
If MsgBox("Clear the existing sheet before importing?", vbYesNo, "Kutools for Excel") = vbYes Then
xSht.UsedRange.Clear
xCount = 1
Else
xCount = xSht.Cells(3, Columns.Count).End(xlToLeft).Column + 1
End If
'Application.ScreenUpdating = False
xFile = Dir(xStrPath & "\" & "*.csv")
Do While xFile <> "" And xSht.Name
Set xWb = Workbooks.Open(xStrPath & "\" & xFile)
xWb.Sheets("Sheet1").Columns(2).Copy xSht.Sheets("").Cells(1, Columns.Count).End(xlToLeft).Offset(, 1)
'Rows(1).Insert xlShiftDown
'Range("B1") = ActiveSheet.Name
'ActiveSheet.UsedRange.Copy xSht.Cells(1, Columns.Count).End(xlToLeft).Offset(0, 1)
'ActiveSheet.Columns(2).Copy xSht.Cells(1, Columns.Count).End(xlToLeft).Offset(0, 1)
xWb.Close False
xFile = Dir
'xCount = xSht.Cells(3, Columns.Count).End(xlToLeft).Column + 1
Loop
Application.ScreenUpdating = True
Exit Sub
ErrHandler:
MsgBox "no files csv", , "Kutools for Excel"
End Sub
代码已修改
xSht
声明为工作表 Sheets(1)
或ActiveSheet` Sheets("")
吗? 已删除。 直接使用声明的xSht
And xSht.Name
并删除了它。 发现工作到目前为止了解。
Sub ImportCSVsWithReferenceI()
'UpdatebyKutoolsforExcel20151214
Dim xSht As Worksheet
'Dim xSht As Workbook
Dim xWb As Workbook
Dim xStrPath As String
Dim xFileDialog As FileDialog
Dim xFile As String
Dim xCount As Long
Dim xOffset As Integer
' On Error GoTo ErrHandler
With Application
' .Calculation = xlCalculationManual
' .EnableEvents = False
' .ScreenUpdating = False
End With
Set xFileDialog = Application.FileDialog(msoFileDialogFolderPicker)
xFileDialog.AllowMultiSelect = False
xFileDialog.Title = "Select a folder [Kutools for Excel]"
If xFileDialog.Show = -1 Then
xStrPath = xFileDialog.SelectedItems(1)
End If
If xStrPath = "" Then Exit Sub
Set xSht = ThisWorkbook.ActiveSheet
If MsgBox("Clear the existing sheet before importing?", vbYesNo, "Kutools for Excel") = vbYes Then
xSht.UsedRange.Clear
End If
'Application.ScreenUpdating = False
xFile = Dir(xStrPath & "\" & "*.csv")
Do While xFile <> ""
Set xWb = Workbooks.Open(xStrPath & "\" & xFile)
xOffset = IIf(IsEmpty(xSht.Cells(1, Columns.Count).End(xlToLeft)), 0, 1)
xWb.Sheets(1).Columns(2).Copy xSht.Cells(1, Columns.Count).End(xlToLeft).Offset(, xOffset)
'Rows(1).Insert xlShiftDown
'Range("B1") = ActiveSheet.Name
'ActiveSheet.UsedRange.Copy xSht.Cells(1, Columns.Count).End(xlToLeft).Offset(0, 1)
'ActiveSheet.Columns(2).Copy xSht.Cells(1, Columns.Count).End(xlToLeft).Offset(0, 1)
xWb.Close False
xFile = Dir
Loop
Application.ScreenUpdating = True
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.