
Originally Posted by
jaryszek
.....
...
......I will know only that for example for column numner 3 (RegionID) there is formula so i have to skip this column while loading.
....
...
..
We can check if the cell has an formula and if not exists, then it can be feeded from the recordset as follows;
Sub Test3()
'Haluk - 02/07/2019
'sa4truss@gmail.com
Dim strFile As Variant
Dim myFile As String
Dim myFolder As String
Dim strSQL As String
'
Dim objConn As Object, RS As Object, FSO As Object
Dim i As Integer, j As Integer
Const adOpenKeyset = 1
Const adLockReadOnly = 1
Const adCmdText = 1
strFile = Application.GetOpenFilename("CSV files,*.csv")
If strFile = False Then Exit Sub
Set objConn = CreateObject("ADODB.Connection")
Set RS = CreateObject("ADODB.Recordset")
Set FSO = CreateObject("Scripting.FileSystemObject")
myFile = FSO.getFileName(strFile)
myFolder = FSO.GetFile(strFile).ParentFolder.Path & Application.PathSeparator
objConn.Open "Driver={Microsoft Text Driver (*.txt; *.csv)};" & _
"Dbq=" & myFolder & ";Extensions=asc,csv,tab,txt;"
strSQL = "Select [ResourceID],[RegionName],[RegionID],[Role] from [" & myFile & "]"
RS.Open strSQL, objConn, adOpenKeyset, adLockReadOnly, adCmdText
For i = 0 To RS.RecordCount - 1
If Not Cells(i + 2, 1).HasFormula Then Cells(i + 2, 1) = RS(0)
If Not Cells(i + 2, 2).HasFormula Then Cells(i + 2, 2) = RS(1)
If Not Cells(i + 2, 3).HasFormula Then Cells(i + 2, 3) = RS(2)
If Not Cells(i + 2, 4).HasFormula Then Cells(i + 2, 4) = RS(3)
Next
RS.Close
Set RS = Nothing
Set FSO = Nothing
objConn.Close
Set objConn = Nothing
End Sub
.
Bookmarks