I am trying to achieve a simple objective,insert row count and column count summary of an excel file to an existing notepad.
I have multiple files in a folder and would like to run this operation for each file and send the details to a notepad.
Issue: Every time i run the code it deletes existing content and inserts new data into the notepad. I would like to retain existing data and start appending from a new line
code:
Sub Sndtotxt()
Dim FF
Dim rCnt AS INTEGER
Dim cCnt AS INTEGER
rCnt = ActiveSheet.UsedRange.Rows.Count
cCnt = ActiveSheet.UsedRange.Columns.Count
FF = FreeFile()
OPEN "C:\Temp files\summaryreport.txt" FOR Output AS #FF
Print #FF, rCnt
Print #FF, cCnt
CLOSE #FF
END Sub
replace
OPEN "C:\Temp files\summaryreport.txt" FOR Output AS #FF
with
OPEN "C:\Temp files\summaryreport.txt" FOR Append AS #FF
In terms of looping through files in a folder, suggest you start with my code from Loop through files in a folder using VBA?
Change the path below for both
Excel files C:\temp\
Txt report C:\Temp\test.txt
to suit
Sub GetEm()
Dim WB As Workbook
Dim StrFile As String
Dim FF
FF = FreeFile()
Open "C:\Temp\test.txt" For Append As #FF
StrFile = Dir("c:\temp\*.xls*")
Do While Len(StrFile) > 0
Set WB = Workbooks.Open("c:\temp\" & StrFile)
StrFile = Dir
Print #FF, WB.Name, WB.Sheets(1).UsedRange.Rows.Count, WB.Sheets(1).UsedRange.Columns.Count
WB.Close
Loop
Close #FF
End Sub
Related
Text file
I am new here and I want t know how to change the value of a number in a text file.
This is for a project I am working on for the company I work for.
The number is used to create new project numbers by clicking a single button.
With the code in the file below I can read the text from the file on my desktop and put it in a cell in Excel. But after that I want the number to increase by 10.
Can somebody please help me.
Thanks alot in advance.
Sub Hallo()
Dim X As Double
Dim TXT As String
Open "C:\Users\Leon\Desktop\Project nummer.txt" For Input As #1
Do While Not EOF(1)
Line Input #1, TXT
Sheets("Blad1").Range("C2") = TXT
Loop
Close #1
End Sub
Write back to the file
Option Explicit
Sub Hallo()
Const FOLDER = "C:\Users\Leon\Desktop\"
Const FILENAME = "Project nummer.txt"
Const INC = 10
Dim s As String, ff
ff = FreeFile()
Open FOLDER & FILENAME For Input As #ff
Line Input #ff, s
Close #ff
If IsNumeric(s) Then
Sheets("Blad1").Range("C2") = Format(s, 0)
' save new number
ff = FreeFile()
Open FOLDER & FILENAME For Output As #ff
Write #ff, Val(s) + INC
Close #ff
Else
MsgBox s & " not valid number", vbCritical
End If
End Sub
VBA: Is there a way to save a list of a combobox to a .txt file?
I did this one here that puts the information of a txt file into a combobox
Dim InFile As Integer
InFile = FreeFile
Open "MYFILE.txt" For Input As InFile
While Not EOF(InFile)
Line Input #InFile, NextTip
ComboBox1.AddItem NextTip
ComboBox1.ListIndex = 0
Wend
Close InFile
The following macro will print a list of items from the specified combobox to the specified text file. Change the name of the macro, and the path and file name for the destination file, accordingly.
'Force the explict declaration of variables
Option Explicit
Private Sub CommandButton1_Click()
'Declare the variables
Dim destFile As String
Dim fileNum As Long
Dim i As Long
'Assign the path and file name for the destination file (change accordingly)
destFile = "C:\Users\Domenic\Desktop\sample.txt"
'Get the next available file number
fileNum = FreeFile()
'Print list items from combobox to destination file
Open destFile For Output As #fileNum
With Me.ComboBox1
For i = 0 To .ListCount - 1
Print #fileNum, .List(i)
Next i
End With
Close #fileNum
MsgBox "Completed!", vbExclamation
End Sub
I am a technical writer and not much of a coder, I have thousands of .xml files that combined create a book. I used a VBA script from this website to rename all of the files to fit within the new guidelines, now I need to go into the xml code and find all references to those links and replace them with the new file name.
I have an excel spreadsheet where in column A it has the old file name and in column B it has the new file name.
The tag looks like this:
<?iads.link docref="R381"?>
It needs to find "R381" in column A and replace it with "R01081-1-1520-237", the file name in the adjacent cell in column B.
The Tag needs to look like this:
<?iads.link docref="R01081-1-1520-237"?>
I tried using the code from the question How can I Find/Replace multiple strings in an xml file? but it did not work and I'm not even sure if that's the correct question to be asking
My current code looks lie this:
Option Explicit ' Use this !
Public Sub ReplaceXML(rFindReplaceRange As Range) ' Pass in the find-replace range
Dim sBuf As String
Dim sTemp As String
Dim iFileNum As Integer
Dim sFileName As String
Dim i As Long
' Edit as needed
sFileName = "C:\Users\s37739\Desktop\chap3"
iFileNum = FreeFile
Open sFileName For Input As iFileNum
Do Until EOF(iFileNum)
Line Input #iFileNum, sBuf
sTemp = sTemp & sBuf & vbCrLf
Loop
Close iFileNum
' Loop over the replacements
For i = 1 To rFindReplaceRange.Rows.Count
If rFindReplaceRange.Cells(i, 1) <> "" Then
sTemp = Replace(sTemp, rFindReplaceRange.Cells(i, 1), rFindReplaceRange(i, 2))
End If
Next i
' Save file
iFileNum = FreeFile
' Alter sFileName first to save to a different file e.g.
sFileName = "C:\Users\s37739\Desktop\chap3"
Open sFileName For Output As iFileNum
Print #iFileNum, sTemp
Close iFileNum
End Sub
Sub mike1()
End Sub
You are passing the file path when in fact you should be passing the fully qualified file name (file path and file name).
You need to edit those lines
' Edit as needed
sFileName = "C:\Users\s37739\Desktop\chap3"
'...
' Alter sFileName first to save to a different file e.g.
sFileName = "C:\Users\s37739\Desktop\chap3"
With
' Edit as needed
sFileName = "C:\Users\s37739\Desktop\chap3\yourfilename.xml"
'...
' Alter sFileName first to save to a different file e.g.
sFileName = "C:\Users\s37739\Desktop\chap3\yourNEWfilename.xml"
Also, remember to provide the correct Range when running the procedure.
Supposing your Range goes from "A1:B50" you can edit your mike1 sub as follows:
Sub mike1()
' Change range as desired
Call ReplaceXML(ThisWorkbook.Worksheets("YourSheetName").Range("A1:B50"))
End Sub
After that, all you need to do is run mike1 from the Immediate window.
Access using Alt+F11 for the VBA editor then View -> Immediate.
You should see a new window at the bottom of the screen. Just type mike1 in there and hit Enter
UPDATE:
Ideally, you should first try to understand the code you currently have and change it make it work on multiple files instead of a single one per run. There are many places around that can give you examples on how to do that, either recursively or in a loop directly into a function. There are many ways to do it and also many material around it.
That being said, you can find below one of the many approaches to solve your issue. The following code consists of two Subs that you can copy/paste into your module.
You need to change the value of HOST_PATH with the main folder and the findReplaceRange with the range to work with. You need to change "Sheet1" with the name of your worksheet and the "A1:B10" with your actual range. After that, just run the ReplaceXML2() Sub.
Note: this will update ALL XML files under the folder provided, so make sure you gave it enough testing (preferably, backup your files) before running it for the entire folder. If you have further issues I suggest asking another question.
Code:
Public Sub ReplaceXML2()
Const HOST_PATH = "C:\Users\s37739\Desktop\chap3\" ' change accordingly
Dim findReplaceRange As Range
Set findReplaceRange = ThisWorkbook.Worksheets("Sheet1").Range("A1:B10") ' change accordingly
Dim FileSystem As Object
Set FileSystem = CreateObject("Scripting.FileSystemObject")
Call RecursivelyReplaceXML(FileSystem.GetFolder(HOST_PATH), findReplaceRange)
End Sub
Public Sub RecursivelyReplaceXML(parentFolder, rFindReplaceRange As Range) ' Pass in the folder and the find-replace range
Dim subFolder As Object
For Each subFolder In parentFolder.SubFolders
RecursivelyReplaceXML subFolder, rFindReplaceRange
Next
Dim file As Object
For Each file In parentFolder.Files
If Right(file.Name, 4) = ".xml" Then
Dim iFileNum As Integer
Dim sTemp As String
Dim sBuf As String
Dim i As Long
Dim fullFileName As String
fullFileName = file.Path
iFileNum = FreeFile
Open fullFileName For Input As iFileNum
sTemp = "" ' clean up to read the next file
Do Until EOF(iFileNum)
Line Input #iFileNum, sBuf
sTemp = sTemp & sBuf & vbCrLf
Loop
Close iFileNum
' Loop over the replacements
For i = 1 To rFindReplaceRange.Rows.count
If rFindReplaceRange.Cells(i, 1) <> "" Then
sTemp = Replace(sTemp, rFindReplaceRange.Cells(i, 1), rFindReplaceRange(i, 2))
End If
Next i
' Save file
iFileNum = FreeFile
' WARNING: New name definition commented out,
' which means all files will be replaced with newer versions!!
'===
' Alter fullFileName first to save to a different file e.g.
' fullFileName = "C:\Users\s37739\Desktop\chap3\"
Open fullFileName For Output As iFileNum
Print #iFileNum, sTemp
Close iFileNum
End If
Next
End Sub
I am trying to create a macro that will search a folder for a .dat file that contains "OPS"(not case sensitive) in the name, if it finds a file I would like to open it and run another macro, save the file as the original filename.xlsm, and close.
So far, I am able to search for the name but that's about the extent of my knowledge.
Sub Test2()
Dim sh As Worksheet, lr As Long, fPath As String, fName As String, rFile() As Variant
fPath = "C:\Users\ntunstall\Desktop\test\"
ctr = 1
fName = dir(fPath & "*.dat")
Do Until fName = ""
If InStr(fName, "OPS") > 0 Then
ReDim Preserve rFile(1 To ctr)
rFile(ctr) = fName
ctr = ctr + 1
End If
fName = dir
Loop
For i = LBound(rFile) To UBound(rFile)
'The variable rFile(i) represents the workbooks you want to work with.
MsgBox rFile(i)
Next
End Sub
Ideally, this macro would run any time a .dat file containing OPS in the filename is opened. Any help is appreciated, thanks.
To the top add
Dim wb as workbook
and then replace your message box line with
Set wb = Workbooks.Open(fPath & rFile(i))
wb.SaveAs fPath & Split(rFile(i), ".")(0), xlOpenXMLWorkbookMacroEnabled
wb.Close
I tested with a tab deliminated file and it worked well. Your issues may vary if you have a different format.
I am converting an excel file to a text file by a macro and I want the text file's location to be the same folder as the excel worksheet's location.
My code is:
Dim strPath As String
strPath = "MyFileName.dat"
Dim fnum As Integer
fnum = FreeFile()
Open strPath For Output As #fnum
'my code
Close #fnum
When running it always goes to Documents. I tried "../MyFileName.dat" and it worked with some of the locations I tried putting the excel worksheet in but not with all.
What's the right way to do this. Thank you.
Assuming the workbook in question is the ActiveWorkbook, this will work. It get the workbook's full path with FullName and subsitutes the data file's name for for the workbook's:
Sub test()
Dim wb As Excel.Workbook
Dim strPath As String
Set wb = ActiveWorkbook
strPath = Replace(wb.FullName, wb.Name, "MyFileName.dat")
Dim fnum As Integer
fnum = FreeFile()
Open strPath For Output As #fnum
'my code
Close #fnum
End Sub