Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to SaveAs in Excel/ VBA using cell values ?

Status
Not open for further replies.

tful282

Programmer
Jun 26, 2001
42
US
I am fairly new with VB, any guidance is truly appreciated.
I am opening an Excel template and filling cells with values from an Access database. I want to be able to automate saving (SaveAs), and name the file with string values taken from 2 different cells.
How do I get these string values ?
This is what i have so far

Dim Rng1 As Range, Rng2 As Range
Dim StrCpy1 As Variant, StrCpy2 As Variant
Dim WrkSht As Worksheet
Dim WrkBk As Workbook
Set WrkBk = ActiveWorkbook
Set WrkSht = WrkBk.Sheets(2)
Set Rng1 = WrkSht.Range("A1")
Set Rng2 = WrkSht.Range("C1")
StrCpy1 =??????
StrCpy2=????
WrkBk.SaveAs FileName:="C:\My Documents\" & StrCpy1 & _
"_" & StrCpy2 & ".xls", _
FileFormat:=xlNormal

 
You can name a Range for the cell that will contain the information you are trying to include in the SaveAs statement.

Say C6's contents is "Accounting"
I would name the Range C6 as sFileName so I could then say
StrCpy1=Range("sFileName")
Whatever is in that cell will be used to formulate your filename. Dim sFileName as String
Hope this helps you out.
 
You can skip the Range defining steps.
Code:
Dim StrCpy1 As String, StrCpy2 As String

StrCpy1 = WrkSht.Range("A1").Text
StrCpy2 = WrkSht.Range("C1").Text
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top