VBA - Copy Cell Before Save
Feb 27, 2008
I've been playing with writing a VBA script that will copy and display the value of a cell right before saving the document.
I already have a script that saves the savetime to a cell but have been unsuccesful in using any code from that.
What I'm trying to do is to have the VBA copy different cells depending on the value of another cell.
I've been trying with some IF rules but don't really know if I'm doing this right. I'm also not sure if the code should be in "Sheet1" or in "ThisWorkBook" and if it should be a Private Sub - I've pretty much just tried a bit of this and that.
Let me try and sketch out the scenario:
I want to copy the value of a cell into a second cell depending on the value of a third cell - just before the document is saved.
How could that be done with the cells listed below?Copy-to: "A1" (the value of "B?" depending on the value of "C1")
Copy-from: "B1" IF "C1"=1
Copy-from: "B2" IF "C1"=2
Copy-from: "B3" IF "C1"=3
Copy-from: "B4" IF "C1"=4
Another problem I've been having is to change the colour of the font in a given cell if another cell returns a value of, say, 1.
View 9 Replies
ADVERTISEMENT
Oct 28, 2008
I have an Excel invoice set up and working well. It does a bunch of things with macros - e.g. on save it increments the invoice number well as creates a jpeg screenshot for the invoice archives. I have added an additional worksheet (titled 'VAT') to the workbook. The new 'VAT' worksheet has five simple columns; Invoice no, Subtotal, VAT, M.O.T. and Total.
What I need:-
On saving the workbook I would like to add a macro function that copys the final contents of the Invoice no (H2), Subtotal (C37), VAT (C38), M.O.T. (F38) and Total (I38) cells from the 'Sales Invoice' worksheet to the newly created 'VAT' worksheet in the respective columns. I would like this to be cumulative, i.e. continue to add the contents of the afore mentioned cells to the appropriate columns in the 'VAT' worksheet every time the invoice is saved. I would also like to have the Subtotal, VAT, MOT and Total columns summed and outputted in a cell of their own - but hopefully I can handle that.
View 2 Replies
View Related
Aug 27, 2012
I need VBA code for the following - I have a worksheet with seven colums of data (A to G) - I need to copy the first column (A) from the active worksheet then open master workbook called 'master' and paste the data in to column D - then save the 'master' as the name in cell Z1 of the 'master' workbook. Once this has been been completed I need to repeat the process but this time copying column (B) and so on.
View 4 Replies
View Related
Mar 27, 2008
I have a sheet which I would like to back up online every time it is saved.
I have a sub, OnlineFile(), which I have used many times without any problems, which creates a .BAT file to use ftp to upload files onto our server. As arguments, it takes the local file path that you want to upload (or download to), the name of the file as you would like it stored, the online file path, and whether you want to upload the file onto the server, or download it off the server.
It should be very easy to combine the two: what I tried was:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, cancel As Boolean)
ThisWorkbook.SaveCopyAs ThisWorkbook.Path & "" & "Rubbish.xls"
Call OnlineFile(ThisWorkbook.Path & "" & "Rubbish.xls", "Rubbish.xls", "wwwrootexcelfilesMasters", "Upload")
Kill ThisWorkbook.Path & "" & "Rubbish.xls"
End Sub
Every time I ran this, the file got uploaded correctly, but Excel had a series of problems. Often the code ran fine the first time it was run, but crashed the second time round.
After a fair bit of troubleshooting, I found that the problem came about when the file was trying to save itself - i.e. after the end of the sub. If you pause any macros from running, it still saved fine, but when the code ran, it found errors, froze or closed excel.
My thought at this point was that the code was fragmented or something. I made a new sheet, and retyped just the relevant bits of code (i.e. the BeforeSave sub, the OnlineFile sub and created the form called by OnlineFile). The problem persisted.
I've come up with a workaround for this now (which uses the same OnlineFile sub & form, and works fine), but it's driving me crazy - why does this cause excel to die? Particularly on the second time through? The code seems far too simple to cause such problems - is there a bug in Excel perhaps (I'm using Excel 2000 on Windows XP).
View 9 Replies
View Related
Nov 7, 2006
I have a workbook with one sheet. Some of the information in this sheet do I want do move to a new workbook, whit only one sheet. Then save the sheet as a CSV file.
I’m sure that this is simple, but I am very new in VBA.
So can someone help me with how to write a macro that create a new file whit only one sheet, and than saves it as a CSV file?
View 5 Replies
View Related
Jul 1, 2008
I want a Macro which will copy my workbook and paste it into a new workbook. Then it will automatically save it under the SAME name as the original and the SaveAs Box must show up (for cosmetic fixes). It is important it asks if it wants to save it under the same name. Example: My file is called "Summary_2008" and when I hit the Macro it will do those things and it will ask me if I want to save it in the SAME directory under the name "Summary_2008" (I will only add a number then).
(The macro must know how to read the original name since I have 7 possible filenames - it depends on which company I choose, that is why it is important it is saved under the same name)
Sub Zamenjaj()
Worksheets.Copy
Dim WS As Worksheet
For Each WS In Worksheets
Zadnja_vrstica = WS.UsedRange.Rows.Count
Zadnji_stolpec = WS.UsedRange.Columns.Count
For rwIndex = 1 To Zadnja_vrstica
For colIndex = 1 To Zadnji_stolpec
With WS.Cells(rwIndex, colIndex)
If WS.Cells(rwIndex, colIndex).HasFormula Then..........................
View 3 Replies
View Related
Aug 15, 2014
I am trying to create a macro to run from a form button, within a report, to save a file to a variable file path and name depending on the date value in cell B5.
The format of B5 looks like - 13/08/2014 16:39
The file path has folders for each year in format "yyyy" with each year having sub folders for each month in format "mm".
The file name is just the date only and is formatted "dd.mm.yy" e.g. 13.08.14
I have tried the code below in various permutations but always end up with an error - Method 'SaveAs' of object '_Workbook' failed.
[Code] ......
View 3 Replies
View Related
Sep 22, 2011
I have a Workbook with around 10 worksheets on, each worksheet is a form.
And when one form is completed we have to save a backup. So i want to assign a macro to a button on a worksheet so when clicked it will save a copy of that worksheet ONLY to a destination and not all the other worksheets along with it.
View 9 Replies
View Related
Oct 19, 2011
How to copy the entire worksheet into a new workbook and save the file to a specific directory with the following filename format ("exceptions191011 - ie. the word exceptions followed by today's date in ddmmyy format).
Here is what I've got so far:
Code:
Sub SaveAs()
Dim FName As String
Dim FPath As String
FPath = "G:Exceptions"
FName = "Exceptions" & Format(Date, "ddmmyy") & ".xls"
Sheets("DataSort").Copy
ThisWorkbook.Sheets("Sheet1").SaveAs Filename:=FPath & "" & FName
End Sub
How to add the following condition that if a file already exists with that name then to bring up an error message stating the file already exists? As well as save the new workbook, it appears to close the original workbook - can I stop this?
View 3 Replies
View Related
Feb 5, 2014
I have a code in which we can split the data using column values into new and same sheet also. I want to modify this accordingly to my need but i am new in VBA so can't modify this. i want 2 changes in this macro:
1. I want to set a value (Like:"Resolved"," In Process" ) in the macro to extract the data related to the value only. Means, when i run this macro it will extract the data only for "Resolved" status. I have to create two macros for each defined status.
2. After splitting, it will ask me to save the new workbook location.
3. Save it.
The codes i m having are :
First code to make directories:
Code:
Public Type BROWSEINFO
hOwner As Long
pidlRoot As Long
pszDisplayName As String
lpszTitle As String
ulFlags As Long
lpfn As Long
[code].....
View 4 Replies
View Related
Aug 11, 2007
I have created a spreadsheet for my friend. As my friend is a little 'scared' of excel I created drop downlists to complete the spreadsheet i have also protected all the cells that have formula in them so that they can not be deleted.
the spreadsheet he opens is prefect, he then updates this spreadsheet and has done something wrong but doesn't realise, he then saves the spreadsheet, it is at this point he wants a back up copy of the perfect spreadsheet, that way if he goes back into the saved copy and realises that something is wrong he can go to the back up copy and start again. This spreadsheet is filled in once a week, that way if he has done something wrong and has to refer to the back up copy he only loses one weeks worth of work.
I did say that he could choose save as instead of just save and re-name the file but again he is worried that he will forget!
Can we create a back up file automatically? If yes, can some please tell me how to?
I have never used macros or anything so if you can do this, can I ask that you also explain to me how to do it.
View 9 Replies
View Related
Jun 13, 2008
When I use this code in the workbook I want it gets as far as creating a new workbook and pasting the data, but then an error comes up saying nothing but "400", and does not complete the saving part.
Private Sub Workbook_Open()
Sheets("Open").Activate
End Sub
Sub SaveASheet()
Dim fName As String
Dim myPath As String
Dim sht As Worksheet
myPath = "K:3. DesignSupply OrdersLog"
For Each sht In ThisWorkbook.Worksheets
If sht.Range("D1").Value "" Then
sht.Copy
With ActiveWorkbook
.SaveAs myPath & ActiveSheet.Range("G11").Value & ".xlsx"
.Close
End With
End If
Next sht...................
View 9 Replies
View Related
Jan 6, 2009
I have a bill of materials for each job I do, I have multiple cut sheets in one spot on the network that must be individually copied and pasted into the project file for each job. I’m hoping to automate the process. Post extraction dump from AutoCAD, my text file goes into a excel file that the information is used by multiple tabs including a bill of materials.
From the bill of materials, I want to say “if C3>0 get file 10P0044HP2.DOC” . . . I want it to get the file and save to the individual project file. .. Possible I’m not so sure. But I’m not sure of where to start.
View 9 Replies
View Related
May 20, 2009
I need save my workbook as copy to another directory, ( C: ) with VBA code
View 9 Replies
View Related
Dec 4, 2009
I know that the line in my code Worksheets.Copy is NOT what I need to have to make this work, but with it it's the closest I could get to what I want.
I want the user to be able to click the SAVE command button, be allowed to either accept or change the default save options, save the current workbook and then go right back to the form that has the SAVE command button. Sounds simple, I know, but when you don't know the right commands to save the WORKBOOK, not just the WORKSHEETS, it becomes a P.I.A.
I know there's a lot there, but here's my code, which is just a slightly altered version of the code by Ron de Bruin:
Private Sub SaveData_Click()
'If no date has been selected then don't allow SAVE
If PubDateLabel.Caption = "" Then
MsgBox "Publication Date not set. You must select a date before saving.", vbCritical, "No Publication Date!"
Exit Sub
End If
View 9 Replies
View Related
Jul 27, 2006
I need a macro that copies a range (example A1... B50) from a excel worksheet to a text file.(txt without the comma) and save the text file with a different name without leaving the excel.
Part of the range had row hidden. Need to save the visible cell only.
View 6 Replies
View Related
Sep 8, 2006
i need to copy from one workbook to another at present i have formulars in columns a b c d e g in f and h the is data that could go to f and h100 these are not formulars
i need to be able to copy the data from a1 to the last cell with data in it in column A to the same row in column h
View 9 Replies
View Related
Oct 18, 2006
I have a workbook with a worksheet named "Report"... this sheet and it's macros generate a report from data on another sheet. I'm adding a button to copy the (filled) Report sheet to a new workbook, rename the new sheet in the new workbook "Rep[Name]", then save and close the new workbook. I've recorded a maco of me doing these tasks manually, but what's recorded seems very generic, and does not seem to specify what's really occurring...
For example (in part);
> Rightclick on the Report tab
> Move or Copy
> To Book: (new book)
> [x]Create a Copy
> [OK]
yields:
Sub Macro1()
Sheets("Report").Select
Sheets("Report").Copy
End Sub
This recorded macro does not specify (new book), or Before:"Sheet1" as was the result of my actions... and when replayed back, only makes a copy in the active workbook, before the active sheet...
View 4 Replies
View Related
Aug 18, 2007
I was able to find the coding that I needed for being able to have a backup of my file. So when I save a workbook and close I also get a backup copy in another folder that is dated. But I am having problems getting the code to work. Probably because I do not understand it correctly. Here it is.
Option Explicit
Private Sub Workbook_BeforeClose(Cancel As Boolean)
ThisWorkbook.SaveCopyAs "C:Documents and SettingsTeacherDesktopTEST" & _
Format( Date, "yyyy-mm-dd") & " - " & ThisWorkbook. Name
End Sub
Where it has, ThisWorkbook.Name, do I put the name of the workbook? Not sure.
View 7 Replies
View Related
Sep 28, 2007
I need some code that copies from one sheet into another sheet... I am thinking to have a middle sheet with all the code pulling the data from "infosheet" into another sheet "vendorsheet". I'm just wondering if this is the right way to approach this issue.. because the vendor is going to want to use the sheet and we want to automate this operation.
This is what I have so far... but this is on the actual file that we are working with so I want it to be from a middle file. It creates a new file but it doesn't save it also... I'm not sure whats wrong here so any help is greatly appreciated!
Private Sub Workbook_Open()
Copy
End Sub
Sub Copy()
'
Application.CutCopyMode = False
Sheets("infosheet").Select
Range("A1").Select
Range("A1:G50").Select
Sheets("infosheet").Select
Sheets("infosheet").Copy
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Selection.Copy
SaveAs1
End Sub
View 4 Replies
View Related
Apr 2, 2008
I do have an excel file that I do not want to be moved to another location. I was wondering if it is possible to hardcode a location for the file and then check its current location in the drive and if both locations does not match delete its contents.
I tried something like below but I keep getting an obeject required error. I would really appreciate if someone can guide me to the right direction.
Private Sub Workbook_Activate()
Dim location
location = "C:InetpubwwwrootLeadershipJC est.xls"
Dim currentloc
currentloc = ActiveWorkbook.FullName
If location = currentloc Then
MsgBox "Due to security settings of this file you can not save this file."
Else
Sheets("JobCosting"). Range(Selection, A2.SpecialCells(xlLastCell)).Delete
End If
End Sub
View 6 Replies
View Related
Apr 20, 2008
I was wondering if it is possible to have a macro running that will automatically save a .csv file to specific location and close the file. I would like to have the macro running so that as the files are opened they will be automatically saved to this location.
View 2 Replies
View Related
Apr 15, 2014
I've been searching for a way to save a copy of a workbook as an .xlsx by bringing up the save as dialogue box so the user can name and choose the location and then to open a new email with the send as attachment function so the copy can be sent to user defined recipients.
Any similar requests that I've seen are asking for a specific file name and specific directory.
View 2 Replies
View Related
Jun 21, 2014
I need a macro that will create a backup copy of excel file in other folder when an original file is closing.
View 6 Replies
View Related
Feb 3, 2014
I have a spreadsheet that I drop data into and it updates a set range on the sheet. I than have to copy that range in to notepad and save it under the name "Hourly Team Stats - 2-2-14" on our companies shared drive. If the file is already there, I have to add the data to that file rather than create a new one.
I am looking for macro that check to see if the file has already been created, if not create a new one. If it does exist, add the range to the file. If you need the path its F:Team Stats.
View 6 Replies
View Related
Nov 13, 2008
Let's say I have a database called testTemplate.mdb in c: which is a template-like database where i import data to. Is there a way for me to copy the testtemplate.mdb and past it to my desktop with file name test.mdb. I don't want to copy any data from the database, just the table's + queries. I would like this to be done via a macro... In order for me to execute other macro's to copy data from excel to that test.mdb file on desktop.
View 2 Replies
View Related
Nov 24, 2009
I am on the final part of a visitor log and I am stumpped on how to finish it. I have a electronic signature pad (Interlink Electronics-ePad) that the controls will not allow me to use in VBA for excel, I can get the image and signature on my usersform but am unable to save just the image through any code.
All I have been able to come up with at this time is saving the userform and pasting it in the active worksheet (code for this I found here written by "Michel Pierron"). What I would like to be able to do is have the userform image cropped to where the "Image1" image is (Signature will be in it place when done, the OCX esign block will not show on upload) and then saved as "visitors name, date/time.JPEG" to a folder in my documents.
The attached file is a stripped down version of what i have so far with no checking involved, just trying to get the save part of this done. If all I can do is save the userform as a whole that would work also but would like to be able to change it to a jpeg for storage. Have tried to use epads SDK's to achieve a saved signature but haven't been able, was suppose to be on vacation this week but need to get this done before the 1st.
View 5 Replies
View Related
Jan 21, 2010
I have this macro I recorded and pieced together from info I found on the net. It works the first time you run it but then it dies because the next new workbook is not "book1" what I would prefer is to have it save the new workbook as the value of sheet2 F2 and "Product Bulletin" so it would look like "##### Product Bulletin" but I don't know how to write it.
View 6 Replies
View Related
Oct 30, 2011
I have a file that opens a number of files from a source directory (which is identified by the User at run-time) & merges the source data into various worksheets (which all works fine)
Now what I'd like to be able to do is to be able to (silently) save multiple worksheets back to separate files in the original directory based on each worksheet name - e.g:
"Sheet1" and "Control" Sheet" are saved to SourceDirectorySheet1.xlsm
"Sheet2" and "Control" Sheet" are saved to SourceDirectorySheet2.xlsm
"Sheet3" and "Control" Sheet" are saved to SourceDirectorySheet3.xlsm
...
"Sheetn" and "Control" Sheet" are saved to SourceDirectorySheetn.xlsm
(Note that "Control Sheet" also contains Command Buttons & VBA which I'd like to preserve)
View 5 Replies
View Related
Jan 20, 2012
picking things up. At the moment I am trying to create a macro using VBA to do the following in this exact order:
1. Delete Column A and B (once only)
2. Automatically pick up credit card number (In column A)
3. Copy all instances of this credit card number (and all data on same row)
4. Open a template I have set up
5. Paste the copied data onto a specific sheet on this template
6. Save as file name "xxxx - branch name - XXXX - DDMMMYY"
7. Repeat (loop?) this for all credit card numbers on the sheet.
There are approximately 90 credit cards, and I need a new workbook for each one to work on separately.
View 7 Replies
View Related