Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 162
Default Referencing a cell value in a macro

Is it possible to reference a variable cell value in a macro ?

For example, A1 is linked to a combo box selection.

A macro called MACRO1 saves the current file under the filename of
<value of A1.txt .

If A1=Apple, the macro should save the current file as Apple.txt .

Any ideas ?



- Ronald K.
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,514
Default Referencing a cell value in a macro

kittronald used his keyboard to write :
Is it possible to reference a variable cell value in a macro ?


Absolutely!


For example, A1 is linked to a combo box selection.

A macro called MACRO1 saves the current file under the filename of
<value of A1.txt .

If A1=Apple, the macro should save the current file as Apple.txt .

Any ideas ?



- Ronald K.


Dim sFilename As String

sFilename = ThisWorkbook.Path & "\" & Range("A1").Value & ".txt"

--
Garry

Free usenet access at http://www.eternal-september.org
ClassicVB Users Regroup! comp.lang.basic.visual.misc


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 162
Default Referencing a cell value in a macro

Garry,

Thanks for the reply.

Took me a while to get to the point where I could test the code
above.

I've made the following changes:

Sub Macro_Export_Output()
'
' Macro_Export_Output Macro
'

'
Dim sFilename As String
sFilename = "C:\Temp\" & Sheets("Data").Range("Data").Text & ".txt"
Application.DisplayAlerts = False
Sheets("Data").Select
ActiveWorkbook.SaveAs Filename:=sFilename, FileFormat:=xlText,
CreateBackup:=False
Application.DisplayAlerts = True
Range("A1").Select
End Sub


However, when the macro is run, the following error occurs:

Run-time error '1004'

Method 'SaveAs' of object _Workbook failed

Any ideas ?


- Ronald K.
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,514
Default Referencing a cell value in a macro

Ronald,
I tried your code and it worked fine for me...

Sub ExportToTextFile()
Dim sFilename As String
sFilename = "C:\TempXls\" _
& ActiveWorkbook.Sheets("Data").Range("Data").Text _
& ".txt"
Application.DisplayAlerts = False
ActiveWorkbook.SaveAs sFilename, xlText
Application.DisplayAlerts = True
End Sub

...what I suspect is that the folder "Temp" does not exist and so the
resulting error. (SaveAs does not allow creating/renaming folders in
the process)

OR

...the active workbook - IS - the text file and you can't overwrite the
file because it's 'IN USE'!

--
Garry

Free usenet access at http://www.eternal-september.org
ClassicVB Users Regroup! comp.lang.basic.visual.misc


  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Referencing a cell value in a macro

Garry,

Thanks for getting back to me.

Google Groups hasn't been updating so I've had to find another way to get
back to you.

I figured out what the problem was.

When creating sFilename, I wasn't referencing the correct range to build
the filename.

There's a "Settings" worksheet that has a named range linked to a
ComboBox. That named range determines the filename prefix (i.e., "File1" in
File1.txt)

Below is the working macro:

Sub Macro_Export_Output()
'
' Macro_Export_Output Macro
'

'
Dim sFilename As String
sFilename = "C:\Temp\" & Sheets("Settings").Range("Data_Type").Text & ".txt"
Application.DisplayAlerts = False
Sheet1.Select
ActiveWorkbook.SaveAs sFilename, xlText
Application.DisplayAlerts = True
Sheets("Settings").Select
End Sub


Thanks for all the help ! +1,000,000



- Ronald K.




  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,514
Default Referencing a cell value in a macro

You're welcome! Glad you were able to get it working.
I appreciate the feedback!

--
Garry

Free usenet access at http://www.eternal-september.org
ClassicVB Users Regroup! comp.lang.basic.visual.misc


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Referencing 2 cells in a macro Bob Excel Programming 3 March 20th 08 03:36 AM
Referencing a Cell in Macro Bob Excel Programming 3 March 19th 08 08:21 PM
referencing a cell in a macro R Dunn Excel Programming 5 February 3rd 07 11:38 AM
Sheet referencing in a macro Nick Hodge Excel Programming 2 December 19th 06 11:15 PM
Sheet referencing in a macro Craig B Excel Programming 0 December 18th 06 09:43 PM


All times are GMT +1. The time now is 02:43 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"