2013-01-31 37 views
8

Tôi có sổ làm việc excel được bật macro có chứa một số trang tính được đặt tên. Một trong các bảng tính được đặt tên là "bảng điều khiển" và một trang tính thứ hai được đặt tên là "dữ liệu". Trang tính có tên "bảng điều khiển" có nút mà macro được chỉ định. Tôi muốn chọn nút trên trang tính có tên "bảng điều khiển" và có cửa sổ duyệt tệp xuất hiện. Khi người dùng chọn tệp csv trên ổ cứng của họ, tôi muốn nội dung của tệp csv được nhập vào trang tính có tên là "dữ liệu" bắt đầu trong ô A1.macro để nhập tệp csv vào một trang tính không hoạt động excel

VẤN ĐỀ 1: vba tôi đã gán cho nút khiến nội dung của tệp csv được đặt trên cùng một trang tính với nút (bảng tính "bảng"). Tôi muốn nội dung của tệp csv được đặt trên trang "dữ liệu".

VẤN ĐỀ 2: Ngoài ra, có một chuỗi mã tham chiếu đến ổ đĩa cứng của tôi và một tệp có tên là "capture.csv". Vì vậy, khi macro kích hoạt tập tin excel là trên một máy tính khác, tập tin bị treo. Bất kỳ cách nào để loại bỏ các chuỗi đường dẫn để bất kỳ máy tính có thể sử dụng các tập tin?

Bất kỳ hỗ trợ nào để khắc phục vấn đề này sẽ được đánh giá cao. Macro được gán cho nút sau:

Sub load_csv() 
Dim fStr As String 
With Application.FileDialog(msoFileDialogFilePicker) 
.Show 
If .SelectedItems.Count = 0 Then 
MsgBox "Cancel Selected" 
End 
End If 
'fStr is the file path and name of the file you selected. 
fStr = .SelectedItems(1) 
End With 
Range("A1").Select 
With ActiveSheet.QueryTables.Add(Connection:= _ 
"TEXT;C:\Users\laptop\Desktop\CAPTURE.csv", Destination:=Range("$A$1")) 
.Name = "CAPTURE" 
.FieldNames = True 
.RowNumbers = False 
.FillAdjacentFormulas = False 
.PreserveFormatting = True 
.RefreshOnFileOpen = False 
.RefreshStyle = xlInsertDeleteCells 
.SavePassword = False 
.SaveData = True 
.AdjustColumnWidth = True 
.RefreshPeriod = 0 
.TextFilePromptOnRefresh = False 
.TextFilePlatform = 437 
.TextFileStartRow = 1 
.TextFileParseType = xlDelimited 
.TextFileTextQualifier = xlTextQualifierDoubleQuote 
.TextFileConsecutiveDelimiter = False 
.TextFileTabDelimiter = True 
.TextFileSemicolonDelimiter = False 
.TextFileCommaDelimiter = True 
.TextFileSpaceDelimiter = False 
.TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) 
.TextFileTrailingMinusNumbers = True 
.Refresh BackgroundQuery:=False 
MsgBox fStr 
End With 
End Sub 

Trả lời

12

Đây có phải là những gì bạn đang cố gắng không?

Sub load_csv() 
    Dim fStr As String 

    With Application.FileDialog(msoFileDialogFilePicker) 
     .Show 
     If .SelectedItems.Count = 0 Then 
      MsgBox "Cancel Selected" 
      Exit Sub 
     End If 
     'fStr is the file path and name of the file you selected. 
     fStr = .SelectedItems(1) 
    End With 

    With ThisWorkbook.Sheets("Data").QueryTables.Add(Connection:= _ 
    "TEXT;" & fStr, Destination:=Range("$A$1")) 
     .Name = "CAPTURE" 
     .FieldNames = True 
     .RowNumbers = False 
     .FillAdjacentFormulas = False 
     .PreserveFormatting = True 
     .RefreshOnFileOpen = False 
     .RefreshStyle = xlInsertDeleteCells 
     .SavePassword = False 
     .SaveData = True 
     .AdjustColumnWidth = True 
     .RefreshPeriod = 0 
     .TextFilePromptOnRefresh = False 
     .TextFilePlatform = 437 
     .TextFileStartRow = 1 
     .TextFileParseType = xlDelimited 
     .TextFileTextQualifier = xlTextQualifierDoubleQuote 
     .TextFileConsecutiveDelimiter = False 
     .TextFileTabDelimiter = True 
     .TextFileSemicolonDelimiter = False 
     .TextFileCommaDelimiter = True 
     .TextFileSpaceDelimiter = False 
     .TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) 
     .TextFileTrailingMinusNumbers = True 
     .Refresh BackgroundQuery:=False 

    End With 
End Sub 
+0

Chính xác những gì tôi sắp nói. Điều này sẽ làm việc – Rick

+0

Cảm ơn bạn đã dành thời gian để giúp tôi. Tôi đã gán cho nút mã bạn đã viết. Khi tôi tải tệp csv, cửa sổ thông báo xuất hiện: "Lỗi thời gian chạy '-2147024809 (80070057) Phạm vi đích không nằm trên cùng một trang tính mà bảng truy vấn đang được tạo." – George

+1

Thay đổi 'Đích: = Phạm vi (" $ A $ 1 "))' thành 'Đích: = ThisWorkbook.Sheets (" Dữ liệu "). Phạm vi (" $ A $ 1 "))' –

0

Đối với Excel trên Mac, có vẻ như đối tượng QueryTable không hỗ trợ thuộc tính "PreserveFormatting" và "RefreshPeriod" và sẽ cung cấp cho bạn lỗi thời gian chạy nếu bạn thử và đặt chúng.

Ngoài ra, Application.FileDialog cũng không hoạt động với Mac, nhưng được bao gồm trong các bài đăng khác.

Đối với Mac:

Sub load_csv() 
Dim fStr As String 

fStr = "Macintosh HD:Users:anthony:Documents:example.csv" 'Keeping file String simple for example. 

With ThisWorkbook.Sheets("Data").QueryTables.Add(Connection:= _ 
"TEXT;" & fStr, Destination:=Range("$A$1")) 
    .Name = "CAPTURE" 
    .FieldNames = True 
    .RowNumbers = False 
    .FillAdjacentFormulas = False 
    '.PreserveFormatting = True **commented out for Mac 
    .RefreshOnFileOpen = False 
    .RefreshStyle = xlInsertDeleteCells 
    .SavePassword = False 
    .SaveData = True 
    .AdjustColumnWidth = True 
    '.RefreshPeriod = 0 **commented out for Mac 
    .TextFilePromptOnRefresh = False 
    .TextFilePlatform = 437 
    .TextFileStartRow = 1 
    .TextFileParseType = xlDelimited 
    .TextFileTextQualifier = xlTextQualifierDoubleQuote 
    .TextFileConsecutiveDelimiter = False 
    .TextFileTabDelimiter = True 
    .TextFileSemicolonDelimiter = False 
    .TextFileCommaDelimiter = True 
    .TextFileSpaceDelimiter = False 
    .TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) 
    .TextFileTrailingMinusNumbers = True 
    .Refresh BackgroundQuery:=False 

End With 
End Sub 
Các vấn đề liên quan