Name or method not found during function call in excel - excel

I'm just getting started with VBA. I have this Subprocess which erases a given Sheet for me without displaying an error message. However, the procecss is halted with an error in the eraseSheet call.. It says that name or method don't exist. I can't figure out where? wb is a workbook (as defined above). name_imported_sheet is defined as a string. Where is there a name or method that doesn't fit?
Sub eraseSheet(ByRef sheet As Worksheet)
Application.DisplayAlerts = False
sheet.Delete
Application.DisplayAlerts = True
End Sub
Sub mainSub()
Dim wb As Workbook: Set wb = ThisWorkbook
...
If sheetExists(name_imported_sheet) Then
eraseSheet (wb.Sheets(name_imported_sheet))
End If
...
End Sub
Also is there a way to get more precise interpreter warnings?

Related

How can I Autoupdate Vlookup Links when the other workbook is open?

I have two workbooks, "Test1" and "Test2" each with a sheet inside. The code is inside the "Test1" workbook, inside the "ThisWorkbook" module.
I have a Vlookup in the sheet "Mytest1" inside the "Test1" workbook, that simply does a Vlookup with workbook "Test2" inside sheet "Mytest2". Both workbooks are password protected, I am having an issue where if I have the workbook "Test2" opened on another computer, I get a prompt to "Enter Password" for "Test2" when I open my "Test1" workbook. I need to have the Vlookups autoupdate without any prompts for password entries or the second workbook opening up.
I have posted screenshots below of both workbooks and code, alongside the code itself. Please edit or ask me any questions to clarify anything.
Private Sub Workbook_Open()
Workbooks.Open Filename:="\\FILEPATH\Databases\Test 2.xlsm", Password:="Swarf", Updatelinks:=3
ActiveWorkbook.Close SaveChanges:=True
End Sub
I dont have time to test, but will later. I'd try rewriting VLOOKUP as a wrapper round VLOOKUP like so
Function VLOOKUP_BYPASS(LookFor As Variant, Rng As Excel.Range, lngCol As Long) As Variant
Dim strWorkbook As String
strWorkbook = Rng.Parent.Parent.Name
If WORKBOOKOPEN(strWorkbook) Then
VLOOKUP_BYPASS = Application.WorksheetFunction.VLookup(LookFor, Rng, lngCol, False)
Else
VLOOKUP_BYPASS = "NA"
End If
End Function
Function WORKBOOKOPEN(strWorkbookName As String) As Boolean
WORKBOOKOPEN = False
Dim wb As Excel.Workbook
On Error GoTo eHandle
Set wb = Workbooks(strWorkbookName)
WORKBOOKOPEN = True
Exit Function
eHandle:
End Function
This is the solution I came up with, I simply made the file I am using to reference my Vlookup ("Test 2" Workbook) open up as a read-only file!
Private Sub Workbook_Open()
Workbooks.Open Filename:="\\FILEPATH \Databases\Test 2.xlsm", Password:="Swarf", Updatelinks:=3,ReadOnly:=True
ActiveWorkbook.Close SaveChanges:=False
End Sub

How do I keep an instance unique?

In my work, I've made an workbook for my entire team to use, saved a macro in personal.xlsb to open it from anywhere in another instance of excel.
Its working fine by now, but I found a problem that i couldnt solve:
When the workbook is the last workbook open(when the first instance of excel is closed and left only the instance of my workbook), the next ones I open, start in the SAME instance of the workbook. (originally in instance 2) forcing me to run the code again to separate it.
Is there any way to protect that instance specially to the workbook itself?
sorry for my bad english.
Thanks
My code is:
Sub quickwb()
Dim NewExcel As Object
Set NewExcel = New Excel.Application
With NewExcel
.DisplayAlerts = False
.Visible = True
.Workbooks.Open "workbooknameandpath"
.DisplayAlerts = True
End With
End Sub
I'm not exactly sure what your issue is but below is a function I use to open a workbook if it's not already open.
Sub QuickWB()
On Error Resume Next
Dim wb As Workbook: Set wb = GetWorkBook("pathandbook")
If Not wb Is Nothing Then
' Do something
End If
End Sub
Public Function GetWorkBook(ByVal sFullName As String, Optional ReadOnly As Boolean) As Workbook
Dim sFile As String: sFile = Dir(sFullName)
On Error Resume Next
Set GetWorkBook = Workbooks(sFile)
If GetWorkBook Is Nothing Then Set GetWorkBook = Workbooks.Open(sFullName, ReadOnly:=ReadOnly)
On Error GoTo 0
End Function

Set Print Area for All Visible Sheets in Workbook

Attempting to set the print area for all visible sheets in my workbook however I am getting an "object required" error on line PageSetup.PrintArea = "$B$2:$K$55
Any help on how to fix this error is appreciated.
Here is a copy of my full code.
Private Sub YesPrint_Click()
Dim Sheet As Worksheet
Dim CompareTool As Workbook
Dim Sheetname As String
Set CompareTool = ThisWorkbook
With Application
.DisplayAlerts = False
.ScreenUpdating = False
End With
For Each Sheet In CompareTool.Worksheets
If Sheet.Visible = True Then
Sheet.Activate
With ActiveSheet.Name
Range("B2:K55").Select
PageSetup.PrintArea = "$B$2:$K$55"
End With
End If
Next Sheet
End Sub
It looks like there are two issues with the code provided.
First, There is a syntax error in the with statement. Each property accessed inside the with statement should have a dot before it.
Second, the ActiveSheet.Name is not the object that includes the Range and PageSetup Method/object. It's the ActiveSheet object.
The corrected code should look like:
With ActiveSheet
.Range("B2:K55").Select
.PageSetup.PrintArea = "$B$2:$K$55"
End With
The MSDN article for PageSetup provides an example as well:
https://msdn.microsoft.com/en-us/library/office/ff196103.aspx

What is my error is setting this up? This Sub runs perfectly in a different workbook

A "Runtime Error 9, Subscript Out of Range" is received on the Set wb1 line. This similar structure runs fine in a different workbook without error.
My goal is to copy a cell from the Source document into te Destination document.
Sub CopySheetsl()
Dim wb As Workbook, wb1 As Workbook
Dim LastRow As Long
Set wb = Workbooks("C:\Test\DST.xlsm")
Set wb1 = Workbooks.Open("C:\Test\Source.xlsx")
wb1.Sheets("SourceNamedSheet").Range("A1") = wb.Sheets("DestinationNamedSheet").Range("A1").Value
wb1.Close
End Sub
If DST.xlsm is open already then
Set wb = Workbooks("DST.xlsm")
ElseIf you need to open DST.xlsm
Set wb1 = Workbooks.Open("C:\Test\DST.xlsm")
for a more robust approach to workbooks handling you may want to use the following GetOrSetWorkbook() function:
Option Explicit
Function GetOrSetWorkbook(wbName As String) As Workbook
On Error Resume Next
Set GetOrSetWorkbook = Workbooks(GetNameOnly(wbName)) '<--| check if a workbook with given name is already open
If GetOrSetWorkbook Is Nothing Then Set GetOrSetWorkbook = Workbooks.Open(wbName) '<--| if no workbook open with given name then try opening it with full given path
End Function
which uses the following helper GetNameOnly() function:
Function GetNameOnly(pathStrng As String) As String
Dim iSlash As Long
iSlash = InStrRev(pathStrng, "\")
If iSlash > 0 Then
GetNameOnly = Mid(pathStrng, iSlash + 1, Len(pathStrng))
Else
GetNameOnly = pathStrng
End If
End Function
so that a possible use of it could be:
Option Explicit
Sub CopySheetsl()
Dim wb As Workbook, wb1 As Workbook
Dim LastRow As Long
Set wb = GetOrSetWorkbook("C:\Test\DST.xlsm") '<--| try getting "C:\Test\DST.xlsm"
If wb Is Nothing Then '<--| if unsuccessful...
'... code to handle C:\Test\DST.xlsm workbook error, like:
MsgBox "Couldn't find 'C:\Test\DST.xlsm' !", vbCritical + vbOKOnly
End If
Set wb1 = GetOrSetWorkbook("C:\Test\Source.xlsx") '<--| try getting "C:\Test\Source.xlsx
If wb Is Nothing Then '<--| if unsuccessful...
'... code to handle 'C:\Test\Source.xlsx' workbook error, like:
MsgBox "Couldn't find 'C:\Test\Source.xlsx'!", vbCritical + vbOKOnly
End If
'here goes rest of the code to be executed once all necessary workbooks have been properly set
wb1.Sheets("SourceNamedSheet").Range("A1") = wb.Sheets("DestinationNamedSheet").Range("A1").Value
wb1.Close
End Sub
of course a very similar GetOrSet approach can be assumed with worksheets, too...

excel 2007 Workbook_open not working

I am trying to clear Print Area And Autofilter when excel opens:
Am total novice in Excel vba so Assmebled the followingcode from googling around
This code I have put in ThisWorkbook of Personal.xlsb in the XLstart folder and ofcourse the macro security has been set to enable all macros
Option Explicit
Public WithEvents xlApp As Excel.Application
Private Sub Workbook_Open()
Set xlApp = Application
End Sub
Private Sub Workbook_Close()
Set xlApp = Nothing
End Sub
Private Sub xlApp_WorkbookOpen(ByVal Wb As Workbook)
Application.EnableEvents = False
Call ClrPrntArea
Application.EnableEvents = True
End Sub
Here is the ClrPrntArea
Sub ClrPrntArea()
Dim ws As Object
For i = 1 To ActiveWorkbook.Worksheets.count
With Worksheets(i)
.PageSetup.PrintArea = ""
.PageSetup.FitToPagesWide = 1
End With
Next
End Sub
I will also be putting another macro call to module in personal xlsb for resetting the autofiter once above starts working..Any inputs will be really helpfull
in PERSONAL.xlsb, module ThisWorkbook, try the below; it's nearly the same code as in your request, with some modif's:
application object declared Private
event routine uses the local WB object variable handed over as parameter, instead of the ActiveWorkbook object
replaced For ... Next by For Each ... Next and working with local object variables
trap processing of PERSONAL.xlsb itself
Once you're happy remove all the MsgBox statements (and the Else), they are just to show what is happening and when.
Private WithEvents Excel_App As Excel.Application
' runs when Excel_App encounters a Workbook_Open() event
Private Sub Excel_App_WorkbookOpen(ByVal WB As Workbook)
Dim WS As Worksheet
If WB.Name <> "PERSONAL.xlsb" Then
MsgBox "PERSONAL.xlsb: Excel_App_WorkbookOpen(): " & WB.Name
For Each WS In WB.Worksheets
WS.PageSetup.PrintArea = ""
WS.PageSetup.FitToPagesWide = 1
If WS.FilterMode Then
WS.ShowAllData
End If
Next
Else
MsgBox "PERSONAL.xlsb: Excel_App_WorkbookOpen(): myself"
End If
End Sub
' runs when PERSONAL.xlsb is opened
' assign current Excel application to object variable Excel_App
Private Sub Workbook_Open()
MsgBox "PERSONAL.xlsb: Workbook_Open()"
Set Excel_App = Application
End Sub
Note:
When the event handler doesn't start when you double-click an Excel file (e.g. on your desktop), close all Excel applications and inspect the task manager for additional orphaned Excel processes which need to be killed. It happened to me while playing around with this code

Resources