LITTLEBLACKDOG.COM Forum Index LITTLEBLACKDOG.COM

 
LWD LWD   FAQ FAQ   Memberlist Memberlist   Usergroups Usergroups   Active Topics Active Topics   Register Register  
  Profile Profile   Log in to check your private messages Log in to check your private messages   Log in Log in  
  Who is Online Who is Online   Image Gallery Image Gallery   Chat Chat   Search Search  
  LWDGear       LBDGear  

View next topic
View previous topic
Post new topic     Reply to topic   LITTLEBLACKDOG.COM Forum Index » General Software
Author Message
squashman
Big Dog
Big Dog


Joined: 08 Oct 2001
Posts: 3429
Location: 1265 Lombardi Ave.

Post Posted: Thu May 15, 2008 7:12 pm   Post subject: Excel Macro Reply with quote Back to top  

Is it possible to have a Macro output the contents of the currently selected Cell to a text file?
View user's profile Send private message Send e-mail
EdisonRex
Guide Dog
Guide Dog


Joined: 06 May 2002
Posts: 9929
Location: Not Moscow

Post Posted: Fri May 16, 2008 12:33 am   Post subject: Reply with quote Back to top  

The short answer is "of course".

Code to do this would be something like

Dim Outfile, OutStr As String

Open Outfile For Output As #1

OutStr = ActiveCell.Value ' you might need to convert to string

Print #1, OutStr

_________________
Garret: It's so retro.
EGM: What does retro mean to you?
Parker: Like, old and outdated.
View user's profile Send private message AIM Address Yahoo Messenger
squashman
Big Dog
Big Dog


Joined: 08 Oct 2001
Posts: 3429
Location: 1265 Lombardi Ave.

Post Posted: Wed Jun 04, 2008 9:31 am   Post subject: Reply with quote Back to top  

This is what I ended up doing.
Code: Select all
Sub SendCellToTextFile2()
    Dim sFullName As String, sPath As String, iFileNum As Integer, iReply As Integer
    If Intersect(ActiveCell, Range("A:D")) Is Nothing Then
        MsgBox "You must select a cell in A or D!", vbExclamation, "ERROR!"
        Exit Sub
    End If
    If Cells(ActiveCell.Row, "D").Value = vbNullString Or Cells(ActiveCell.Row, "B").Value = vbNullString Or Cells(ActiveCell.Row, "A").Value = vbNullString Then
        MsgBox "You need to fill out both cells!", vbExclamation, "ERROR!"
        Exit Sub
    End If
    sPath = ThisWorkbook.Path & "\batch\"
    If Dir(sPath, vbDirectory) = "" Then MkDir sPath
    sFullName = sPath & "String_" & Cells(ActiveCell.Row, "A").Value & ".bat"
    iFileNum = FreeFile
    Open sFullName For Output Access Write As #iFileNum
    Print #iFileNum, Cells(ActiveCell.Row, "D").Value
    Close #iFileNum
   
    iReply = MsgBox(Prompt:="Do you wish to run String_" & Cells(ActiveCell.Row, "A").Value & ".bat", _
            Buttons:=vbYesNo, Title:="Run Batch File")
           
    If iReply = vbYes Then
        Shell sFullName 'Run the batch file
    Else 'They clicked No
        Exit Sub
    End If
End Sub
View user's profile Send private message Send e-mail
Display posts from previous:   
Post new topic     Reply to topic

View next topic
View previous topic
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2002 phpBB Group
phpBB SEO
All times are GMT - 8 Hours

Help us keep advertisements off this site. Donate today!