VB: Highlight Excel cell in worksheet - excel

I was wondering if it was possible to create a vbs file to highlight a cell in an excel workbook.
I have an excel worksheet with multiple computer host-names, I also run a script in batch that pings each host-name in a text document. I want to call the vbs file to highlight the cell in excel if the ping result was successful. Is this possible?
Thanks!
Dimitri

There are plenty of ways to do this, but I must ask why you're using a batch script and a text file to ping the hostnames when you can do that right in Excel?
There are two ways to do this... one is a bit more complex and correct and the other is quick and dirty. Frankly, I recommend the quick and dirty.
Correct Way:
Declare the ReadConsole & WriteConsole methods from your Windows kernel32.dll and utilize them to get the results of your ping. It's described well here:
http://visualbasic.about.com/od/learnvb6/l/bldykvb6dosa.htm
Q&D Way:
Use the built-in Shell() function in VBA and pipe the output of the ping to a text file. Parse said text file and delete it when you're done.
e.g.
for each currCell in hostnameRange
' Ping each hostname and pipe the results to a file
shell "ping " + currCell.value + " >> ping_result.txt"
next currCell
inFile = FreeFile()
Open "ping_result.txt" for Input as #inFile
fileBuffer = Input$(LOF(inFile ), inFile) ' Open and read the file to a buffer
for each currCell in hostnameRange
' Search for ping failures in the buffer
if instr(1, fileBuffer, "could not find host " + currCell.value) = 0 then
debug.print "Ping successful."
end if
next currCell

Related

Search txt and csv files for string using Excel VBA without opening files

I have a text file that is automatically generated from a machine. The machine writes the txt file in "chunks" (sorry I don't know the exact terminology). I need to pull data from this txt file, but I need the txt file to be finished before pulling data from it. I found a solution to verify that the machine has finished writing to the file... It is not as elegant as i had hoped, but seems to do the trick. Excel VBA opens a command prompt, the command prompt uses a Find command to find the string "End of Report"... This is basically one of the last lines of the txt file and pretty safe to assume the txt file is finished after this is found. This code runs in a loop 1000 times, every 10 seconds, until it finds this string or reaches 1000 tries...
The issue is that "result" returns some other characters besides just "End of Report" this is further complicated by the fact that I am attempting to run this on some csv files too... and "result" returns some additional characters also, but different from the ones returned from the txt files. For example, if I check the length of "result"... The length comes back as 43 on one file and 48 on another file... I think it is counting the file path + "End of Report" + a few more characters?
Anyways, I don't really need the "result"... I really only need a "true" / "false" if "Find" found "End of Report" or not... How can I accomplish this? Is there a different better way to do this? I am not familiar with command prompt programming.
Note: It is important that I search these files without opening them.
Sub test()
Dim SearchStr As String
Dim cmdLine As Object
Dim result As String
Dim FilePath As String
FilePath = "D:\test2.txt"
SearchStr = """End of Report"""
Set cmdLine = CreateObject("WScript.Shell")
result = cmdLine.Exec("%comspec% /C Find " & SearchStr & " " & Chr(34) & FilePath & Chr(34)).STDOut.ReadAll
Debug.Print (result)
End Sub
I am not really an expert in command line, but what I would do is export the result of the FIND command to a file, like here
Then I would check in your VBA code how many rows are in the file (either clean the file before, or check the number of rows before the export is done).
If the number of rows meets the criteria (probably 2 or more rows instead of 1), then you can set the flag to True.

Copying lines from Wordpad into Excel using VBA

I am writing some code where I import some files under TMX (a form of xml).
I tried various options
a) using the Open FileName For input, but this messes up the character encoding
b) opening the file and copying the data using the msoDialog, but this return an error if the file is too large (which is often the case) and this put the data in an utterly messy manner.
c) opening the file using notepad, but there are the same limitations in so far as copying the entirety of the file into Excel as the previous option.
I am not trying to use a shell function calling onto Wordpad.
My issue right now, is that I need to copy the file line by line to treat its content according to my needs (hopefully without losing the character encoding
Would someone know how to copy every single line from the file opened in WordPad and paste it post treatment (selection of the relevant elements) into Excel?
Thank you
For large files you can use this solution:
Public Sub ImportTMXtoExcel()
Call Application.FileDialog(msoFileDialogOpen).Filters.Clear
Call Application.FileDialog(msoFileDialogOpen).Filters.Add("TMX Files", "*.tmx")
Application.FileDialog(msoFileDialogOpen).Title = "Select a file to import..."
Application.FileDialog(msoFileDialogOpen).AllowMultiSelect = False
intChoice = Application.FileDialog(msoFileDialogOpen).Show
If intChoice <> 0 Then
strFileToImport = Application.FileDialog(msoFileDialogOpen).SelectedItems(1)
Else
Exit Sub
End If
intPointer = FreeFile()
Open strFileToImport For Input Access Read Lock Read As #intPointer
intCounter = 0
Do Until EOF(intPointer)
Line Input #intPointer, strLine
intCounter = intCounter + 1
Worksheets(1).Cells(intCounter + 1, 1).Value2 = strLine
Loop
Close intPointer
End Sub
For other encodings you can use ADO's Stream as described in this solution:
VB6/VBScript change file encoding to ansi
If you have large files which require ADO's Stream then you might want to consider breaking down the large files first as described in this solution:
How to split a large text file into smaller files with equal number of lines?
The following website provides a tool which mimics the Unix command split for Windows in command prompt: https://www.fourmilab.ch/splits/

Reading each line of a text file into VB

I'm looking to automate some work that I currently have to carry out.
I currently receive a number of machine names that I have to query in unix from config files, but I have to amend the list I receive each day to produce the command. I'm looking for a way to automate this, so I can store the names in a text file and run a script in vb that will automatically produce the command I need to run in unix.
e.g the text file (machines.text) may contain the following:
ABCDE1234
ADEFR1234
BCDFREWE1
each line will be the machine name, but i require this to be changed to lower case and get the following commands output:
grep -i abcdef1234 */*.cfg
grep -i adefr1234 */*.cfg
grep -i bcdfrewe1 */*.cfg
I sometimes get hundreds a day, so looking to shorten the processes as I can just use the original file i receive and not have to manually go through it.
Any suggestions will be much appreciated, even if anyone has any alternatives to VB and excel.
Thanks
you could open the list in Excel, so that all the data to be manipulated goes into Column A, then use formulas as below to create your UNIX commands in Columns B, C, D:
FIRST Command:
="grep -i " &LOWER(LEFT(A:A,FIND(" ",A:A))) & " */*.cfg"
SECOND Command:
="grep -i "&LOWER(LEFT(TRIM(MID(A:A,FIND(" ",A:A),LEN(A:A))),FIND(" ",TRIM(MID(A:A,FIND(" ",A:A),LEN(A:A))))))&" */*.cfg"
THIRD Command:
="grep -i "&LOWER(MID(RIGHT(A:A,LEN(A:A)-FIND(" ",A:A)),FIND(" ",RIGHT(A:A,LEN(A:A)-FIND(" ",A:A))),LEN(RIGHT(A:A,LEN(A:A)-FIND(" ",A:A)))))&" */*.cfg"
Once you have got all your texdt to be manipulated in Column A, copy down the formulas in columns B,C,D
then you could run the below code (may need tweaking) to create your output:
Sub getCommands()
Dim oFso As New Scripting.FileSystemObject
Dim oWriteFile As TextStream
Dim oRange As Range
Set oWriteFile = oFso.CreateTextFile("C:\Commands.txt", True)
Set oRange = Range("B2")
Do Until oRange.Text = ""
With oWriteFile
.WriteLine oRange.Text
.WriteLine oRange.Offset(0, 1).Text ' second command
.WriteLine oRange.Offset(0, 2).Text ' third command
End With
Set oRange = oRange.Offset(1, 0)
Loop
oWriteFile.Close
End Sub

Applescript file search using data from a spreadsheet

I am currently using this Applescript I found that searches for a file name and returns the file path in a text doc. This works fine for finding 1 or 2 files, but I would like to find 500 files that are spread over hundreds of folders. My ideal script would use data from an excel spreadsheet or csv, perform a search, find the file and make a copy of it in a designated folder on my desktop. Any help is appreciated.
Here is the script I found:
tell application "System Events"
activate
set thePattern to text returned of (display dialog "Search for" default answer "")
end tell
if thePattern = "" then return
try
set foundFiles to do shell script "mdfind -name " & quoted form of thePattern & " | /usr/bin/egrep -i " & quoted form of thePattern & "[^/]*/?$ | /usr/bin/grep -vi " & quoted form of thePattern & ".*" & quoted form of thePattern
on error
set foundFiles to "Nothing Returned"
end try
if foundFiles = "" then set foundFiles to "Nothing Returned"
tell application "TextEdit"
activate
delay 0.5
try
set theDoc to document 1
get text of theDoc
if result is not "" then
make new document
set theDoc to result
end if
on error
make new document
set theDoc to result
end try
set text of theDoc to foundFiles
end tell
You need to read the data from the text file, then turn it into a return or linefeed delimited list and do a repeat over the items of this list. Then turn each item (which is actually a line) into e.g. a tab delimited list and again do a (nested) repeat loop over the items of this list. If you know that e.g. item 3 is the file path, you can set a variable to item 3 of the line as text and use this variable in your shell script.
I think you need to show that you understand the concept of repeat loops by posting your own attempt of implementing this. If you do, I'll be happy to come back and help you with the next step.
Kind regards,
Mark

access command line data in Excel VBA fails

I need to access the command line parameters in an Excel VBA macro, and found many variations but only one that worked with Excel-2010, seems that the API has changed over time. I tried this code I found "out there":
Dim pCmdLine As Long ' Pointer to the Comand-line string
Dim strCmdLine As String ' Command line string
Dim CmdLine As String ' Command line string
' Get the pointer to the command line string
pCmdLine = GetCommandLineA
' Fill the string with zeros
' (300 characters for command line seems to be enough)
strCmdLine = String$(300, vbNullChar)
' Copy from the pointer to VBA-style string
lstrcpynA strCmdLine, pCmdLine, Len(strCmdLine)
' At this point we got the string,
' now skip the rest of it filled with 0 characters.
CmdLine = Left(strCmdLine, InStr(1, strCmdLine, vbNullChar) - 1)
MsgBox "Length of the command line = " & Len(CmdLine) '' Debug
MsgBox "Command Line:: " & CmdLine '' Debug
Which I put into the Auto_open macro of a spreadsheet. If I try this call:
start excel TestMacro.xlsm /e/abcd/xyz
It seems to generally work and the macro reports:
Command line = " C:/.../excel.exe TestMacro.xlsm"
So I get the invocation part, but the parameters are lost.
Partial Solution:
I find that if I change the invocation to:
start excel /e/abcd/xyz TestMacro.xlsm
It works, except the parsing code has to be changed to ignore the file name which is not at the end, and also this form doesn't seem to allow any blanks in any parameter, even if quoted. The system seems to interpret them as file names for a target excel file and give an error. For example:
start excel /e/abc/"my sheet"/ TestMacro.xlsm
gives the error:
file 'sheet"/.xlsx' not found
Although after the spurious startup error, the intended sheet does open and get teh entire line to work with.

Resources