ID Artikel: 272729 - Kajian Terakhir: 22 September 2011 - Revisi: 2.0

Cara mengimpor data dengan lebih dari 256 kolom atau kolom ke Excel

Tips SistemThis article applies to a different operating system than the one you are using. Article content that may not be relevant to you is disabled.
Perbesar semua | Perkecil semua

RINGKASAN

Microsoft Excel spreadsheet yang 256 kolom (kolom IV). Oleh karena itu, Excel tidak dapat menerima data dengan lebih dari 256 fields per record pada lembar kerja. Ketika Anda mencoba untuk secara manual mengimpor data dari database atau teks berkas yang memiliki lebih dari 256 fields per merekam, bidang tambahan dipotong dari catatan.

Anda dapat menggunakan Visual Basic untuk Aplikasi makro untuk mengimpor data di beberapa lembar. Artikel ini menyediakan contoh makro yang impor Comma Separated variabel (CSV) diformat database dengan bidang hingga 510 ke Excel. Makro tempat 256 pertama bidang dalam satu lembar di buku kerja dan bidang sisa (mulai dengan bidang 257th) pada selembar kedua di buku kerja.

INFORMASI LEBIH LANJUT

Microsoft menyediakan contoh pemrograman hanya sebagai ilustrasi, tanpa jaminan apa pun baik tersurat maupun tersirat. Termasuk, namun tidak terbatas pada, jaminan tersirat mengenai kelayakan untuk diperdagangkan atau kesesuaian untuk keperluan tertentu. Artikel ini mengasumsikan bahwa Anda telah terbiasa dengan bahasa pemrograman yang ditunjukkan dan dengan alat yang digunakan untuk membuat dan mendebug prosedur. Teknisi dukungan Microsoft dapat membantu menjelaskan fungsionalitas prosedur tertentu, namun mereka tidak akan memodifikasi contoh untuk memberikan fungsionalitas tambahan atau menyusun prosedur untuk memenuhi persyaratan khusus Anda. Jika database Anda dalam format lain, bukan dalam CSV, menyimpan database dalam CSV format sebelum Anda mengimpor menggunakan berikut makro.
  1. Mulai atau beralih ke Excel.
  2. Pada Alat menu, titik Makro, lalu klik Penyunting Visual Basic.
  3. Pada Masukkan menu, klik Modul.
  4. Dalam modul, ketik kode berikut:
    Sub LargeDatabaseImport()
    
        'In the event of an error, make sure the application is reset to
        'normal.
        On Error GoTo ErrorCheck
    
        'Dimension Variables
        Dim ResultStr As String
        Dim FileName As String
        Dim FileNum As Integer
        Dim Counter As Double
        Dim CommaCount As Integer
        Dim WorkResult As String
    
        'Ask for the name of the file.
        FileName = InputBox("Please type the name of your text file, for example, test.txt")
    
        'Turn off ScreenUpdating and Events so that users can't see what is 
        'happening and can't affect the code while it is running.
        Application.ScreenUpdating = False
        Application.EnableEvents = False
    
        'Check for no entry.
        If FileName = "" Then End
        
        'Get next available file handle number.
        FileNum = FreeFile()
        
        'Open text file for input.
        Open FileName For Input As #FileNum
        
        'Turn ScreenUpdating off.
        Application.ScreenUpdating = False
    
        'Set the counter to 1.
        Counter = 1
    
        'Place the data in the first row of the column.
        Range("A1").Activate
    
        'Loop until the end of file is reached.
        Do While Seek(FileNum) <= LOF(FileNum)
    
            'Show row number being imported on status bar.
            Application.StatusBar = "Importing Row " & _
                    Counter & " of text file " & FileName
    
            'Store one line of text from file to variable.
            Line Input #FileNum, ResultStr
    
            'Initialize the CommaCount variable to zero.
            CommaCount = 0
            
            'Store the entire string into a second, temporary string.
            WorkResult = ResultStr
    
            'Parse through the first line of data and separate out records 
            '257 to 510.
            While CommaCount < 255
    
                WorkResult = Right(WorkResult, Len(WorkResult) - InStr(1, WorkResult, ","))
                CommaCount = CommaCount + 1
    
            Wend
    
            'Parse out any leading spaces.
            If Left(WorkResult, 1) = " " Then WorkResult = Right(WorkResult, Len(WorkResult) - 1)
    
            'Ensure that any records that contain an "=" sign are 
            'brought in as text, and set the value of the current
            'cell to the first 256 records.
            If Left(WorkResult, 1) = "=" Then
                ActiveCell.Value = "'" & Left(ResultStr, Len(ResultStr) - Len(WorkResult))
            Else
                ActiveCell.Value = Left(ResultStr, Len(ResultStr) - Len(WorkResult))
            End If
    
            'Ensure that any records that contain an "=" sign are 
            'brought in as text,and set the value of the next cell 
            'to the last 256 records.
            If Left(WorkResult, 1) = "=" Then
                ActiveCell.Offset(0, 1).Value = "'" & WorkResult
            Else
                ActiveCell.Offset(0, 1).Value = WorkResult
            End If
    
            'Move down one cell.
            ActiveCell.Offset(1, 0).Activate
    
            'Increment the Counter by 1.
            Counter = Counter + 1
    
            'Start again at top of 'Do While' statement.
        Loop
    
        'Close the open text file.
        Close
    
        'Take records 257-510 and move them to sheet two.
        Columns("B:B").Select
        Selection.Cut
        Sheets("Sheet2").Select
        Columns("A:A").Select
        ActiveSheet.Paste
    
        'Run the text-to-columns wizard on both sheets.
        Selection.TextToColumns Destination:=Range("A1"), DataType:=xlDelimited, _
                TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, _
                Semicolon:=False, Comma:=True, Space:=False, Other:=False, FieldInfo _
                :=Array(Array(1, 1), Array(2, 1), Array(3, 1))
        Sheets("Sheet1").Select
        Columns("A:A").Select
        Selection.TextToColumns Destination:=Range("A1"), DataType:=xlDelimited, _
                TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, _
                Semicolon:=False, Comma:=True, Space:=False, Other:=False, FieldInfo _
                :=Array(Array(1, 1), Array(2, 1), Array(3, 1))
    
        'Reset the application to its normal operating environment.
        Application.StatusBar = False
        Application.EnableEvents = True
        Application.ScreenUpdating = True
    
        Exit Sub
    
    ErrorCheck:
    
        'Reset the application to its normal operating environment.
        Application.StatusBar = False
        Application.EnableEvents = True
        Application.ScreenUpdating = True
        MsgBox "An error occured in the code."
    
    End Sub
    						
  5. Pada Berkas menu, klik Menutup dan mengembalikan ke Microsoft Excel.
  6. Pada Alat menu, titik Makro, lalu klik Makro.
  7. Dalam daftar makro, pilih LargeDatabaseImport makro. Klik Menjalankan.
  8. Dalam kotak dialog yang muncul, ketik lintasan dan nama berkas file CSV yang ingin Anda impor.

REFERENSI

Untuk informasi lebih lanjut tentang cara menggunakan kode contoh dalam Artikel ini, klik nomor artikel di bawah ini untuk melihat artikel di Basis Pengetahuan Microsoft:
212536  (http://support.microsoft.com/kb/212536/EN-US/ ) OFF2000: Bagaimana untuk menjalankan kode contoh dari artikel Basis Pengetahuan
Untuk informasi tambahan tentang mendapatkan bantuan dengan Visual Basic forApplications, klik nomor artikel di bawah ini untuk melihat artikel di dalam Basis Pengetahuan Microsoft:
226118  (http://support.microsoft.com/kb/226118/EN-US/ ) OFF2000: Pemrograman sumber daya untuk Visual Basic untuk aplikasi
Untuk tambahan informasi tentang spesifikasi Excel dan limiatons, klik berikut nomor artikel untuk melihat artikel di dalam Basis Pengetahuan Microsoft:
264626  (http://support.microsoft.com/kb/264626/ ) Penjelasan mengenai Excel 2000 spesifikasi
Note This is a "FAST PUBLISH" article created directly from within the Microsoft support organization. The information contained herein is provided as-is in response to emerging issues. As a result of the speed in making it available, the materials may include typographical errors and may be revised at any time without notice. See Terms of Use (http://go.microsoft.com/fwlink/?LinkId=151500) for other considerations.

Berlaku bagi:
  • Microsoft Excel 2002 Standard Edition
  • Microsoft Excel 2000 Standard Edition
Kata kunci: 
kbvba kbprogramming kbautomation kbmacro kbhowto kbmt KB272729 KbMtid
Penerjemahan MesinPenerjemahan Mesin
PENTING: Artikel ini diterjemahkan menggunakan perangkat lunak mesin penerjemah Microsoft dan bukan oleh seorang penerjemah. Microsoft menawarkan artikel yang diterjemahkan oleh seorang penerjemah maupun artikel yang diterjemahkan menggunakan mesin sehingga Anda akan memiliki akses ke seluruh artikel baru yang diterbitkan di Pangkalan Pengetahuan (Knowledge Base) dalam bahasa yang Anda gunakan. Namun, artikel yang diterjemahkan menggunakan mesin tidak selalu sempurna. Artikel tersebut mungkin memiliki kesalahan kosa kata, sintaksis, atau tata bahasa, hampir sama seperti orang asing yang berbicara dalam bahasa Anda. Microsoft tidak bertanggung jawab terhadap akurasi, kesalahan atau kerusakan yang disebabkan karena kesalahan penerjemahan konten atau penggunaannya oleh para pelanggan. Microsoft juga sering memperbarui perangkat lunak mesin penerjemah.
Klik disini untuk melihat versi Inggris dari artikel ini:272729  (http://support.microsoft.com/kb/272729/en-us/ )