I am getting an error on my evaluate function.
This code is running on a test sheet, so I know that it should give me "1" as answer, but I cannot make it run.
Here are the codes;
Check1 = Worksheets(Persona).Range("A3:A" & LastRowE3 & "")
Check2 = Worksheets(Persona).Range("J3:J" & LastRowE3 & "")
Ur_Val = "Production_End"
y = Application.Evaluate("=COUNTIFS(" & Check1 & ", " & xu_value & ", " & Check2 & ", " & Ur_Val & ")")
I know that "y" should be equal to "1", but I cannot get the answer right.
first declare your variables:
Dim Check1 as String,Check2 as String,Ur_Val as String,xu_value as String
Then
You want the Address of the ranges not just the range values:
Check1 = Worksheets(Persona).Range("A3:A" & LastRowE3 & "").Address(1,1,,1)
Check2 = Worksheets(Persona).Range("J3:J" & LastRowE3 & "").Address(1,1,,1)
And the strings Ur_Val and xu_value need to be surronded in " in the final formula so we need to add them:
, """ & xu_value & """,
So:
Dim Check1 as String,Check2 as String,Ur_Val as String,xu_value as String
Dim y as Long
Check1 = Worksheets(Persona).Range("A3:A" & LastRowE3 & "").Address(1,1)
Check2 = Worksheets(Persona).Range("J3:J" & LastRowE3 & "").Address(1,1)
Ur_Val = "Production_End"
xu_value = "SOMETHING_ELSE"
y = Application.Evaluate("=COUNTIFS(" & Check1 & ", """ & xu_value & """, " & Check2 & ", """ & Ur_Val & """)")
Related
I'm working on a large Excel project that requires entering a lot of data spread out over the worksheet that needs to be entered as quick as possible. To try and aide with the entry, I've created a number of UserForms that the user would enter the data into. One such form returns the above "Process Too Large" error when trying to transfer the data.
I understand why the error pops up - it's far too long. I've included the code for one such entry (slightly modified of course) and was wondering how I would be able to truncate it?
Dim ws As Worksheet
Dim i As Long
Set ws = ThisWorkbook.Sheets("STOCK")
' 101
If entry101.Value <> "" Then
Dim NUM101 As String
If com101.Value <> "" Then
NUM101 = "# - " & UCase(com101.Value)
Else
NUM101 = ""
End If
If cmb101.Value = "FULL" Then
ws.Range("_101").Value = UCase(code101.Value) & " " & Chr(10) & UCase(com101.Value) & " - FULL " & Chr(10) & " "
End If
If cmb101.Value = "OUT OF STOCK" Then
ws.Range("_101").Value = UCase(com101.Value) & " OUT OF STOCK " & Chr(10) & UCase(code101.Value) & " " & Chr(10) & " "
End If
If cmb101.Value = "SHIPPED" Then
ws.Range("_101").Value = UCase(code101.Value) & " " & Chr(10) & " - SHIPPED " & Chr(10) & NUM101
End If
If cmb101.Value = "DAMAGED" Then
ws.Range("_101").Value = UCase(code101.Value) & " DAMAGED " & Chr(10) & " "" & Chr(10) & NUM101"
End If
If cmb101.Value = "LOW STOCK" Then
ws.Range("_101").Value = UCase(com101.Value) & " LOW-STOCK " & Chr(10) & UCase(code101.Value) & " " & Chr(10) & " "
End If
If cmb101.Value = "RETURN" Then
ws.Range("_101").Value = UCase(code101.Value) & " " & Chr(10) & "RETURNED - " & UCase(com101.Value) & " " & Chr(10) & " "
End If
If cmb101.Value = "" Then
ws.Range("_101").Value = UCase(code101.Value) & Chr(10) & " - UNKNOWN CONDITION"
End If
End If
The UserForm has two text boxes ("code101" & "com101") and a single ComboBox ("cmb101") for each entry. The above code needs to be applied to a range from "_101" to "_143" so needs to repeat 43 times.
Any help would be greatly appreciated. Thank you all.
Something like this (untested):
Dim ws As Worksheet, vCom, vCode
Dim i As Long, s, num As String
Set ws = ThisWorkbook.Sheets("STOCK")
For i = 101 To 143
If Me.Controls("entry" & i).Value <> "" Then
vCom = UCase(Me.Controls("com" & i).Value)
vCode = UCase(Me.Controls("code" & i).Value)
num = IIf(vCom <> "", "# - " & vCom, "")
s = ""
Select Case Me.Controls("cmb" & i).Value
Case "FULL": s = vCode & " " & Chr(10) & vCom & " - FULL " & Chr(10) & " "
Case "OUT OF STOCK": s = vCom & " OUT OF STOCK " & Chr(10) & vCode & " " & Chr(10) & " "
Case "SHIPPED": s = vCode & " " & Chr(10) & " - SHIPPED " & Chr(10) & num
'etc
'etc
End Select
If Len(s) > 0 Then ws.Range("_" & i).Value = s
End If
Next i
I have a master spreadsheet that analyzes records from another spreadsheet with rows going all the way up to 1.4 million.
Below are the relevant pieces from the code:
Sub Whyamidoingthis()
Dim USISINLfp As String
Dim ISINL As String
Dim echeck As String
Dim wUSISIN As Workbook
Dim lastrow As Long
Dim Result As Worksheet
Dim i As Long
Set OutShVar = ThisWorkbook.Worksheets("in1")
ISINL = "CONSOLIDATED - Country_Of_Incorp_US_2019-03-01 (Consolidated).xlsx"
USISINLfp = "W:\Product Platforms\ISIN- CUSIP Country of Incorporation\March 2019\"
Workbooks.Open (USISINLfp & ISINL)
Set wUSISIN = Workbooks(ISINL)
With Result
lastrow = .Cells(.Rows.Count, "H").End(xlUp).Row
End With
'US Security 1
For i = 2 To lastrow
With Result
echeck = Trim(.Range("O" & i))
If echeck = "" Then
.Range("P" & i & ":Q" & i).Value = "N"
Else
.Range("P" & i).Value = "=ifna(vlookup(O" & i & "," & ISINL & "First Sheet'!$B:$C,2,false)," & Chr(34) & "N" & Chr(34) & ")"
.Range("Q" & i).Value = "=ifna(vlookup(O" & i & "," & ISINL & "Second Sheet'!$B:$C,2,false)," & Chr(34) & "N" & Chr(34) & ")"
'Debug.Print "=ifna(vlookup(O" & i & "," & ISINL & "Second Sheet'!$B:$C,2,0)," & Chr(34) & "N" & Chr(34) & ")"
End If
'US Security 2
echeck = Trim(.Range("S" & i))
If echeck = "" Then
.Range("T" & i & ":U" & i).Value = "N"
Else
.Range("T" & i).Value = "=ifna(vlookup(S" & i & "," & ISINL & "First Sheet'!$A:$C,3,false)," & Chr(34) & "N" & Chr(34) & ")"
.Range("U" & i).Value = "=ifna(vlookup(S" & i & "," & ISINL & "Second Sheet'!$A:$C,3,false)," & Chr(34) & "N" & Chr(34) & ")"
End If
End With
Next I
If Not wUSISIN Is Nothing Then wUSISIN.Close savechanges:=False
End Sub
The code is getting stuck at the following line:
.Range("T" & i).Value = "=ifna(vlookup(S" & i & "," & ISINL & "First Sheet'!$A:$C,3,false)," & Chr(34) & "N" & Chr(34) & ")"
Whenever a result is found and the error is application defined error.
Try this formula:
"=ifna(vlookup(O" & i & ",'[" & ISINL & "]First Sheet'!$B:$C,2,false)," & Chr(34) & "N" & Chr(34) & ")"
This places square brackets around the workbook name and single quotes around the workbook-worksheet combo. See this tutorial about using VLOOKUP from another workbook.
This question already has an answer here:
Passing variables using excel macros in order to complete a sentence
(1 answer)
Closed 5 years ago.
I want to print the below output in my excel sheet rather than displaying on the msg box.
I have written the below code but I am not able to print the output of sCommand in my excel sheet.
I want to print the output on cell A1.
Thanks in advance.
sCommand = "Call " & s1 & "." & s2 & "(" & "'" & INPUT_DATE & "'" & "," & "'" &EXIT_DATE & "'" & "," & "STATUS " & ")" & ";"
MsgBox (sCommand)
Same as Gadziu, but instead of using .Cells(1,1) using .Range:
sCommand = "Call " & s1 & "." & s2 & "(" & "'" & INPUT_DATE & "'" & "," & "'" &EXIT_DATE & "'" & "," & "STATUS " & ")" & ";"
ActiveSheet.Range("A1").Value = sCommand
You should also declare which Sheet you are actually wanting to write to, as ActiveSheet will take the sheet that is currently selected/active, so this could be better defined as:
Sub foo()
Dim ws As Worksheet: Set ws = Sheets("Sheet1")
'declare and set your worksheet, amend as required
Dim s1 As String, s2 As String, sCommand As String
s1 = InputBox("Enter schema name")
s2 = InputBox("Enter procedure name")
Input_date = Format(Date, "dd/mm/yyyy") 'get today's date or: Input_date = InputBox("Enter Input Date")
Exit_Date = Format(Date, "dd/mm/yyyy") 'get today's date or: Exit_Date = InputBox("Enter Exit Date")
Status = InputBox("Status")
sCommand = "Call " & s1 & "." & s2 & "(" & "'" & Input_date & "'" & "," & "'" & Exit_Date & "'" & "," & Status & ")" & ";"
'concatenate your variables into a string
'ws.Cells(1, 1).Value = sCommand 'the Cells(1, 1) refer to Cells(row_number, column_number)
ws.Range("A1").Value = sCommand
End Sub
sCommand = "Call " & s1 & "." & s2 & "(" & "'" & INPUT_DATE & "'" & "," & "'" &EXIT_DATE & "'" & "," & "STATUS " & ")" & ";"
ActiveSheet.Cells(1,1).Value = sCommand
I've concatenated a string to paste an index match formula into rows of a column. Every time I try running this piece of code, I get Runtime Error '1004, but I can't see what I have wrong. Here's the code I have:
Dim j As Long
'Loop down the rows in mainfile
For j = 2 To lastFullRow2
Dim firstArgument As String
firstArgument = "Sheet2!" & valuecolumnLetter & "2:" & valuecolumnLetter & lastFullRow1 & ""
'MsgBox "firstArgument" & firstArgument
Dim secondArgument As String
secondArgument = "Sheet2!" & parameter1columnLetter & "2:" & parameter1columnLetter & lastFullRow1 & ""
'MsgBox "secondArgument " & secondArgument
Dim thirdArgument As String
thirdArgument = "Sheet2!" & parameter2columnLetter & "2:" & parameter2columnLetter & lastFullRow1 & ""
'MsgBox "thirdArgument " & thirdArgument
Dim fourthArgument As String
fourthArgument = "Sheet2!" & parameter2columnLetter & "2:" & parameter2columnLetter & lastFullRow1 & ""
'MsgBox "fourthArgument " & fourthArgument
Dim condition3 As String
condition3 = "Sheet3!" & "D2:" & D & j & ""
'MsgBox "condition3 " & condition3
Dim patid1 As String
patid1 = "Sheet2!" & "D2:" & D & lastFullRow2 & ""
'MsgBox "patid1 " & patid1
With ws_mainfile
Dim commandstring As String
commandstring = "=INDEX(" & firstArgument & ",MATCH(1,(" & secondArgument & "=" & condition1 & ")*(" & thirdArgument & "=" & condition2 & ")*(" & patid1 & "=" & condition3 & "),0))"
ws_mainfile.Range("AN" & j).FormulaArray = commandstring
End With
Next j
The debugger is saying the error is at the ws_mainfile.Range... = commandstring line.
condition3 = "Sheet3!" & "D2:" & D & j & ""
patid1 = "Sheet2!" & "D2:" & D & lastFullRow2 & ""
Have you defined a variable D and what is its value?
Maybe you meant:
condition3 = "Sheet3!" & "D2:D" & j
patid1 = "Sheet2!" & "D2:D" & lastFullRow2
There's also no need to concatenate an empty string onto the end of those lines.
I need to import data from excel to access. The importation is now "working" for the obvious types (string, integers). However, In the excel file i have some strings that i need to convert to boolean in my access tables. The strings can take only 2 values "oui" or "non" (yes or no in french).
In my vba code i have the following line:
cSQL = "INSERT INTO " & strTable & " ( [N_CLIENT], [NOM_CLI], ) VALUES (" & Chr(34) & .Range("A" & i) & Chr(34) & "," & Chr(34) & .Range("F" & i) & Chr(34) & ");"
DoCmd.RunSQL cSQL
I would liketo know if i can use an if condition to check the value itself inside the cSQL call and replace it with either true or false. like what follows.
cSQL = "INSERT INTO " & strTable & " ( [N_CLIENT], [NOM_CLI], ) VALUES (If(" & Chr(34) & .Range("A" & i) & Chr(34) & " = "oui") then "true" else then "false"," & Chr(34) & .Range("F" & i) & Chr(34) & ");"
DoCmd.RunSQL cSQL
You can use VBA-Functions in Access-SQL-Queries:
"[...] VALUES(strcomp('" & Range("A" & i) & "','oui', 1)=0 [...]"
Regards,
AKDA
It looks like you want the Access SQL to evaluate your oui / non into true and false. You could do this using the iif statement http://www.techonthenet.com/access/functions/advanced/iif.php. However, why bother? You could just evaluate the range in vba and pass through the boolean variable. Using something like this:
dim result as boolean
if sheet1.Range("A" & i) = "oui" then
result = true
else
result = false
end if
Then just insert that into your SQL:
cSQL = "INSERT INTO " & strTable & " ( [N_CLIENT], [NOM_CLI], ) VALUES (If(" & Chr(34) & result & Chr(34) & , & Chr(34) & .Range("F" & i) & Chr(34) & ");"
You could also use the IIF statement in SQL to check the value:
http://msdn.microsoft.com/en-us/library/hh213574.aspx
It is easier to use apostrophes rather than Chr(34), and splitting the statement across lines using the line-continuation character '_' helps to read the statement. The IIf() function is also used in the following.
Also note that you have an extra comma after the term [NOM_CLI] which shouldn't be there.
cSQL = "INSERT INTO " & strTable & "( [N_CLIENT], [NOM_CLI] ) VALUES ('" _
& IIf(StrComp(.Range("A" & i), "oui", 1) = 0, "true", "false") _
& "','" & .Range("F" & i) & "');"
This results in a string like this:
INSERT INTO ( [N_CLIENT], [NOM_CLI] ) VALUES ('true','hello');
To use the Boolean values (0, -1) change to:
cSQL = "INSERT INTO " & strTable & "( [N_CLIENT], [NOM_CLI] ) VALUES (" _
& IIf(StrComp(.Range("A" & i), "oui", 1) = 0, -1, 0) _
& ",'" & .Range("F" & i) & "');"
which yields
INSERT INTO ( [N_CLIENT], [NOM_CLI] ) VALUES (-1,'hello');