Calling a variable in VBA Formula - vba

I am very new in VBA and have the following problem:
I have a code like this
IE.Document.getElementsByName("selectedNotifiers")(tparty).Checked = True
The purpose is to check a checkbox on a website. The code works. Now I want to use tparty as a variable based on a excel cell.
I tired this
Dim tparty As String
tparty = ThisWorkbook.Sheets("Sheet1").Value(Range("q" & rn))
IE.Document.getElementsByName("selectedNotifiers")(tparty).Checked = True
but is causing the IE crashing. Any help plz.
Latest status. even Tried this
Dim tparty As String
tparty = "SFD0087G"
IE.Document.getElementsByName("selectedNotifiers")(tparty).Checked = True
no luck.

Related

Excel VSTO - Hide multiple columns lambda function issue - VB.NET

I've been trying to figure out what is wrong with my code below, but no success so far.
My simple task is to hide multiple Excel columns using VSTO / VB.NET.
This works:
Dim app As Excel.Application = Globals.ThisAddIn.Application
Dim act_sheet As Excel.Worksheet = app.ActiveSheet
act_sheet.Range("A:A").EntireColumn.Hidden = True
act_sheet.Range("B:B").EntireColumn.Hidden = True
This doesn't work:
Dim app As Excel.Application = Globals.ThisAddIn.Application
Dim act_sheet As Excel.Worksheet = app.ActiveSheet
Dim base_hide As New List(Of String)({"A:A", "B:B"})
base_hide.ForEach(Function(x) act_sheet.Range(x).EntireColumn.Hidden = True)
I get no errors compiling it, the string address is taken correctly. Any idea?
Thank you,
C
Found what caused the issue. Running a ForEach loop as lambda with Function() would expect to return something. My code inside the function never run. Changing it to Sub() works as expected.
base_hide.ForEach(Sub(x) act_sheet.Range(x).EntireColumn.Hidden = True)

Multi criteria filter in VBA (Not equal to)

I am using the below code in Blue prism for filtering in excel for multi criteria.
But i am not able to filter multi criteria for Not equal to scenario.
Dim wb As Object
Dim excel as Object
Dim range as Object
Try
wb = GetWorkbook(Handle, Workbook)
excel = wb.Application
range = excel.Range(FRange)
Dim listOfValues as Array
listOfValues = Split(FCriteria,";")
wb.worksheets(Worksheet).select
range.select
range.Autofilter(FCol,listOfValues,7)
Success = True
Catch e As Exception
Success = False
Message = e.Message
Finally
wb = Nothing
End Try
Please help me tweaking the script
I'm almost sure that there is no filter option to set a "negative list". You can specify either a (positive) list of values (this is what your code does so far, for this you have to set the 7 as third parameter), or you can give a maximum of 2 individual criteria (in Excel, choose "Custom Filter" to set them.
You should play with the filter directly in Excel and try to set it like you want. Once you are satisfied with it, clear the filter, record a macro and repeat the filtering. Go to the VBA editor and see what's in there. It is straightforward to translate this into C# code.
But:
It's not possible to set any filtering by code (neither C# nor VBA) that you cannot set via the Excel GUI
I would question what you are trying to do. Since you are using Blue Prism, you should be trying to access the underlying data in a BP Collection(VB DataTable), rather than applying a filter, which is a visual tool for humans to further play with the interface. The robot will still have to do something with the filtered data, and it far easier to write code to proceed with data during the loop.
Otherwise use the Filter Collection Page of the 'Utilities - Collection Manipulation' VBO to get a filtered collection.
Also you are using VBA Split function, when you should use Split in VB as a method of the String.
Try this for a new page in the 'Utilities - Collection Manipulation' VBO(untested):
Dim NewRow As DataRow
Collection_Out = Collection_In.Clone
Dim Select_Concat As String
Select_Concat = "NOT(" & fieldName & " = '" & [String].Join("' OR " & fieldName & " = '", FCriteria.Split(";"c)) & "')"
For Each parentRow As DataRow In Collection_In.Select(Select_Concat)
NewRow = Collection_Out.NewRow
For Each c As DataColumn In NewRow.Table.Columns
NewRow(c.ColumnName) = parentRow(c.ColumnName)
Next c
Collection_Out.Rows.Add(NewRow)
Next parentRow
NewRow = Nothing
Collection_In = Nothing
Inputs: Collection_In(Collection), fieldName(Text), FCriteria(Text)
Outputs: Collection_Out(Collection)
You first need to get the entire range into an unfiltered Collection(which will be your Collection_In to this page, and then get the filtered Collection out....

VBA replace logo in Excel file

I have a little strange question. I used to have few reports worked upon daily.
All these are in Excel and had a logo of the company in all the sheets of each file.
However, now the company name is changed and hence a new logo needs to be replaced in place of the existing. Wanted to check if this replacement can be done with VBA.
I tried with the application.shapes method. But, was confused to proceed further.
Try this....
Sub ChangePicture(strNewPath As String)
Dim oOld As Picture
Dim oNew As Picture
Set oOld = ActiveSheet.Pictures(1)
Set oNew = ActiveSheet.Pictures.Insert(strNewPath)
oNew.Left = oOld.Left
oNew.Top = oOld.Top
oNew.Width = oOld.Width
oNew.Height = oOld.Height
oOld.Delete
End Sub

VBA function result type mismatch

I am trying to run as a macro a custom Excel function XpathOnUrl from the add-in called SeoTools by Niels Bosma. The function runs fine and it seems that I am able to store its result in a variable. This variable can then be correctly output to an Excel cell, but when I try to look for a string in it in the next part of the macro, I get the error Run-time error '13': Type mismatch. From what I understand from here, the function returns an array, but when I try to access it as the first item of the array, I get the same error. I tried to convert the variable into a string with CStr, but no luck there either. What am I missing?
Here's the problematic part of the code:
WebSite = Sheet1.Range("A1")
contactPage = Application.Run("XPathOnUrl", WebSite, "//a[contains(translate(#href, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'),""contact"")]", "href")
MsgBox TypeName(contactPage) 'trying to find out the data type results in 'Error'
If Left(contactPage(0), 4) = "http" Then
Sheet1.Range("B1").Value = contactPage
ElseIf InStr(contactPage, "/") = 1 Then
Sheet1.Range("B1").Value = WebSite & contactPage
End If
Just to make it clear: the problem starts only with conditional statements. If I assign the value of the variable directly to a cell like this Sheet1.Range("B1").Value = contactPage, it outputs the correct result.
Here's a easy workaround:
Make XpathURL spit its return to a range. Then, use Range.value to assign the return to a contactpage and clear the range using .Clearcontents property. I think Application.Run is not letting XpathURL return get to contactpage.
Edit: Added the comment below:
Sheet1.Range("B1").Value = Application.Run("XPathOnUrl", WebSite, "//a[contains(translate(#href, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'),""contact"")]", "href")
contactPage = Sheet1.Range("B1").Value
Did you try the Formula and FormulaR1C1 functions? Just my assumptions.
Try and let us know if it helped.
Sheet1.Range("B1").Formula = WebSite & contactPage
or
Sheet1.Range("B1").FormulaR1C1 = WebSite & contactPage
What does the MsgBox TypeName(contactPage) display?

Remove sas content from Excel with VBA (MS Office Add-in)

I've figured out how to use the MS Office Add-in to run a SAS Stored Process from within Excel (using an input stream) to obtain outputs.
Everytime I run this, new columns are inserted to accommodate the new output. Also, additional content is added to the workbook.
My understanding of the SAS Add-in object model is poor, so I was hoping someone could help me:
1) Remove the existing content with VBA before running the process again; or
2) Write VBA code to refresh existing content only (within the same output range).
The code I'm using is:
Sub sasTests()
Application.ScreenUpdating = False
Dim sas As SASExcelAddIn
Set sas = Application.COMAddIns.Item("SAS.ExcelAddIn").Object
Dim inputStream As SASRanges
Set inputStream = New SASRanges
inputStream.Add "Prompts", Worksheets("sasInput").Range("sasInput")
sas.InsertStoredProcess "/Shared Data/C139/sasTests", _
Worksheets("sasOutput").Range("A1"), , , inputStream
End Sub
Many thanks!
PS. If anyone has any handy references for the SAS Add-in for MS Office, please provide links
You could clear the range containing the existing content with the .Clear method. If this range were in columns M:P, you could use the following line:
sheetNameHere.Range("M:P").Clear
If you added that line after setting
Application.ScreenUpdating = False
I believe this would imitate a "refresh" of the data.
This will remove all old components from the workbook. I am not sure how to refresh all but assuming its similar:
Sub DeleteSasContent()Dim sas As SASExcelAddIn
Set sas = Application.COMAddIns.Item("sas.exceladdin").Object
Dim stpList As SASStoredProcesses
Dim dataList As SASDataViews
Dim pivotList As SASPivotTables
Dim reportList As SASReports
Set stpList = sas.GetStoredProcesses(ThisWorkbook)
Set dataList = sas.GetDataViews(ThisWorkbook)
Set pivotList = sas.GetPivotTables(ThisWorkbook)
Set reportList = sas.GetReports(ThisWorkbook)
For i = 1 To stpList.Count
stpList.Item(i).Delete
Next i
For i = 1 To dataList.Count
dataList.Item(i).Delete
Next i
For i = 1 To pivotList.Count
pivotList.Item(i).Delete
Next i
For i = 1 To reportList.Count
reportList.Item(i).Delete
Next i
End Sub
See the SAS help here http://support.sas.com/kb/45/606.html