Insert Data Paste Error on other Computers - excel

I have finished a small Project at work and distributed the File to my coworkers.
I saved the file as .xlsm to enable makros. I have used the code for about 2 weeks now and never had any problems with it.
However when i sent them to my Coworkers one makro doesnt work.
I have multiple makros in the File and only one of them doesnt work anymore so i believe its the makros problem and not the new Excel version:
Sub Loomisinsert()
'Deletes old Data and inserts new
Sheets("MSE").Range("B2:J1000").Clear
Sheets("MSE").Range("B2:B2").Select
Sheets("MSE").Paste
'deletes unnecessary data
Sheets("MSE").Range("K3:N5000").Delete
Sheets("MSE").Range("P3:Q5000").Delete
Sheets("MSE").Range("S3:U5000").Delete
Sheets("MSE").Range("L3:L5000").Delete
End Sub
The user is only supposed to copy Data from another Workbook and use an excel button to insert the data at the right place and delete the correct cells.
As you can see i tried to specify which sheet to use for each command. And it works fine for me but not on my Coworkers Computer.
They have Excel 2013 and 2010 the same i use.

Below are some suggestions. Modify and try:
Option Explicit
Sub Loomisinsert()
With ThisWorkbook.Worksheets("Sheet1")
'Deletes old Data and inserts new
.Range("B2:J1000").Clear
.Range("B5").Copy .Range("B2") '<- Easy way to copy - paste
'Deletes unnecessary data
.Range("K3:N5000,P3:Q5000,S3:U5000").Clear'<- Does not need to use "L3:L5000" because included in "K3:N5000". Additionally, have in mind that *.Delete* may cause problem with columns & rows. It s more preferable to use *.Clear* or *.Clearcontents*. *.Clear* used to clear both formats and value. In the other hand *.Cleatcontents* just empty the cell from value.
End With
End Sub

Related

Copy/Paste data to other existing workbook on timer

I currently have this VBA -
Sub StartTimer()
Application.OnTime Now + TimeValue("00:00:15"), "AutoCalc"
End Sub
Sub AutoCalc()
Application.CalculateFull
Sheets("Kine").Range("B603:E603").Copy _
Destination:=Workbooks("AutoImportAverages.xlsx").Worksheets("AvgKine").Range("B1:E1")
Application.OnTime Now + TimeValue("00:00:15"), "AutoCalc"
End Sub
The .OnTime command is working perfectly without the Copy/Paste section, which is great.
This gives me a list of values from an SQL query that will auto-update, as well as an average at the bottom of each column of values.
I'm trying to set this up so that the average will automatically be added onto columns in Minitab but I believe that the Macro is stopping the Auto-Update in Minitab.
So my idea is to copy-paste the averages into an Excel Workbook that has no macros of its own and then link that to Minitab.
Have I put in the Copy-Paste code wrong or is there some issue with where the macros need to be stored and how?
Quick Edit - I should add that current code gives "Run-Time Error 9, Subscript out of range" and highlights copy/paste code.
I've found the solution.
My destination workbook was open in a separate window so the source wasn't recognising it as being open. Bit of a nightmare!
It's necessary to have both workbooks open in the same instance of Excel.
Additionally, my original paste code only pasted "#REF". I have changed that to -
Workbooks("AutoImportAverages.xlsx").Worksheets("AvgKine").Range("B1:E1").PasteSpecial xlValues
Works much better.
One more thing, in case anybody might find it useful. The source workbook must be active in order to carry out the auto-update.
Adding below line sorted out most issues though its still a work in progress -
ThisWorkbook.Activate

automatically run excel macro upon creation from template

I want to create an excel template that will includes formulas for dating the columns. However, since those formulas will be based on TODAY(), I need to convert them to static strings (so the dates don't change everytime someone opens it). Is there a way to add a macro that will run automatically when someone creates a new spreadsheet based on the template? (Similarly to Auto_Open(), only on Create, rather than Open). If so, I could just create a macro that will replace the formulas with their results upon document creation. Can this be done?
[Note: I'm not married to this solution; it just seemed like the simplest way to protect my spreadsheet. If someone can suggest an alternative approach, I'd be obliged.]
I have a couple thoughts...
If you run a copy/paste values macro every time it really won't matter, right?
You could check if the file exists yet (has been saved), and if not
then this must be the template opened as a new workbook, maybe?
Code:
Private Sub Workbook_Open()
If Dir(ActiveWorkbook.Name) = "" Then
'run the macro?
MsgBox "I'm gonna run this macro"
End If
End Sub
You could have a cell on one of the sheets, that will never be used,
or is hidden, that will store whether or not to run the macro, and
change that when the file is opened or when the macro is ran. Then
have a macro run on open that checks that cell. (Or custom/document property)
You could populate the cells that have the today() formula only on
open and if they are already populated then don't run the macro?
I realized that there is no need for a Workbook_Create() function, as that behavior can be emulated by simply deleting the macro after it has run once (which happens when it is first created). Deleting macros is done automatically when the file is saved with a .xlsx extension. In addition, you need to prevent the macro from running when you open the template itself (while editing the it). You can hold the SHIFT key when opening it to prevent auto-run macros, but since that method isn't foolproof, I added this code at the top:
Private Sub Workbook_Open()
'If we're opening the template, don't run the macro
If Application.ActiveWorkbook.FileFormat = xlOpenXMLTemplateMacroEnabled Then
Exit Sub
End If
...
'add code here to SaveAs .xlsx, thus removing the macros, so it won't run every time.
End Sub
(Note: I didn't show my SaveAs code as it is rather messy: I wanted to suppress the default warning about losing macros, but wanted to also protect the user from inadvertantly overwriting previous file. If anyone is interested, I could post it)

Excel 2010: How to change pivot table source data without disconnecting slicers?

I have researched like mad about this, and I'm worried there isn't an answer. But maybe the really smart people on this site can help.
I have two workbooks that work together - Charts.xlsm and Data.xlsm. They are always kept together in the same folder. The Charts.xlsm obviously contains all of my charts, but they are all linked to tables in Data.xlsm for their source. I also have lots of slicers in my Charts.xlsm that are connected to the charts, and they share caches when they are connected to charts with the same data source. The two workbooks are always open at the same time so that the data source reference looks like this: 'Data.xlsm'!Table1
This all works great, until I put these workbooks on another computer (which is why I am doing this so I need to find out how to fix this).
Once the workbooks are closed, the source data references change to a specific location on my harddrive: 'C:\Folder\Data.xlsm'!Table1
If I want to manually change this back to a local reference, I have to first go through and disconnect every single slicer, refresh the tables, then reconnect every slicer. Not a viable solution for my clients.
I would use VBA to change the references every time Charts.xlsm is open, but when I tried it one of two things would happen: either the workbook produced errors that would prevent saving, or Excel would crash completely.
This is the code that works perfectly for disconnecting the slicers, but produces the 'save' error:
Sub Disconnect_Slicers()
Dim oSliceCache As SlicerCache
Dim PT As PivotTable
Dim i As Long
For Each oSliceCache In ThisWorkbook.SlicerCaches
With ActiveWorkbook.SlicerCaches(oSliceCache.Name).PivotTables
For i = .Count To 1 Step -1
.RemovePivotTable (.Item(i))
Next i
End With
Next oSliceCache
End Sub
So... I am asking the Excel/VBA geniuses out there if there is any way I can maintain a relative location for my charts when they are looking for Data.xlsm so that no matter what computer I open those workbooks on, they will always be actively linked.
Thank you SO much in advance!
If always both files are in the same folder you could possibly go this way.
A. Switch off auto 'UpdateLinks' of Chart.xlsm file. You could do this once manually or, for safety reason, always when BeforeClose event fires to avoid some possible problems:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
ThisWorkbook.UpdateLinks = xlUpdateLinksNever
End Sub
B. When you open Chart.xlsm change the link to Data.Xlsm using Workbook Open event + additionally refresh links. In this situation we check path to Chart.Xlsm file and search Data.Xlsm in the same folder. I assume that there is only one link to any other file otherwise some changes could be required:
Private Sub Workbook_Open()
'changing first and only one link to new one
Dim a
a = ActiveWorkbook.LinkSources
ThisWorkbook.ChangeLink Name:=a(1), _
NewName:=ThisWorkbook.Path & "\Data.xlsm", Type:=xlExcelLinks
'update the link
ThisWorkbook.UpdateLink Name:=a(1), Type:=xlExcelLinks
End Sub
I admit I do not consider all the risks therefore some test are required.

Linking worksheets to a master worksheet and keeping values if the master worksheet is not present? Excel 2010

I have many worksheets and at present, every 3 months I have to go through these one by one to update information in just a few cells.
It would be great if I could create a master source file containing these few cells that get updated, that all my worksheets linked to, so that I could just update this file and all my files would then update.
The only trouble I have is that I then send these worksheets out to clients by email, each client gets a specific worksheet.
This would mean that the local links to the master source file would no longer be there and I'm assuming there would be errors. Is there some way that I could link my files, as desired, to a master file, be able to update the master file and have all the other files update accordingly, but then send only a single file to a client and keep the values from the master file.
I hope this is making sense! It's quite simple what I want to do, it's just a bit tricky putting it into words.
Any help or advice would be great!
You would think there would be a simple way to do this out of the box, but it does present a problem. This not very elegant solution shows how to copy cells or ranges from a master workbook that you must have open when you are updating your client sheets. Obviously the client user won't have the master workbook and so the macro fails silently in that case. Put this code in your ThisWorkbook module of each client workbook.
Private Sub Workbook_Open()
On Error Resume Next
Dim master As Workbook
Set master = Workbooks("master.xlsm")
If master Is Nothing Then
'the client is probably opening the wbook. do nothing
Else 'copy your stuff here
With Workbooks("master.xlsm")
.Worksheets("Sheet1").Range("A1:D4").Copy _
Destination:=Worksheets("Sheet1").Range("A1:D4")
End With
End If
End Sub
I hope I understood your problem correctly. What we do is save the workbook as a different workbook with a different filename. We usually append "_sent.xlsx" to the workbook filename. Then open it, go to Data, Edit Links, and break all the links. Only the 'values' will remain in the work book. Save the workbook and you can send it out without having to worry about values that will break when they open it. And you still have your original file if you need to change anything.

Referencing Excel Sheets with JET Driver Sheets Are Duplicated with Underscores

I'm referencing an excel document with the JET database driver, which was working just fine until one day the application failed. Upon further investigation I noticed that the tabs were duplicated in some instances (all tabs actually) - i.e. Tab1$ and Tab1$_. I've researched this issue and found that it is caused by the user applying a filter. Now the filters have been removed, no special formatting, frozen panes, or print formats, but still I have these tabs duplicated. Normally I'd code around this, but since we're in a code freeze I need to come up with an Excel based solution. I want to get rid of those crazy underscored tab names / reverences. I could probably run a program to clean up the file if necessary, to send it back to the users, but I want to explore Excel options first. Thoughts? I have Excel 2010, and access to any other version as well.
The underscore "sheet" is a named range. Is it possible for you to run VBA? if so:
Dim defName As Name
''Either
With ThisWorkbook ''Entire workbook
For Each defName In .Names
MsgBox defName.Name
defName.Delete
Next
End With
''Or
With Sheets("Sheet1") ''Scoped to sheet only
For Each defName In .Names
MsgBox defName.Name
defName.Delete
Next
End With

Resources