VBA error 1004 on Paste from CSV file with CSV file opened first - vba

I'm a beginner on Excel VBA and working on a little tool at work. We use a tool to measure our NPS-score, and therefore I can download all our comments as CSV file. I created a VBA, to paste all these comments in an worksheet and convert it to columns and do the math.
If I open my Excel file first, then download the comments, manually copy the comments and run the script, it works fine. But if I download the comments at first, then open my Excel file and run the script, it throws a 1004 error, Method Paste of class Worksheet failed.
I've tried a lot of answers on here, but none of them seems to work. This is the part of script in the beginning to paste the comments:
Application.ScreenUpdating = False
Dim wb As Workbook
Set wb = ThisWorkbook
wb.Activate
Sheets("Hulpblad").Visible = -1
Sheets("Hulpblad").Paste Destination:=Sheets("Hulpblad").Range("A1")
Application.CutCopyMode = False
I also tried to record a macro and use the Excel generated code, but all the answers give the same error.
Next to that, by some users of the script it doesn't matter in which order you open the files, it always throws the 1004 error. Can somebody please help :-)

1004 is an error, that is generated, when you try to change a protected sheet.
In your code, you are trying to paste a value, which cannot be done, because the sheet is protected.
Try to unprotect it first, before pasting the value and then protect it again.

I think there is no copy method in your statement. You are trying to paste the content directly

Related

How to run a macro from a different workbook in a shared network?

So, I've done a lot of research on this and my code isn't working still. As per the title, the problem is this:
I pull a data report off of a website, this report is downloaded as an .xlsx file. I created a macro on the ribbon so I when I click it, it will then open another workbook and run that macro. The code I'm using is as below:
Option Explicit
Sub NotHardAtAll()
Dim ws As Worksheet,
Dim wb As Workbook
Set wb = ActiveWorkbook
Set ws = ActiveSheet
Workbooks.Open Filename:="C:\Users\a0c27n\Desktop\Projects\incident_extended_report1.xlsm"
'With Sheets("Sheet4").Activate '*Not sure if this is enter code here
necessary...at all*
Application.Run "!ADDHMKRFID"
'End With
End Sub
I've tried putting the path before the macro (i.e. Application.Run"'incident_extended_report1.xlsm!ADDHMKRFID") but it doesn't work either*
I'm aware, at least form the research I've done, that I should be able to just use the 'Application.Run' Method, however I couldn't get it to access the correct sheet.
When I run the Macro, it pulls a Run-time error '1004' error, a '400', or the it pulls the most is: "Cannot run the macro '!ADDHMKRFID'. The macro may not be available in this workbook or all macros may be disable."
The file that I'm trying to pull the macro from is below:
Workbook name: incident_extended_report1.xlsm
Worksheet name: Sheet4 (TEST MACRO)
Macro Name:
Sub ADDHMKRFID()
End Sub
I understand that the C:\ is not a shared network, the one I will be working out of will be the S:\, however I'm not sure how much information I can post due to confidentiality.
Please ask for any clarification or questions you may have. I've been stuck for a bit and am not sure what I'm doing wrong. Thanks in advance!
The string you need to pass to Application.Run depends on whether the workbook containing the macro is active, and if it isn't, the filename of the macro-containing workbook (IE: what's in the workbook.Name property).
if the macro is supposed to be run while the data report workbook is active, you want:
dim wb_data as Workbook: set wb_data = ActiveWorkbook
dim ws_data as Worksheet: set ws_data = ActiveSheet
dim wb_macro as Workbook
set wb_macro = Workbooks.Open(Filename:="C:\Users\a0c27n\Desktop\Projects\incident_extended_report1.xlsm")
ws_data.Activate
Application.Run wb_macro.Name & "!ADDHMKRFID"
This will guarantee that the correct string is supplied, even if you change the name of the macro file.
Otherwise, if the macro workbook is supposed to be active, skip activating the data worksheet, as the last opened workbook will be active by default, then use "ADDHMKRFID" as your string. Note that the "!" is missing. You need that only if you are specifying a macro in another workbook. It's the same kind of notation used when referring to data in other worksheets.
First of all, I solved my own problem. I would, however, be grateful if someone might explain to me why it worked the way it did.
I saved the original macro on the shared network, but I had to save it as a module (in this case Module1). I also saved the 2nd macro (to run the original one) in a different workbook (though it shouldn't matter, as long it is not a .xlsx file).
The Code I wrote was:
Sub Test() 'Name doesn't matter
Application.Run "'S:\xxxx\xxxx\xxxx\incident_extended_report.xlsm'!module1.ADDHMKRFID"
End Sub
Then I saved this macro to the ribbon so I could run it on the data report.xlsx file I have to download. Now, anytime I want to run the original macro, I just click the Test Macro, and it'll run the other one!
I'm guessing if you want to close the other workbook that you opened, you can just add a
Workbooks (“S:\xxxx\xxxx\xxxx\incident_extended_report.xlsm").Close Savechanges:=False
Good Luck!

Excel Macro to Copy Data From a Worksheet Yields the Query Definition instead of Data

As the title says, I'm trying to copy data from a particular file into a new tab in my workbook, but the macro only copies the query definition that (I assume) was used to retrieve the data itself.
I've already removed any connections to external sources, but that didn't fix the problem. Here's a screenshot of the paste results:
Here's the portion of my code I am using to paste:
Set DestWS = wkbDest.Sheets(Curr_Input_File)
If Len(wkbDest.Sheets(Curr_Input_File).Range("A1").Value) > 0 Then
wkbDest.Sheets(Curr_Input_File).UsedRange.ClearContents
SheetToCopy.UsedRange.Copy
DestWS.Range("A1").PasteSpecial xlPasteAll
Application.CutCopyMode = False
Else
SheetToCopy.UsedRange.Copy
DestWS.Range("A1").PasteSpecial xlPasteAll
Application.CutCopyMode = False
End If
I know the copy/paste loop works since all the other files are copied/pasted without issue. However, for this file only, I'm having the issue as described above. How can I code it to paste the actual data instead of the query? FWIW, I can't see the query anywhere in the source data file.
Okay, the answer is obvious but I figured it out. There was a hidden sheet with the SQL query definition. My copy code was just copying the first tab in the workbook by default SheetToCopy = wkbSource.Sheets(1), thus it was grabbing the SQL query.
I didn't even know you could hide sheets. It's done by going to the Home tab, under the 'Cells' section click Format-->Hide & Unhide-->Unhide Sheet.

What is the command in Excel VBA to delete a specific macro?

I have an Excel Macro Template. I run a macro on it and it saves the new spreadsheet as an xlsx instead.
I'm just wondering if there's a simple command for VBA that just deletes a macro by name.
I've researched this a lot, and there are basically two answers which don't really fit.
The first is to not have macros in the workbook I'm saving and to just run macros from one workbook to another. (I don't want to do this for a few reasons, but simplicity is the main reason.)
The second is a VBA script that strips ALL VBA and connections from the workbook. (I don't need it to do that much, and I'd rather just delete the one macro I have.)
So, what I'm looking for is just something like this:
Delete.Macro("Import") 'This command deletes the macro in this workbook named "Import".
try
Sub main()
Application.DisplayAlerts = False
Workbooks("PutWorkBookName").SaveAs FileFormat:=xlOpenXMLWorkbook
Application.DisplayAlerts = True
End Sub
Fun thought, use VBA to remove VBA... but seems more work than necessary.
What about activeworkbook.saveas "filename.xlsx", xlExcel12?
As I am sure you are aware, an .xlsx doesn't have any code, so if you set warnings to false, etc., it will just save the file and delete the code.
Now, if I misread, and you want to delete only some of your code, leave the rest, and save as xlsx... you are out of luck.

VBA macro crashing Excel 2010

I have an Excel 2010 spreadsheet that I use to maintain tests for an automated test application written with Selenium WebDriver. The spreadsheet has 6 ActiveX buttons that Upload (to a database), Import (from another sheet), Clear All Sheets, Export (to another sheet), Merge (two sheets), and Restore (a test suite from the database). My problem seem to stem from the macro that runs when the Import button is selected
The macro takes two sheets, source and destination, clears the destination sheet and copies the cells and data from the source workbook to the corresponding sheet in the destination workbook. From there, it can either be uploaded to the database or run from the sheet by the testing app
A couple weeks ago when selecting the Import button Excel crashed. I looked on the web and found a solution that worked. It involved disabling all macros, shutting down trust documents and trusted locations, recompiling, and reopening the workbook. This worked for a couple weeks until the same thing happened again this past weekend and the fix did not work anymore.
Here is the code I have:
numSheets = wrkBook.Sheets.Count
appName = wrkSheet.Cells(2, 3).Value
bkPath = Application.GetOpenFilename("Excel Macro-Enabled Workbook (*.xlsm),*.xlsm")
If bkPath <> "False" Then
Call Clear_All(wrkBook, wrkSheet)
'get the number of sheets in the source workbook
destCnt = wrkBook.Sheets.Count
Set importBook = Workbooks.Open(bkPath)
For a = 1 To destCnt
thsShtName = wrkBook.Worksheets(a).Name
'activate the source workbook
importBook.Activate
'get the numer of sheets in the source workbook
srcCnt = importBook.Sheets.Count
'loop through each sheet checking title
For b = 1 To srcCnt
'get the name of the 'a' sheet
**srcSheetName = importBook.Worksheets(b).Name**
'do more stuff with the source sheet
Next b
Next a
End If
The code is crashing on the following line
srcSheetName = importBook.Worksheets(b).Name
Here are a few things that I have noticed. Once the importBook variable is set, I cannot expand the variable in a watch window even if the code has not reached the offending line. However, the macro will successfully get the srcCount (number of sheet on the source sheet). Once the macro gets to the offending line it crashes consistently with a message stating simply that Excel has encountered an error and needs to close . No information, Excel closes, and sometimes restarts to a blank workbook
OK...I figured this out myself after heeding Siddarth Rout's suggestion about a corrupt spreadsheet. I took the steps that I described above
So here's what I did.
I copied one of the other spreadsheets and cleared it. As soon as I ran the Import macro, it passed through the line where it was failing before, so a corruption seems to have been the issue. I then stepped through the original code and rewrote the Import macro to simplify the code in it. It now seems to be working just fine
Thanks for the suggestions!!

Excel error when saving as XML spreadsheet

I encountered a strange error in an Excel 2007 add-in that seems to be reproducible with a few lines of macro code (Update: even without code, see below).
Open a new workbook and add the following code to the first worksheet.
Private Sub Worksheet_Change(ByVal Target As Range)
Application.DisplayAlerts = False ' Suppress macro and overwrite warnings
ActiveWorkbook.SaveAs "test.xml", XlFileFormat.xlXMLSpreadsheet
End Sub
Now each change will save the workbook as an XML spreadsheet file.
However, when I open a second instance of Excel and copy a single cell from there to the auto-saving workbook Excel 2007 crashes. (I have also seen an RPC_E_SERVERFAULT error in a comparable situation.) In Excel 2010 the file is saved as expected.
Any ideas what might be the root cause for this behaviour and how to avoid it?
UPDATE
Seems like it's even worse: If I copy data from one Excel (2007) instance to another and save the target workbook as XML spreadsheet Excel crashes. I tried this on two machines, so is this a known bug?