I am new to VBA and learning; however, I am also running VBA on my MAC instead of PC. That being said, I copy the code down just like I have seen in VBA used by PCs and I get a run-time Error '1004'Method 'Range' of object '_Global' failed. Can someone please explain to me why that is?
My code is simple: MonthArray(1) = Range("myMonths").Cells(i,1).value
Thoughts anyone?
Sub Array_OneDimension()
Dim MonthArray(1 To 12) As String
Dim i As Byte
For i = 1 To 12
MonthArray(i) = Range("myMonths").Cells(i, 1).Value 'This is where the error is
Next i
End Sub
I was expecting the code to loop through the range and pull out the value in the locals window.
I have two related problems. It seems like I don't have a library attached. I have searched and cannot find what I am missing. This is a call to a sub from a function that is called(referenced) in an Excel cell.
The syntax checker fails when I put an array in as a parameter like this which is taken from this VB Page
Sub EmptyValid210917 _
(RefData As String, RefRow As Integer, PhraseArray As String(,), _
DayIndex As Integer, EmptyCol As Boolean, ValidCol As Boolean)
The second related issue is the syntax check fails when I try to dim an array like this:
Dim ColOffset(,) As Integer
It will accept this:
Dim ColOffset(2, 2) As Integer
But does not accept loading as below which is taken from this VB Guide How To
Dim ColOffset(2, 2) As Integer = {{1,2},{3,4}}
Perhaps this is also related. When I call the max function. I get: Compile Error - Method or data member not found
FirstValidDay = Math.Max(1, 2)
Thank you for your help.
So I wrote up an Array formula which works fine on my excel, but when converting it to VBA I'm getting an error: Unable to set the FormulaArray property of the Range class. I'm not sure why I'm getting this issue. Please see the code here:
Selection.FormulaArray = _
"=IF(IF(AND(OR(RC[-14]<>R[-1]C[-14],RC[-15]<>R[-1]C[-15]),Pay_Periods!R2C10<>0),MAX(IF(Pay_Periods!R2C3:R250C3>=Pay_Periods!R1C10+(14*Pay_Periods!R3C10),IF(Pay_Periods!R2C2:R250C2<=Pay_Periods!R1C10+(14*Pay_Periods!R3C10),Pay_Periods!R2C3:R250C3))),MAX(IF((Pay_Periods!R2C3:R250C3>=Sheet1!RC[-10])*(IF(AND(Sheet1!RC[-14]=Sheet1!R[-1]C[-14],Sheet1!R[-1]C[-10]<Sheet1!RC[" & _
"-10]+14),Pay_Periods!R2C3:R250C3<(RC[-10]+14),IF(AND(RC[-14]=R[-1]C[-14],RC[-15]=R[-1]C[-15]),Pay_Periods!R2C3:R250C3<R[-1]C[-10],1))),Pay_Periods!R2C3:R250C3,"""")))=0,"""",IF(AND(OR(RC[-14]<>R[-1]C[-14],RC[-15]<>R[-1]C[-15]),Pay_Periods!R2C10<>0),MAX(IF(Pay_Periods!R2C3:R250C3>=Pay_Periods!R1C10+(14*Pay_Periods!R3C10),IF(Pay_Periods!R2C2:R250C2<=Pay_Periods!R1C10+" & _
"(14*Pay_Periods!R3C10),Pay_Periods!R2C3:R250C3))),MAX(IF((Pay_Periods!R2C3:R250C3>=Sheet1!RC[-10])*(IF(AND(Sheet1!RC[-14]=Sheet1!R[-1]C[-14],Sheet1!RC[-15]=Sheet1!R[-1]C[-15],Sheet1!R[-1]C[-10]<Sheet1!RC[-10]+14),Pay_Periods!R2C3:R250C3<(RC[-10]+14),IF(AND(RC[-14]=R[-1]C[-14],RC[-15]=R[-1]C[-15]),Pay_Periods!R2C3:R250C3<R[-1]C[-10],1))),Pay_Periods!R2C3:R250C3,"""")" & _
")))" & _
""
By the way I copied it exactly how the macro itself recorded it so you may notice some spaces. I'm thinking it could just be too long, but it seems weird that writing it in the cell gets it fine, but not in the vba form. If anyone can help it would be much appreciated! I'm attempting to have this formula self insert by code and copy paste values so the excel doesn't have to keep loading in information.
Here is how the formula looks normally:
=IF(IF(AND(OR(C2<>C1,B2<>B1),Pay_Periods!$J$2<>0),MAX(IF(Pay_Periods!$C$2:$C$250>=Pay_Periods!$J$1+(14*Pay_Periods!$J$3),IF(Pay_Periods!$B$2:$B$250<=Pay_Periods!$J$1+(14*Pay_Periods!$J$3),Pay_Periods!$C$2:$C$250))),MAX(IF((Pay_Periods!$C$2:$C$250>=Sheet1!G2)*(IF(AND(Sheet1!C2=Sheet1!C1,Sheet1!G1<Sheet1!G2+14),Pay_Periods!$C$2:$C$250<(G2+14),IF(AND(C2=C1,B2=B1),Pay_Periods!$C$2:$C$250<G1,1))),Pay_Periods!$C$2:$C$250,"")))=0,"",IF(AND(OR(C2<>C1,B2<>B1),Pay_Periods!$J$2<>0),MAX(IF(Pay_Periods!$C$2:$C$250>=Pay_Periods!$J$1+(14*Pay_Periods!$J$3),IF(Pay_Periods!$B$2:$B$250<=Pay_Periods!$J$1+(14*Pay_Periods!$J$3),Pay_Periods!$C$2:$C$250))),MAX(IF((Pay_Periods!$C$2:$C$250>=Sheet1!G2)*(IF(AND(Sheet1!C2=Sheet1!C1,Sheet1!B2=Sheet1!B1,Sheet1!G1<Sheet1!G2+14),Pay_Periods!$C$2:$C$250<(G2+14),IF(AND(C2=C1,B2=B1),Pay_Periods!$C$2:$C$250<G1,1))),Pay_Periods!$C$2:$C$250,""))))
Then ctrl+shift+enter naturally
I went ahead and built a custom solution just for you. Here, you'll see I broke up your formula into smaller parts and substitute them back together (just like the good 'ol days back in algebra class).
Also note, that I don't use the Selection object as you do in your question. I recommend working with Ranges directly rather than using .Select, Selection. or .Activate and so on. So In my example below I assume the range you have "Selected" is A1 on the first worksheet.
strFormula = "=IF(IF(AND(OR(C2<>C1,B2<>B1),Pay_Periods!$J$2<>0),MAX(W_W),MAX(X_X))=0,""""," & _
"IF(AND(OR(C2<>C1,B2<>B1),Pay_Periods!$J$2<>0),MAX(Y_Y),MAX(Z_Z)))"
strFormulaW_W = "IF(Pay_Periods!$C$2:$C$250>=Pay_Periods!$J$1+(14*Pay_Periods!$J$3)," & _
"IF(Pay_Periods!$B$2:$B$250<=Pay_Periods!$J$1+(14*Pay_Periods!$J$3),Pay_Periods!$C$2:$C$250))"
strFormulaX_X = "IF((Pay_Periods!$C$2:$C$250>=Sheet1!G2)*(IF(AND(Sheet1!C2=Sheet1!C1,Sheet1!G1<Sheet1!G2+14)," & _
"Pay_Periods!$C$2:$C$250<(G2+14),IF(AND(C2=C1,B2=B1),Pay_Periods!$C$2:$C$250<G1,1))),Pay_Periods!$C$2:$C$250,"""")"
strFormulaY_Y = "IF(Pay_Periods!$C$2:$C$250>=Pay_Periods!$J$1+(14*Pay_Periods!$J$3),IF(Pay_Periods!$B$2:$B$250<=" & _
"Pay_Periods!$J$1+(14*Pay_Periods!$J$3),Pay_Periods!$C$2:$C$250))"
strFormulaZ_Z = "IF((Pay_Periods!$C$2:$C$250>=Sheet1!G2)*(IF(AND(Sheet1!C2=Sheet1!C1,Sheet1!B2=Sheet1!B1," & _
"Sheet1!G1<Sheet1!G2+14),Pay_Periods!$C$2:$C$250<(G2+14),IF(AND(C2=C1,B2=B1),Pay_Periods!$C$2:$C$250<G1,1))),Pay_Periods!$C$2:$C$250,"""")"
With ThisWorkbook.Worksheets(1).Range("A1")
.FormulaArray = strFormula
.Replace "W_W", strFormulaW_W
.Replace "X_X", strFormulaX_X
.Replace "Y_Y", strFormulaY_Y
.Replace "Z_Z", strFormulaZ_Z
End With
I have been looking to find a solution and cant find anything online that fully explains what is going on. I looked at some other posts but they all seem to fall a bit short.
When I run this bit of code it works perfectly (as it was recorded).
ActiveSheet.Range("$A$1:$AL$1002").AutoFilter Field:=17, Criteria1:=Array( _
"73578", "78759", "78765"), Operator:=xlFilterValues
But then when I try to make it more robust it fails. I want to change the Criteria1 argument to an already stored array.
I am trying to get the following to work.
ActiveSheet.Range("$A$1:$AL$1002").AutoFilter Field:=17, _
Criteria1:=Array(StoredArray.Values), Operator:=xlFilterValues
I have the array stored and I manipulate it anyway but I have yet to get anything to work. I have also tried to create a string to be exactly like the recorded macro but that does not work either.
Dim StoredArrayString as Variant
StoredArrayString = "73578"", ""78759"", ""78765"
ActiveSheet.Range("$A$1:$AL$1002").AutoFilter Field:=17, _
Criteria1:=StoredArrayString, Operator:=xlFilterValues
Thanks for the help here I have spent lots of time on MSDN trying to research this issue but can't find a solution.
I think your issue is with how you're defining your array. Try this instead.
Dim StoredArrayString As Variant
StoredArrayString = Array("73578", "78759", "78765")
ActiveSheet.Range("$A$1:$AL$1002").AutoFilter Field:=17, _
Criteria1:=StoredArrayString, Operator:=xlFilterValues
Starting with:
and run:
Sub Macro7()
Dim ary(1 To 3) As String
ary(1) = "Alice"
ary(2) = "Boris"
ary(3) = "James"
ActiveSheet.Range("$A$1:$D$22").AutoFilter Field:=3, Criteria1:=ary, Operator:=xlFilterValues
End Sub
will produce:
I have created a forum in vb6 and made connection with a database in access,everything went perfect.
my problem is in my form there is 2 combobox one to select Number and other to get me other numbers (watch the video to understand )
anyway the first combo is working and the second combo is working too but after selecting different number from the first combo i don't get anything in the second combo.anyway i know i just miss something in the code something very stupid
i have uploaded a video so you can see my problem, thanks in advance.
Private Sub Form_Load()
liaison
Do Until rspatient.EOF
Me.npa.AddItem rspatient.Fields(0)
rspatient.MoveNext
Loop
End Sub
Private Sub npa_Click()
rspatient.MoveFirst
Dim cr As String
cr = "npation ='" & npa & "'"
rspatient.Find cr
nom = rspatient.Fields(1)
prenom = rspatient.Fields(3)
rshospita.MoveFirst
nh.Clear
While rshospita.EOF = False
If UCase(rshospita.Fields(14)) = UCase(npa) Then
nh.AddItem rshospita.Fields(0)
End If
rshospita.MoveNext
Wend
End Sub
video for more detail :
https://www.youtube.com/watch?v=Tidm18_tvp0
The simplest possible reason for your problem is that you don't have any hospital records associated with your second patient. Check that first.
However, your code is also a bit convoluted. A simpler way to do what you want is to use Filter instead of Find. Filter restricts your recordset to only those records which match the filter, so you can simply iterate the filtered recordset the same way you do the whole one (rspatient). Something like this:
Private Sub npa_Click()
With rshospita
.Filter = ".Fields(14) = '" & npa & "'"
.MoveFirst
nh.Clear
Do Unitl .EOF
nh.AddItem .Fields(0)
.MoveNext
End With
End Sub