I have this code to import a .txt file to my Excel sheet (Sheet1). In Sheet1 I have the first column with a formula, so I import my text data into B1.
This code does it well the first time, however the following ones adds a column to the right, moving the selected cells in the formula of column A. The data is copied in B1 still, but somehow adding a column first. Any help?
Sheets("Sheet1").Select
Columns("B:F").Select
Selection.ClearContents
Dim Ret
Ret = Application.GetOpenFilename("Text Files (*.txt), *.txt", Title:="Select text file(.txt)")
If Ret <> False Then
With ActiveSheet.QueryTables.Add(Connection:= _
"TEXT;" & Ret, Destination:=Range("$B$1"))
.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, 2)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
End If
Change
.RefreshStyle = xlInsertDeleteCells to .RefreshStyle = xlOverWriteCells, then run the macro just once. After that you can simply have Excel do the work by hitting the refresh data button on the data tab and get rid of the macro.
See: http://jkp-ads.com/articles/importtext.asp
Related
I have an issue where i want to import value from a textfile into my excel-document which it does but when i run macro over and over to refresh said values with new ones it just moves it over and adds more, it needs to delete values and replace them, i didnt write this code myself so i cant understand the issue with it so here i am pleading for help
Sub ImportSaldo()
Call Shell("C:\import\GetFromFTP.bat", vbNormalFocus)
Dim fileName As String, folder As String
folder = "c:\import\"
fileName = "Saldot.txt"
ActiveCell.Offset(0, 0).Range("A1").Select
With ActiveSheet.QueryTables _
.Add(Connection:="TEXT;" & folder & fileName, Destination:=ActiveCell)
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 850
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = False
.TextFileSemicolonDelimiter = True
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(1, 1, 1, 1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
End Sub
.RefreshOnFileOpen = False change this to True
Once the file is opened it should clear the existing values in the ActiveSheet.
I have a macro that I use to import data on a daily basis. If yesterday's import ended at row 100 then today's import will start at row 101.
Column B has unique values and I don't want any duplicates in my data. How can I select only the imported file and remove duplicates in that file?
Sub Import_Data()
Dim Ret
Ret = Application.GetOpenFilename("Text Files (*.txt), *.txt")
If Ret <> False Then
With ActiveSheet.QueryTables.Add(Connection:= _
"TEXT;" & Ret, Destination:=Application.InputBox(prompt:="Select Input Cell", Type:=8))
.Name = "Sample"
.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 = False
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = True
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(2, 2, 1, 2, 2, 1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
End If
End Sub
I am trying to import a text file automatically into Excel. It seems to import perfectly with the code as follows, however if I want it to import it into a table, it does not do so and results in the table moving to the right.
path = CreateObject("Scripting.FileSystemObject").GetAbsolutePathName(ThisWorkbook.path)
Sheets("Sheet1").Activate
Sheets("Sheet1").Select
Sheets("Sheet1").Range("A1").Select
With ActiveSheet.QueryTables.Add(Connection:="TEXT;" & path & "\Users
Roles Entitlements.csv", Destination:=Sheets("Sheet1").Range("A1"))
.Name = "positions_1"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 857
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = False
.TextFileSemicolonDelimiter = True
.TextFileCommaDelimiter = True
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
Lets assume the Table Name is Table1.
Would appreciate if someone could suggest an amendment to this code so it imports directly to Table1 and readjusts the Excel Table accordingly.
I have written macro to import three columns from csv file to excel file which has 4 columns. 1 column is formulated based on 3 coming from csv file. So before running macro, there is excel file with 3 blank columns(not even column name) and 4th column with default values. Now when I run the macro, 3 columns are getting imported frm csv but 4th column is getting deleted.I don't know why this is happening. I have used Record Macro functionality to create macro. Below is my macro code:
Private Sub Workbook_Open()
Sheet11.Cells.ClearContents
With ActiveSheet.QueryTables.Add(Connection:= _
"TEXT;D:\Sample SSRS\power View\AlertHistory.csv", Destination:=Range("$A$1") _
)
.Name = "AlertHistory"
.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 = False
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = True
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(1, 1, 1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
End Sub
Please help.
You are clearing the entire worksheet with,
Sheet11.Cells.ClearContents
The 'default values' in the fourth column are going to be deleted along with everything else. If you only want to clear columns A through C and leave the 'default values' in column D then change that line to,
Sheet11.Cells(1, 1).Resize(1, 3).EntireColumn.ClearContents
This will not clear the entire worksheet; only columns A:C.
Please check below code... Here I dont want converted xml code to save in any drive, Instead I want to put the contents of converted file into the next sheet of existing excel.
Sub GenerateXML()
'
' GenerateXML Macro
'
'
Sheets("Sheet2").Select
ActiveWorkbook.XmlMaps("result_Map").Export URL:="D:\temp3.xml"
Sheets.Add After:=Sheets(Sheets.Count)
With ActiveSheet.QueryTables.Add(Connection:="TEXT;D:\temp3.xml", Destination:=Range("$A$1"))
.Name = "temp3"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 1252
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = True
.TextFileTabDelimiter = True
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(2, 1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
' Kill "C:\temp.xml"
End Sub
Instead of using XmlMaps.Export method, msdn says the following:
Use the ExportXml method to export the contents of the mapped cells to a String variable.