Excel VBA - Can I manipulate data on another workbook? - vba

Background:
Every month I need to run a report. However, before I can do this, I must export some data in to excel. Once the data is in excel, I have to alter some columns and get the format correct before it is appended to the end of another document to do some analysis on.
What I would like:
I would like to have the document that I append the data to open. I will then export the data in to excel from my program (excel just opens with the data and it is not saved anywhere) and from my larger document, run a VBA script that will alter the data on the other workbook (Book1) so that it can be copied over to the analysis document when in the correct format.
What I have so far:
I have started basic. So far all I am trying to do is set all the cells to the correct height to make it easier to read. However, when I run this code, I get:
Run-time error '9':
Subscript out of range
The code I have so far is:
Sub Data_Ready_For_Transfer()
' Format all cell heights to 15
With Workbooks("Book1.xlsm").Worksheets("Sheet1")
Cells.RowHeight = 15
End With
End Sub
It appears to be having issues with the With Workbooks("Book1.xlsm").Worksheets("Sheet1") part of the code. I have also tried With Workbooks("Book1").Worksheets("Sheet1") and I have tried this with the open, unsaved document and a saved version of the workbook.
Am I missing something obvious?

As follow up from comments, workbook Book1 was opened in another instance of Application object.
In that case this code should work (for already opened workbook):
Sub Data_Ready_For_Transfer()
Dim wb As Workbook
Set wb = GetObject("Book1")
'if we get workbook instance then
If Not wb Is Nothing Then
With wb.Worksheets("Sheet1")
.Cells.RowHeight = 15
End With
End If
End Sub
One more issue, I've changed Cells.RowHeight = 15 to .Cells.RowHeight = 15 to specify, that Cells belongs to workbook Book1 sheet Sheet1.

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!

Why are two Excel sheets opening when I try to create one in VBscript?

I have written a program in VBscript to create an excel sheet. The program works fine, and when i click on the Excel sheet to open it, the sheet i created is opened as well as a new excel "Book1". Why is this happening? How can I stop it so that only the sheet i created will open?
Code is given below:
Set objExcel = CreateObject("Excel.Application") 'Bind to the Excel object
objExcel.Workbooks.Add 'Create a new workbook.
Sheet = 1 'Select the first sheet
Set objSheet = objExcel.ActiveWorkbook.Worksheets(Sheet) 'Bind to worksheet.
objSheet.Name = "InfoBook" 'Name the worksheet
strExcelPath = "c:\scripts\InfoBook.xlsx" 'Set the save location
objSheet.Range("A1:C1").Font.Bold = True
objExcel.Columns(3).AutoFit()
objSheet.Cells(1, 1).Value = "Date" 'Row 1 Column 1 (A)
objSheet.Cells(1, 2).Value = "Time" 'Row 1 Column 2 (B)
objSheet.Cells(1, 3).Value = "Category" 'Row 1 Column 3 (C)
objExcel.ActiveWorkbook.SaveAs strExcelPath
objExcel.ActiveWorkbook.Close
objExcel.Application.Quit
Have you tried rebooting your computer and running the code again?
The additional workbook may be left over from an earlier, failed, attempt at running the code. Although I cannot replicate the error using just your posted code, I can replicate it by performing
Set objExcel = CreateObject("Excel.Application") 'Bind to the Excel object
objExcel.Workbooks.Add 'Create a new workbook.
and then running your script.
Because the first script creates a workbook but doesn't close the Excel application, when you open the workbook created by your script, the still running instance of Excel is opened (which still contains Book1) and your workbook is also opened.
Rebooting your computer will get rid of the running instance of Excel - or you can just terminate it using Task Manager.
I figured out what the problem is. The line
objSheet.Name = "InfoBook"
creates a workbook with that name. Then when we specify the path for the workbook, we are again creating a workbook of that name in the following line
strExcelPath = "c:\scripts\InfoBook.xlsx"
so the trick is to only name the excel workbook in the path, so it'll only be opened once, and saved where we want it.
Thanks to everyone who spent time on this little predicament of mine. Hope it helped you guys too.

Excel Vba - Calling a sheet with a variable sheet name

I've been coding in VBA for some time, but this one has really stumped me.
I'm creating a workbook which creates technical certificates for machines. We have varying templates depending on the machine type and I am attempting to get my code to select the correct sheet from a user input and then populate the sheet. FYI these template sheets will be hidden and the user can only interact with the userforms.
Heres the code that is failing:
Machine = MachineType.Text '<-- input from userform, for example Machine = "Vertex 251"
Set wsCopy = ThisWorkbook.Sheets(Machine) '<--- select that machine's sheet
wsCopy.Copy '<--Run time Error 1004: Method copy of object_worksheet failed
I've tried numerous different types including just sheets(machine).copy or
Sheets(machine).activate
Activesheet.copy
but nothing has worked so far - I cannot tell if I am doing something fundamentally wrong.
Any help would be be appreciated.
Cheers.
You must unhide the sheet before copying it (at least to a new workbook as lturner notes) - you can then re-hide it
Dim shtTemplate as Worksheet, sheetWasHidden As Boolean
Set shtTemplate = ThisWorkbook.Sheets(Machine)
'handle the case where the sheet to be copied is Hidden
If shtTemplate.Visible = xlSheetHidden Then
shtTemplate.Visible = xlSheetVisible
sheetWasHidden = True
End If
shtTemplate.Copy
If sheetWasHidden Then shtTemplate.Visible = xlSheetHidden 're-hide if needed
When you have the worksheet object and use the Copy method, Excel seems to be making assumptions (or not) about where you want to put the new sheet. I pretty much always use the After option to define where the new sheet should go.
Option Explicit
Sub test()
Dim wsCopy As Worksheet
Set wsCopy = ActiveSheet
wsCopy.Copy After:=wsCopy
End Sub

Saving/restoring clipboard in Excel VBA

I have an Excel workbook with many sheets. Most sheets have some code in the Worksheet_Activate() subroutine.For some reason this code causes the contents of the clipboard to be lost - extremely annoying, as spreadsheet user cannot copy and paste between sheets.
In an attempt to fix this I have added the following lines at the beginning of the Worksheet_Activate code:
Dim dClipBoard As MsForms.DataObject
On Error Resume Next
Set dClipBoard = New MsForms.DataObject
dClipBoard.GetFromClipboard
And the following lines just before exit of the Worksheet_Activate code:
On Error Resume Next
dClipBoard.PutInClipboard
Unfortunately, it doesn't work. My clipboard is still lost when I move from sheet to sheet.
I can imagine 2 different workarounds for this case.
1- If possible change the code in the worksheets. Where ever the .copy command is used replace it with codes like these:
range("Some Range") = range("Some other range")
2- If changing the code is not possible (using the approach you are currently using) instead of storing the data to an MsForms.DataObject you could copy them to some auxiliary sheet, And retrieve the data from the worksheet before the exit worksheet event triggers.

Excel 2010 VBA running in all opened files

I have one excel file (*.xlsm) with VBA code on first sheet:
Private Sub Worksheet_Calculate()
ActiveSheet.ChartObjects("Podtlak").Chart.Axes(xlCategory, xlPrimary).MaximumScale = Range("AV79").Value
End Sub
And second excel file with macro that is changing value in cell in first excel (it is automaticaly recalculated) and then copy value of new result from first excel and paste it to second excel file.
Problem is: when macro is going to second excel and paste value, worksheet is recalculated and code from first excel is calling, but it stops with error because in second excel it cant find chart object "Podtlak".
How to set worksheet_calculate() to run only for file whitch one is it written?
Try specifying the workbook:
ThisWorkbook.ActiveSheet.ChartObjects("Podtlak").Chart.Axes(xlCategory, xlPrimary).MaximumScale = Range("AV79").Value
Or specifying the worksheet and workbook:
ThisWorkbook.Worksheets("yourWorksheetName").ChartObjects("Podtlak").Chart.Axes(xlCategory, xlPrimary).MaximumScale = Range("AV79").Value
I like the 2nd option best as you are clearly specifying the worksheet and workbook that this function runs on. Anytime you use ActiveSheet or ActiveWorkbook, you are relying on the fact that your intended worksheet is active when the code runs.