473,396 Members | 1,967 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Excel change save location when combobox value changes

13
Hello

I was wondering if it is possible to define a save location when a value in a combobox is selected.

ex.

Combobox:
a
b
c

if 'a' is selected i want the save location to be Z:\a\filename.xls

I've searched a bit already but haven't found anything specific yet, any help is very much appreciated.

Regards

Dresse
Jul 30 '10 #1

✓ answered by Stewart Ross

Hi. It is quite possible to do what you require, but it needs a little thought as to how to do it.

In VBA you will need to use the SaveAs method of the Workbook object to save the file to a new location. SaveAs requires a filename inclusive of its path, so if you want to make the path variable on selection of a combo value then you will need to define the path and the filename separately.

You do not say whether or not your code will be running in Excel itself, or whether you are using Excel as an automation server running from Access, say.

If I assume that your combo box is in an Excel workbook then you would need something like this in the combo's Change or AfterUpdate event code:

Expand|Select|Wrap|Line Numbers
  1. Private Sub YourCombo_Change()
  2.     Dim strPath as String
  3.     Dim strFileName as String
  4.     Const cFileName = "yourfile.xls"
  5.     If Not IsNull(Me.yourcomboname) then
  6.         Select Case Me.yourcomboname
  7.             Case "a"
  8.                  strPath = "x:\"
  9.             Case "b"
  10.                  strPath = "z:\subfolder\"
  11.             Case Else
  12.                  strPath = "C:\"
  13.         End Select
  14.         strFileName = strPath & cFilename
  15.         ActiveWorkbook.SaveAs FileName := strFileName
  16.     End If
  17. End Sub
You will need to adapt the basics above as necessary, but the idea is to have a path that is selectable using the Change or AfterUpdate event of the combo, combining it with a name for the file to produce a full path and filename, then saving the current Excel workbook using the SaveAs method and the full path for the filename.

Be careful with paths - as these are combined with the filename to provide the full location you must ensure that the path is correctly terminated with a backslash (i.e. C:\somefolder\ and not C:\somefolder) before you concatenate it with the filename.

-Stewart

2 2717
Stewart Ross
2,545 Expert Mod 2GB
Hi. It is quite possible to do what you require, but it needs a little thought as to how to do it.

In VBA you will need to use the SaveAs method of the Workbook object to save the file to a new location. SaveAs requires a filename inclusive of its path, so if you want to make the path variable on selection of a combo value then you will need to define the path and the filename separately.

You do not say whether or not your code will be running in Excel itself, or whether you are using Excel as an automation server running from Access, say.

If I assume that your combo box is in an Excel workbook then you would need something like this in the combo's Change or AfterUpdate event code:

Expand|Select|Wrap|Line Numbers
  1. Private Sub YourCombo_Change()
  2.     Dim strPath as String
  3.     Dim strFileName as String
  4.     Const cFileName = "yourfile.xls"
  5.     If Not IsNull(Me.yourcomboname) then
  6.         Select Case Me.yourcomboname
  7.             Case "a"
  8.                  strPath = "x:\"
  9.             Case "b"
  10.                  strPath = "z:\subfolder\"
  11.             Case Else
  12.                  strPath = "C:\"
  13.         End Select
  14.         strFileName = strPath & cFilename
  15.         ActiveWorkbook.SaveAs FileName := strFileName
  16.     End If
  17. End Sub
You will need to adapt the basics above as necessary, but the idea is to have a path that is selectable using the Change or AfterUpdate event of the combo, combining it with a name for the file to produce a full path and filename, then saving the current Excel workbook using the SaveAs method and the full path for the filename.

Be careful with paths - as these are combined with the filename to provide the full location you must ensure that the path is correctly terminated with a backslash (i.e. C:\somefolder\ and not C:\somefolder) before you concatenate it with the filename.

-Stewart
Jul 30 '10 #2
Dresse
13
Edit - nvm I found what I was looking for using part of your solution

Thanks again!
Jul 30 '10 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: codecraig | last post by:
Hi, I am using Tkinter and I have a Label and a Scale. I want to update my label everytime the Scale value changes. What is the best way of doing this? Do i have to bind for every event type? ...
7
by: User | last post by:
Hi, A page I have shows a different background colour depending on the hash portion of the url as it is first loaded. For example a link to mysite/mypage#0000FF would result in a page with a blue...
4
by: Rodger Dusatko | last post by:
In VB .NET I am having problems setting breaks. I want the break to be independant of any single function: for example: err.number or global variables When I try to set a break with the 'Data'...
4
by: andreas.w.h.k. :-\) | last post by:
How do I change the address location in the wsdl <wsdl:port name="SearchSoap12" binding="tns:SearchSoap12"> <soap12:address location="http://searchservices/engine/search.asmx" /> </wsdl:port> ...
7
by: Macolm64 | last post by:
I am looking for some code that would loop until two cell values in excel equal the same. By cell value I mean the result of the formulas in the cells are the same. This procedure needs to be run...
0
by: joinzulfi | last post by:
Dear fellows! I have designed a form having SaleMasterTable and SaleDetailTable. SaleDetailTable is Bounded with DataGrid. Now I want that if in2nd Column's cell (it is combobox) value is...
4
by: g1adiat0r | last post by:
Alright, so I'm working on a board and I want the chatbox located on the board to change colours whenever you change the board theme. I have a couple of questions: If I create a variable in a...
1
by: raul15791 | last post by:
Hi, I'm new to C#. I'm writing a program that open a new excel file, write into it and lastly close the file. But the program is that there will be a orphaned process named EXCEL.EXE left on the...
5
by: laredotornado | last post by:
Hi, Is there a way to trigger an event when the value of a hidden field changes? At a certain point in time my hidden field is getting changed to a value that I don't want but I can't find a...
3
by: crjunk | last post by:
Hi Everyone, I have a web form that I would like to enhance. What I'd like to do is this: A user adds/edits the text in a textbox. I want the adjoining label to change color. Example: User...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.