I have created an add-in using VBA which is a workbook with calculations. The add-in has a userform to extract relevant information from access database and populates the workbook. After the data is populated, calculations are performed in Sheet1. I need to paste the worksheet "Sheet1" from the add-in worksheet to a new workbook on running the add-in macro.
When I run the add-in however, the worksheet appears to be hidden so my data is not updating. I get this error:
" Run-time error '1004': Method 'Worksheets' of object '_Global' failed".
Can someone tell me how to work with an add-in which has a worksheet where the required calculations are performed?
The intriguing part is when I load the add-in after removing it from the list of add-ins in excel, it runs perfectly. But when I re-run the macro, the worksheet becomes hidden, so the same error appears. I am fairly new to VBA so any suggestions would be appreciated!
Edit
Code:
Private Sub OptionOK_Click() 'On selecting OK from userform
Dim ws1 As Worksheet
Sheets("Sheet1").Visible = True
Set ws1 = Worksheets("Sheet1")
'User Form Validation
If Trim(Me.cboData.value) = "" Then
Me.cboData.SetFocus
MsgBox "Please complete the form"
Exit Sub
End If
'copies data to given cell in excel
ws1.Range("A1").value = Me.cboData.value
'To copy selection from "Sheet1" into new workbook
Workbooks("myaddin.xlam").Sheets(1).Copy
End Sub
I get the error on ...> Sheets("Sheet1").Visible = True.
I just realized that I had to use "ThisWorkbook" in the add-in VBA code.
Set ws1 = ThisWorkbook.Sheets ("Sheet1")
VBA code within a workbook should use "ThisWorkbook" to reference to sheets or ranges inside the add-in.
If you know what sheet it is and you have access to the add-in code just make sure it's visible before the line that throws the error.
Sheets("Sheet3").Visible = True
I suspect you have another problem though because you can still reference a hidden sheet in code.
Are you sure this line is correct:
Workbooks("myaddin.xlam").Sheets(1).Copy
Before you were referencing the name of the sheet now your referencing the position of the sheet in the workbook.
Related
I am trying to select copy everything from a "database" workbook. and paste it in the current workbook, sheet 5. The code I am using is the following.
Sub Import()
Dim DBaseWB As Workbook
Set DBaseWB = Workbooks.Open("http://collaboration.pwc.ca/team/Plant5EngineLines/Documents/Plant 5 master build plan/Database.xlsm", UpdateLinks:=False)
' set DBaseWB as the database workbook after opening it from sharepoint.
Dim DBaseSheet As Worksheet
Set DBaseSheet = DBaseWB.Sheets(1) 'DBaseSheet is referenced to sheet 1 of Database workbook.
Sheet5.Cells.Clear
DBaseSheet.UsedRange.Copy 'Copy everything from the database
Sheet5.Range("A1").PasteSpecial xlPasteAll 'Paste everything in sheet 5 of current workbook
Application.DisplayAlerts = False
DBaseWB.Close saveChanges:=False
Application.DisplayAlerts = True
End Sub
I am receiving an error when running the following code. Though, sometimes it doesn't give me an error.
Run-time error '1004': PasteSpecial method of Range class failed
I think I know why it's giving me an error at the PasteSpecial line. When I have a different cell selected in sheet 5, it gives me no error. but when I recheck, without selecting any other cell, (so it will have the pasted range selected), I get this error.
I tried using the following line between copy and pastespecial,
Sheet5.range("A1").select
it gives me the same error.
----------UPDATE----------
I first used DisplayName's solution and it worked until yesterday. But this morning, it was causing problems. It another error. Then I tried all other solutions with no luck. all gave me the same errors. I also added the workbook.worksheet to the solutions below giving me no luck. This time the error was with the copy method. I also noticed that lots of columns say #REF.
Versions I tried:
DisplayName's solution combined with thisworkbook.worksheet
Sheet5.UsedRange.Clear
With Workbooks.Open("http://collaboration.pwc.ca/team/Plant5EngineLines/Documents/Plant 5 master build plan/Database.xlsm", UpdateLinks:=False) 'open and reference your Database workbook
.Sheets(1).UsedRange.Copy Destination:=ThisWorkbook.Sheets(5).Range("A1") ' copy referenced workbook sheet 1 content and paste it to sheet 5
.Close False
End With
Gary's Student's solution combined with thisworkbook.worksheet
Dim DBaseWB As Workbook
Set DBaseWB = Workbooks.Open("http://collaboration.pwc.ca/team/Plant5EngineLines/Documents/Plant 5 master build plan/Database.xlsm", UpdateLinks:=False)
' set DBaseWB as the database workbook after opening it from sharepoint.
Dim DBaseSheet As Worksheet
Set DBaseSheet = DBaseWB.Sheets(1) 'DBaseSheet is referenced to sheet 1 of Database workbook.
Dim Destination As Worksheet
Set DestinSh = ThisWorkbook.Sheets(5)
Sheet5.Cells.Clear
DBaseSheet.UsedRange.Copy DestinSh.Range("A1").PasteSpecial 'copy database info in planning tool.
Application.DisplayAlerts = False
DBaseWB.Close saveChanges:=False
Application.DisplayAlerts = True
errors:
Run-time error '1004': Copy method of Range class failed
First insure that there are no merged cells on either worksheet, and then try:
DBaseSheet.UsedRange.Copy Sheet5.Range("A1")
you could simply go:
Sub Import()
Sheet5.UsedRange.Clear
With Workbooks.Open("http://collaboration.pwc.ca/team/Plant5EngineLines/Documents/Plant 5 master build plan/Database.xlsm", UpdateLinks:=False) 'open and reference your Database workbook
.Sheets(1).UsedRange.Copy Destination:=Sheet5.Range("A1") ' copy referenced workbook sheet 1 content and paste it to sheet 5
.Close False
End With
End Sub
I am with Bruce Wayne on this one
As you have opened a new workbook the focus is now on that workbook.
If you are trying to clear the cells in the workbook the code resides in (destination) after you have launched a new workbook and switched the focus you will need to reference the original workbook to do so, like this:
ThisWorkbook.Sheets(5).Cells.Clear
Hope this helps
I have written a macro that should copy a range of data from one workbook into another. The sheet in the other workbook is password protected, but if I copy and past manually, it allows me to do so. However, when I try and write this into my macro, it won't allow the paste.
Code is currently as follows:
Sub COPYT()
'
' COPYT Macro
Range("B2:U109").Select
Selection.Copy
Workbooks.Open Filename:= _
"_FileName_.xls"
Windows("_FileName_.xls").Activate
Range("H10").Paste
End Sub
When I run the macro as is, I get a
Run-time error '438': "Object doesn't support this property or method"
and the debugger shows that the issue is with the last line Range("H10").Paste
I can't unprotect the sheet (compliance), and I can obviously get round this by just running the macro and then CTRL+V but (as this allows me to paste....), but I would rather that this was automated. Do I need a different syntax for the paste command due to the sheet being protected?
Thanks
Paste is a method of the worksheet, not range. Try this. I have assumed sheet references but you may need to adjust.
Sub COPYT()
Dim wb As Workbook
Set wb = Workbooks.Open(Filename:="_FileName_.xls")
wb.Sheets("compliance").unprotect
ThisWorkbook.Sheets(1).Range("B2:U109").Copy wb.Sheets("compliance").Range("H10")
wb.Sheets("compliance").protect
End Sub
I have a workbook with a userform that contains a listbox that is used to populate data on a sheet.
If I have multiple workbooks open and I click from one workbook directly to the listbox on the userform in the other workbook, the ListBox_Change event fires before Activeworkbook changes to reflect the workbook that contains the userform. So when the code reaches Set EqDataSht = ActiveWorkbook.Worksheets("Equipment-Data") I get a subscript out of range error because the workbook I'm coming from doesn't contain a sheet named "Equipment-Data".
What is the best way to set the ActiveWorkbook to the parent of the userform? Thoughts I've had are setting a public variable wb = ActiveWorkbook on workbook_open or just trapping Err.Number=9 and telling the user to click on the sheet before clicking the userform. I'm sure there is something simple I am completely overlooking (VBA amateur).
Thoughts?
Instead of activeworkbook use thisworkbook which returns the workbook in which the code resides.
To make it active thisworkbook.activate should work
With Excel 2013 VBA, I am familiar with setting the value of a Workbook variable with:
Set oWBSource = Workbooks.Open(strFileToOpen) 'Works well.
In my situation though, sometimes the Workbook is already open on the desktop. I am trying to find a more elegant way to "select" that spreadsheet and then attach my variable to that workbook to operate on. The code is running on a separate "master" spreadsheet.
Currently, I am asking the user if the file is already open. If so, then:
Dim rng As Range
Set rng = Application.InputBox("Select any cell on workbook to operate on.", "Click Workbook Cell", Type:=8) 'Stops code and allows user to select a cell on an open spreadsheet
Dim sTest As String 'variable used to test.
sTest = ActiveCell.Value 'Testing to see what value code is seeing.
sTest = ActiveWorkbook.Name 'Testing to see what value code is seeing
Set oWBSource = Workbooks(ActiveWorkbook.Name) 'Works if selected Workbook is maximized and minimized.
If I run the above code and when it runs the inputbox, if I click one cell on the spreadsheet I want the code to operate on and allow the code to continue, the variables still reference the Workbook where the code resides rather than the "selected" workbook. If, while selecting one cell, I also maximize and minimize the spreadsheet (basically Activate it) and then click a cell and allow the code to continue, the active spreadsheet is set and it seems to work. I realize the cell reference is doing nothing. But the pause allows me to "Activate" the desired spreadsheet.
I am looking for a more elegant way to handle attaching my code to an already open spreadsheet. My current solution is clunky. Isn't there a way to have the user select the open spreadsheet and then have my code "act" on that spreadsheet?
I know this is a weird question. Be kind rating it please??!!
If I understand correctly what you're trying to do, just use the rng to resolve the selected Workbook:
Set oWBSource = rng.Parent.Parent
rng.Parent is the Worksheet a Range is a part of, and the .Parent of a Worksheet is its Workbook.
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.