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 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
This is the code I am using for my login screen, for the login buttton using a link from youtube but i am facing difficulty. The first line is highlighted in yellow which may suggest there is a problem with it:
Private Sub Command9_Click()
If IsNull(Me.TxtUsername) Then
MsgBox "Please enter Username", vbInformation, "Username required"
Me.TxtUsername.SetFocus
ElseIf IsNull(Me.txtPassword) Then
MsgBox "Please enter Password", vbInformation, "Password required"
Me.txtPassword.SetFocus
Else
'process the job
If (IsNull(DLookup("[Username]", "User details", "[Username] ='" & Me.TxtUsername.Value & "' And password = '" & Me.txtPassword.Value & "'"))) Then
MsgBox "Incorrect Username or Password"
Else
DoCmd.Close
End If
End If
End If
End Sub
You have 3 EndIfs but only 2 Ifs.
Remove one of the Endifs.
Ok, so I want to make a program that creates a text file on the user's desktop, But I also want it to work on more than just my computer, Any ideas?
I see you've already accepted an answer from #Jonco98. However, there's no guarantee that C:\Users will always be the root user folder. On XP, it may be under Documents and Settings. Or, the OS may be installed on a drive letter other than C.
A better way to retrieve the user's desktop is by using the WshSpecialFolders collection.
strDesktopFolder = CreateObject("WScript.Shell").SpecialFolders("Desktop")
Ok, so heres what I got:
Set File = CreateObject("Scripting.FileSystemObject")
Set User = CreateObject("Wscript.Network")
Set Report = File.CreateTextFile("C:\Users\" & User.UserName & "\Desktop\Report.txt", True)
And if you want to type in something to the file say the date and time add this after the first three lines:
Report.WriteLine(vbNewLine & "Accessed On: " & Day(date) & "/" & Month(date) & "/" & Year(date) & vbNewLine & "Accessed At: " & Hour(Now) & ":" & Minute(Now) & ":" & Second(Now))
Report.Close
I'm trying to copy several complete worksheets from one workbook to another. I have recorded a macro to create the code, but when I implement it into my main workbook I get the Subscript Out of Range error (runtime error ‘9’). My research for this question reveals how to copy data from one workbook to another, but not multiple worksheets.
Here is the code. It is supposed to copy the worksheets in Therm Cal Compiler.xlsm and paste them into SN1813016 - Copy.xlsx after the fourth sheet:
Windows("Therm Cal Compiler.xlsm").Activate
Sheets(Array("Thermal Calibration", "Therm_R1_-_0uA_-25C", "Therm_R1_-_0uA_0C", _
"Therm_R1_-_0uA_23C", "Therm_R1_-_0uA_40C", "Therm_R1_-_0uA_50C", _
"Therm_R1_-_0uA_60C", "Therm_R1_-_0uA_65C", "Therm_R1_-_0uA_70C")).Select
Sheets(Array("Thermal Calibration", "Therm_R1_-_0uA_-25C", "Therm_R1_-_0uA_0C", _
"Therm_R1_-_0uA_23C", "Therm_R1_-_0uA_40C", "Therm_R1_-_0uA_50C", _
"Therm_R1_-_0uA_60C", "Therm_R1_-_0uA_65C", "Therm_R1_-_0uA_70C")).Copy Before _
:=Workbooks("SN1813016 - Copy.xlsx").Sheets(5)
Both the Therm Cal Compiler.xlsm and SN1813016 - Copy.xlsx are open, and the error occurs on the second array command,
Sheets(Array("Thermal Calibration",…)).Copy Before _
:=Workbooks("SN1813016 - Copy.xlsx").Sheets(5).
Any ideas?
Thanks
If your destination workbook does not have 5 sheets, this will cause that error.
You should use After:= instead. Also, the Activate and Selects are not required.
Try this
Workbooks("Therm Cal Compiler.xlsm").Worksheets(Array( _
"Thermal Calibration", _
"Therm_R1_-_0uA_-25C", _
"Therm_R1_-_0uA_0C", _
"Therm_R1_-_0uA_23C", _
"Therm_R1_-_0uA_40C", _
"Therm_R1_-_0uA_50C", _
"Therm_R1_-_0uA_60C", _
"Therm_R1_-_0uA_65C", _
"Therm_R1_-_0uA_70C")).Copy _
After:=Workbooks("SN1813016 - Copy.xlsx").Sheets(4)