2017-10-15 4 views
1

私は2つのExcelワークブックを持っており、下のマクロを使用して1つ(WB1)から別のもの(WB1)にデータ(A1:A20)を取り込んでいます。私は、文字列レコードがない間に数字のレコードだけが引かれるという問題があります。フィールドタイプは数字とみなされ、数値だけが引かれているようです。それを解決するためにコード内で何を変更する必要がありますか?リンクの下のレコードセット:文字列レコードのための空白?

は、ソースファイルが含まれています https://drive.google.com/open?id=0B64seB8-qtdLYk80N3hvX2F6VGc

Private Source As Variant 

Sub Copy_Paste() 
'copy the data from the source 
Source = ThisWorkbook.Path & "\WB1.xlsx" 
GetData Source, "Sheet1", "A1:A20", Sheets("Database").Range("A1") 
End Sub 

Public Sub GetData(Source As Variant, SourceSheet As String, SourceRange As String, TargetRange As Range) 
Dim rsCon As Object 
Dim rsData As Object 
Dim szSQL As String 
Dim szConnect As String 
'Create the connection string based on excel version 
    If Val(Application.Version) < 12 Then 
     szConnect = "Provider=Microsoft.Jet.OLEDB.4.0;" & _ 
        "Data Source=" & Source & ";" & _ 
        "Extended Properties=""Excel 8.0;HDR=No"";" 
    Else 
     szConnect = "Provider=Microsoft.ACE.OLEDB.12.0;" & _ 
        "Data Source=" & Source & ";" & _ 
        "Extended Properties=""Excel 12.0;HDR=No"";" 
    End If 
szSQL = "SELECT * FROM [" & SourceSheet$ & "$" & SourceRange$ & "];" 

On Error GoTo SomethingWrong 
Set rsCon = CreateObject("ADODB.Connection") 
Set rsData = CreateObject("ADODB.Recordset") 
rsCon.Open szConnect 
rsData.Open szSQL, rsCon, 0, 1, 1 
' Check to make sure we received data and copy the data 
If Not rsData.EOF Then 
    TargetRange.Cells(1, 1).CopyFromRecordset rsData 
Else 
    MsgBox "No records returned from : " & Source, vbCritical 
End If 
' Clean up our Recordset object. 
rsData.Close 
Set rsData = Nothing 
rsCon.Close 
Set rsCon = Nothing 
Exit Sub 
SomethingWrong: 
    MsgBox "The file name, Sheet name is invalid of : " & Source, vbExclamation, "Error" 
On Error GoTo 0 
End Sub 

答えて

2

ここを参照してください:あなたは、あなたの接続文字列にIMEX = 1を追加する必要がhttps://social.msdn.microsoft.com/Forums/sqlserver/en-US/ce095b10-84a4-4ae3-8944-70a2b53daa44/mixed-data-types-in-excel-column-to-oedb-destination?forum=sqlintegrationservices

。例:ドライバは、データ列が数値である(最初の数行に基づいて)と推測し、数値以外の値は無視します。

+0

多くのありがとうTim! –

関連する問題