I would like to use a CountIfs-function with two criterias. My programm should go through two different columns and compare the cells.
I used this code:
WorksheetFunction.CountIfs(Data!E:E;"=Open";Daten!Q:Q;"=company")
Could you please tell me if it is possible to use the CountIfs-function like that? Because my compiler is dropping a syntax-error.
Thanks a lot!
Is this what you are trying?
Option Explicit
Sub Sample()
Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets("Date")
Dim wksFn As WorksheetFunction
Set wksFn = Application.WorksheetFunction
Debug.Print wksFn.CountIfs(ws.Columns(5), "Open", ws.Columns(17), "company")
End Sub
Related
I'm trying to ensure the UsedRange is accurate, having experienced it being substantially different to what is actually used in a worksheet. Having .UsedRange is meant to reset the used range to what is in use. I'm finding it doesn't unless structured in a very particular way.
ActiveSheet.UsedRange ' Works OK
Dim My_Sheet as Worksheet
Set My_Sheet = ActiveSheet
My_Sheet.UsedRange ' VBA compiler doesn't like this
#Geoff_B I don't have any issues. Do you mind sharing the screenshot? I tried different variations everything seems fine. unlikely the compiler confused with the expression.
Sub test()
Debug.Print ActiveSheet.UsedRange.Address ' Works OK
Dim My_Sheet As Worksheet
Set My_Sheet = Sheets(ActiveSheet.Name)
Debug.Print My_Sheet.UsedRange.Address
'=====================and=====================
Dim My_Sheet As Object
Set My_Sheet = ActiveSheet
Debug.Print My_Sheet.UsedRange.Address
End Sub
Thanks Tim Williams, dimming as Object has resolved it. Looks like a bug to me.
I'm working on a macro that will change the default style of a worksheet back to "Normal", but I'm having trouble figuring out how to do so. I've tried a variety of different methods to accomplish this, and have also looked at the Microsoft documentation, but I can't see a specific way to change this setting programmatically. Can someone please assist? Thank you!
This is the format I'm working with:
' Set style for Worksheet
Sub SetSheetStyle(ws As Worksheet)
With ws
Selection.Style = "Normal"
End With
End Sub
Style is a Range property, so try something like:
Dim ws As Worksheet: Set ws = ThisWorkbook.Sheets("Sheet1")
ws.Cells.Style = "Normal"
I was able to get it with this syntax:
' Set style for Worksheet
Sub EWMBranding_SetSheetStyle(ws As Worksheet)
With ws
.Cells.Style = "Bad"
End With
End Sub
If I have an excel function such as:
=my_function(A1:C4)
and I want to call this from VBA like:
Dim t as variant
t = Application.Run("my_function",X)
What is the simplest way for X to represent A1:C4?
Your range has to come from a worksheet, so if you define that range first and then pass it to the function in your other workbook (I assume that's what you want to do since you wouldn't use Application.Run otherwise). So the following code will do the trick:
Sub RunFunctionInAnotherWorkbook()
Dim rThisRange as Range
Dim vResult as Variant
Set rThisRange = ActiveSheet.Range("A1:C4")
vResult = Application.Run("'MyOtherWorkbook.xls'!TheModuleName.TheSubName", rThisRange)
End Sub
And the function in the workbook "MyOtherWorkbook.xls":
Function TheSubName(inputRange as Range) as Variant
'Work your magic here
End Function
I haven't try it, but wouldn't
t=myfunction(range)
The simplest syntax is
Application.Run("my_function", [A1:c4])
it is possible to connect worksheet like so? const ws = me.textbox
I would like to make a drop down list with possibility to chose worksheets in my user form.
I know that method won't work i pasted it below.
Is there any other possibility to make it work?
I use select casebut it means that i write my whole code X times
Sub Populate()
Dim ws As Worksheet
Set ws = me.ChoseSheet
First assign text box value to string and then pass it to set
strSheetName = Me.ChoseSheet
Set ws = ThisWorkbook.Worksheets(strSheetName)
You could use a For each loop to populate a ComboBox on a UserForm
For example:
Public Sub UserForm_Initialize()
Dim ws as WorkSheet
For each ws in ActiveWorkbook.Worksheets
Combobox1.AddItem(ws.Name)
next ws
End sub
If I have an excel function such as:
=my_function(A1:C4)
and I want to call this from VBA like:
Dim t as variant
t = Application.Run("my_function",X)
What is the simplest way for X to represent A1:C4?
Your range has to come from a worksheet, so if you define that range first and then pass it to the function in your other workbook (I assume that's what you want to do since you wouldn't use Application.Run otherwise). So the following code will do the trick:
Sub RunFunctionInAnotherWorkbook()
Dim rThisRange as Range
Dim vResult as Variant
Set rThisRange = ActiveSheet.Range("A1:C4")
vResult = Application.Run("'MyOtherWorkbook.xls'!TheModuleName.TheSubName", rThisRange)
End Sub
And the function in the workbook "MyOtherWorkbook.xls":
Function TheSubName(inputRange as Range) as Variant
'Work your magic here
End Function
I haven't try it, but wouldn't
t=myfunction(range)
The simplest syntax is
Application.Run("my_function", [A1:c4])