Selection.End(xlToRight) does not work - excel

I am having a rough time getting this VBscript line to work with the excel object:
set fso=CreateObject("Scripting.FileSystemObject")
Set WShell = CreateObject("WScript.Shell")
Set objExcel = createobject("Excel.application")
objexcel.Visible = true
objexcel.Application.ScreenUpdating = True
objexcel.Workbooks.Open dir & masterFileName
objexcel.Activeworkbook.Worksheets("xActive_User_Ratio").Activate
objexcel.Range("A1").Select
objexcel.Range(Selection, Selection.End(xlToRight)).Select
when I run this code I get an error:
Object required: 'Selection'
What am I doing wrong? Any example would be very much helpful.
Please Help

It's because you are running this from outside Excel.
Use objExcel.Selection instead of just Selection. So that your code knows that Selection is associated with the Excel Application. Additionally, you'll need to define xlToRight or replace it with it's numerical value.
Better yet, I'd use with and rewrite the whole thing like so:
Set fso = CreateObject("Scripting.FileSystemObject")
Set WShell = CreateObject("WScript.Shell")
Set objexcel = CreateObject("Excel.application")
xlToRight = -4161 ' -4161 is the value of xlToRight
With objexcel
.Visible = True
.Application.ScreenUpdating = True
'using variables for workbook and worksheet to be explicit
Set wb = .Workbooks.Open(Dir & masterFileName)
Set ws = wb.Worksheets("xActive_User_Ratio")
ws.Activate
ws.Range("A1").Select
ws.Range(.Selection, .Selection.End(xlToRight)).Select
End With

Related

Getting error on closing excel macro using vbscript

Dim ObjExcel, ObjWB
Set ObjExcel = CreateObject("Excel.Application")
Set ObjWB = ObjExcel.Workbooks.Open("C: \Net_Zero_Final.xlsm",0,True)
ObjExcel.Visible = True
ObjExcel.Run "Net_Zero"
ObjExcel.application.quit ' <--- error here
Set ObjWB = Nothing
Set ObjExcel = Nothing
Getting error on the BOLD Part
Can anyone please suggest. tried everything to fix this.
I think you are trying to run a VBA Macro from a .vbs file (or any other location).
I tested the following code inside test.vbs.
Set ObjExcel = CreateObject("Excel.Application")
Set ObjWB = ObjExcel.Workbooks.Open("C:\Users\PC\Downloads\TestMacro2.xlsm", False, False)
ObjExcel.Visible = True
ObjExcel.Application.Run "TestMacro2.xlsm!TheMacro"
ObjWB.Close
Set ObjWB= Nothing
Set ObjExcel = Nothing
Where the macro inside the Excel file TestMacro2.xlsm was pretty simple
Sub TheMacro()
MsgBox ("Hello")
End Sub
It worked without any problems.
Notice that instead of trying to close Excel application (ObjExcel.Application.Quit), I just close the opened Workbook (ObjWB.Close). This will avoid the script to close Excel while the user is working in another project.
Dim ObjExcel, ObjWB
Set ObjExcel = CreateObject("Excel.Application")
Set ObjWB = ObjExcel.Workbooks.Open("C: \Net_Zero_Final.xlsm",0,True)
ObjExcel.Visible = True
ObjExcel.Run "Net_Zero"
ObjExcel.DisplayAlerts = False
ObjWB.Close = False
ObjExcel.quit
Set ObjWB = Nothing
Set ObjExcel = Nothing
It worked like this, I just passed ObjExcel.DisplayAlerts = False
it won't display any alerts and it will simply close the excel without saving it.
i hope this is right.

How to use Excel worksheet functions in VBScript?

I need to count the number of active cells in column A of Excel.
I can achieve this easily using 'worksheetfunction.countA' in Excel VBA, but unable to get the same in VBScript.
I have tried the following code:
Dim objXl , objWorkbook, objSheet ,numofactivecells
Set objXl = createobject("Excel.Application")
set objWorkbook= objXl.Workbooks.open("C:\Users\Username\Desktop\filename.xlsm")
'change filename
set objSheet = objWorkbook.Worksheets(1)
objXl.visible = true
objsheet.cells(1,1).select
numofactivecells = objsheet.WorksheetFunction.CountA(Range("A:A"))
msgbox numofactivecells
I need count of cells containing data in column A stored in variable.
I get the following error messages when I execute the code:
Microsoft VBScript compilation error: Expected identifier
Microsoft VBScript compilation error: Expected ')'
A few mistakes:
WorksheetFunction is a method of the Excel.Application object, not Worksheet.
Range can't be used by itself, it's a method of a Worksheet object.
Here's code that will work:
Dim objXl
Dim objWorkbook
Dim objSheet
Dim iActiveCells
Set objXl = CreateObject("Excel.Application")
Set objWorkbook = objXl.Workbooks.open("C:\Temp\test2.xlsx") 'change filename
Set objSheet = objWorkbook.Worksheets(1)
objXl.Visible = True
With objSheet
.Cells(1, 1).Select
iActiveCells = objXl.WorksheetFunction.CountA(.Range("A:A"))
End With
MsgBox iActiveCells

Getting AdvancedFilter method of range class failed error while coping unique data from one excel to another

I have attached the snippet of the excel file where column B is the desired Output. This similar logic worked in macro but as per our requirement, we need to build a VBScript for it. Below is my code
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oExcel = CreateObject("Excel.Application")
oExcel.Visible = True
Set inputExcelWorkbook = oExcel.Workbooks.Open("C:\...\Test1.xlsx")
Set inputExcelSheet = inputExcelWorkbook.ActiveSheet
inputExcelSheet.Range("A2:A11").AdvancedFilter _
xlFilterCopy,,inputExcelSheet.Range("B2"),True
inputExcelWorkbook.save
inputExcelWorkbook.close
Snippet of the Excel File
So, My issue was resolved when instead of using xlFilterCopy , I used "2" as per #nick.McDermaid suggestion
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oExcel = CreateObject("Excel.Application")
oExcel.Visible = True
Set inputExcelWorkbook = oExcel.Workbooks.Open("C:\...\Test1.xlsx")
Set inputExcelSheet = inputExcelWorkbook.ActiveSheet
inputExcelSheet.Range("A2:A11").AdvancedFilter _
2,,inputExcelSheet.Range("B2"),True
inputExcelWorkbook.save
inputExcelWorkbook.close

Excel application not closing from Outlook VBA function

I'm writing a sort of homegrown ticketing system for myself in Outlook VBA, and I'm using Excel to store all the persistant data. I have a function written in Outlook to get some data from the .csv and return it. This is all working fine, but after I close the workbook, quit the application, and set the app to nothing I still have an Excel process running! Here is my code:
Private Function GetNewTicketNumber() As Integer
Dim xlApp As Excel.Application
Set xlApp = New Excel.Application
With xlApp
.Visible = False
.EnableEvents = False
.DisplayAlerts = False
End With
Dim FileStr As String
Dim NumberBook As Workbook
Dim TheRange As Range
FileStr = "C:\OMGITSAPATH.csv"
Set NumberBook = Workbooks.Open(FileStr)
Set TheRange = NumberBook.Worksheets(1).Range("A1")
GetNewTicketNumber = TheRange.Value
TheRange.Value = TheRange.Value + 1
NumberBook.Save
NumberBook.Close
xlApp.Quit
With xlApp
.Visible = True
.EnableEvents = True
.DisplayAlerts = True
End With
Set xlApp = Nothing
End Function
Is there something that I'm doing wrong here? My problem is similar to the one here, but I have disabled DisplayAlerts... What can I do to fix this problem?
Try fully qualifying your references to Excel, from xl_doesnt_quit
The problem presented here is exactly what you have. This line
Range("a1").Value = Range("a1").Value + 1
leave the xl instance open
The most common cause of the problem is a 'global' reference to the automated application. Unfortunately, under some circumstances it is possible to directly refer to an entity (property/method/object) of the automated object. This reference effectively is global to the calling application. Hence, the reference remains in place as long as the calling program is active. Consequently, the operating system will not end the automated application while the caller is active.
Re-cut code below (which also uses late binding - which rules out the unqualified possibility).
Pls change you path to suit.
code
Private Function GetNewTicketNumber() As Long
Dim xlApp As Object
Dim objWB As Object
Dim objWs As Object
Dim FileStr As String
FileStr = "C:\temp\test.xlsx"
Set xlApp = CreateObject("excel.application")
With xlApp
.EnableEvents = False
.DisplayAlerts = False
End With
Set objWB = xlApp.Workbooks.Open(FileStr)
Set objWs = objWB.Sheets(1)
GetNewTicketNumber = objWs.Range("A1")
objWs.Range("A1") = objWs.Range("A1") + 1
objWB.Save
objWB.Close
Set objWB = Nothing
xlApp.Quit
Set xlApp = Nothing
End Function

Error copying cells using PasteSpecial method

Set objExcel = createObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Open("C:\Users\deven.kamlesh.jain\Desktop\abcd.xlsx")
iRow = objworkbook.sheets("Sheet1").Usedrange.rows.Count
objworkbook.Sheets("sheet1").Range("A1:C"& iRow).Copy
objExcel.visible = true
Set Obj1 = objexcel.workbooks.add()
objExcel.displayalerts = false
Obj1.saveas("Copied abcd")
Obj1.Sheets("Sheet1").Range("A1").PasteSpecial
I am trying to copy one file to another, but I get an error saying that the PasteSpecial method of the Range class failed.
The reason why PasteSpecial fails with the observed error is because the SaveAs operation clears the copied data. Move the SaveAs after the PasteSpecial and move the Copy right before the PasteSpecial. I would also recommend to put general configuration properties like Visible or DisplayAlerts right after the instantiation of the application object.
Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = True
objExcel.DisplayAlerts = False
Set objWorkbook = objExcel.Workbooks.Open _
("C:\Users\deven.kamlesh.jain\Desktop\abcd.xlsx")
Set Obj1 = objExcel.Workbooks.Add
iRow = objWorkbook.Sheets("Sheet1").UsedRange.Rows.Count
objWorkbook.Sheets("sheet1").Range("A1:C"& iRow).Copy
Obj1.Sheets("Sheet1").Range("A1").PasteSpecial
Obj1.SaveAs "Copied abcd"

Resources