'Error 1004' when concatenating string - excel

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.

Related

VBA - Shell, Run Time Error 5 Invalid Procedure - Thunderbird Email with HTML table

I have a VBA code which allows to create an email(Thunderbird) and in email body appers HTML table based on cell values. Everythings works fine, but only until table has less then 19 rows and 24 columns. Then pops up Run Time Error 5 - line with Call Shell.
Here code for HTML Table(found in Net):
Function create_table(rng As Range) As String
Dim mbody As String
Dim mbody1 As String
Dim i As Long
Dim j As Long
mbody = "<TABLE width=""30%"" Border=""1"", Cellspacing=""0""><TR>" ' configure the table
'create Header row
For i = 1 To rng.Columns.Count
mbody = mbody & "<TD width=""100"", Bgcolor=""#000000"", Align=""Center""><Font Color=#FFFFFF><b><p style=""font-size:12px"">" & rng.Cells(1, i).Value & " </p></Font></TD>"
Next
' add data to the table
For i = 2 To rng.Rows.Count
mbody = mbody & "<TR>"
mbody1 = ""
For j = 1 To rng.Columns.Count
mbody1 = mbody1 & "<TD width=""80"", Align=""Center""><p style=""font-size:12px"">" & rng.Cells(i, j).Value & "</TD>"
Next
mbody = mbody & mbody1 & "</TR>"
Next
create_table = mbody
End Function
Code for email:
email = Worksheets("Sheet1").Range("B1").Value
subj = Worksheets("Sheet1").Range("B2").Value
body = "Hello" & "<br><br>" & _
create_table(ActiveSheet.Range("A1").CurrentRegion) & "</Table></Table>"
thund = "Thunderbird path" & _
" -compose " & """" & _
"to='" & email & "'," & _
"cc='" & cc & "'," & _
"bcc='" & bcc & "'," & _
"subject='" & subj & "'," & _
"body='" & body & "'" & """"
Call Shell(thund, vbNormalNoFocus)
Application.Wait (Now + TimeValue("0:00:03"))
Is there any limition? Is it possible to change it?
Thunderbird allows using a file for the body with the message parameter, use that instead of the body parameter
Dim FSO As New FileSystemObject
Set FSO = CreateObject("Scripting.FileSystemObject")
fname = FSO.GetTempName & ".html"
Set FileToCreate = FSO.CreateTextFile(fname)
FileToCreate.Write body
FileToCreate.Close
Email = Worksheets("Sheet1").Range("B1").Value
subj = Worksheets("Sheet1").Range("B2").Value
body = "Hello" & "<br><br>" & _
create_table(ActiveSheet.Range("A1").CurrentRegion) & "</Table></Table>"
thund = "Thunderbird path" & _
" -compose " & """" & _
"to='" & Email & "'," & _
"cc='" & cc & "'," & _
"bcc='" & bcc & "'," & _
"subject='" & subj & "'," & _
"message='" & fname & "'" & """"
Call Shell(thund, vbNormalNoFocus)
Kill fname
Application.Wait (Now + TimeValue("0:00:03"))

Assign variable to graph column in excel vba

I want change the columns AN, Z, AD from text to the column number which I will store as a integer variable.
AChart.SeriesCollection.NewSeries
AChart.FullSeriesCollection(iCollection).Name = "=" & "'" & ws.Name & "'" & "!$AN$" & iStart
AChart.FullSeriesCollection(iCollection).XValues = "=" & "'" & ws.Name & "'" & "!$Z$" & iStart & ":$Z$" & iEnd
AChart.FullSeriesCollection(iCollection).Values = "=" & "'" & ws.Name & "'" & "!$AD$" & iStart & ":$AD$" & iEnd
I tried "!C" & variable name but that didn't work.
Please help
Thanks
Dan

VLOOKUP from another spreadsheet using VBA

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.

Printing the output of a macro directly on cell A1 rather than displaying on the msgbox [duplicate]

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

excel vba countifs evaluate

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 & """)")

Resources