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.
Related
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
I am trying to loop through the Headers of the Table and then populate the formula as mentioned below:
Dim headerRng As Range
For Each headerRng In IndMetricsSht.ListObjects(TableName2).Range.Rows(1).Cells
Debug.Print headerRng
If headerRng.Value <> "Function" And headerRng.Value <> "Team Members" And headerRng.Value <> "Accuracy %" Then
IndMetricsSht.ListObjects(TableName2).ListColumns(headerRng.Value).DataBodyRange.Select
Selection.FormulaR1C1 = "=SUMIFS(INDIRECT(" & Chr(34) & TableName & Chr(34) & " & ""["" & [#[Team Members]] & CHAR(10) & ""Volumes"" & ""]"")," & Chr(34) & TableName & Chr(34) & " & ""["" & [#[Sub-Function]] & ""]"",LEFT(" & Chr(34) & headerRng.Value & Chr(34) & ",FIND(""_""," & Chr(34) & headerRng.Value & Chr(34) & ")-1))" 'I'm facing error (application object error)
End If
Next
Actual formula in Excel sheet looks like:
=SUMIFS(INDIRECT("IM_012022" & "[" & [#[Team Members]] & CHAR(10) & "Volumes" & "]"),IM_012022[Sub-Function],LEFT(Consolidated_012022[[#Headers],[Corporate Treasury_Volumes]],FIND("_",Consolidated_012022[[#Headers],[Corporate Treasury_Volumes]])-1))
I'm trying to replicate the same formula for each header using the loops concept, but somwhere in that formula, I'm missing.
Dim HeaderRng As Range
For Each HeaderRng In IndMetricsSht.ListObjects(TableName2).Range.Rows(1).Cells
Debug.Print HeaderRng
If HeaderRng.Value <> "Function" And HeaderRng.Value <> "Team Members" And HeaderRng.Value <> "Volumes" And Not (HeaderRng.Value Like "*Errors") And HeaderRng.Value <> "Accuracy %" Then
IndMetricsSht.ListObjects(TableName2).ListColumns(HeaderRng.Value).DataBodyRange.Select
Selection.FormulaR1C1 = "=SUMIFS(INDIRECT(" & Chr(34) & TableName & Chr(34) & " & ""["" & [#[Team Members]] & CHAR(10) & ""Volumes"" & ""]""),INDIRECT(" & Chr(34) & TableName & Chr(34) & " & ""[Sub-Function]""),LEFT(" & Chr(34) & HeaderRng.Value & Chr(34) & "," & InStr(1, HeaderRng.Value, "_") - 1 & "))"
End If
Next
I'm trying to create a vba code to write a formula in determined cell on my sheet, but it doesn't work. What's wrong ? 'Cause, i really don't see what am i doing wrong in this code.
Range("BC" & ActiveCell.Row).Formula = "=IF($BB" & ActiveCell.Row & "=" & """" & "REVIEW" & """" & ";IF(ROW($BB" & ActiveCell.Row & ")<MAX(IF($BB:$BB=" & """" & "OK" & """" & ";$A:$A));IF(TODAY()-$AY" & ActiveCell.Row & ">=3;" & """" & "DROP" & """" & ";" & """" & "REVIEW" & """" & ");" & """" & "REVIEW" & """" & ");" & """" & """" & ")"
Your computer's use of a ; as the regional list separator is fouling you up. VBA is very EN-US-centric so the Range.Formula and Range.FormulaR1C1 expect a comma (,) as the function's argument list separator.
Range("BC" & ActiveCell.Row).Formula = "=IF($BB" & ActiveCell.Row & "=" & """" & "REVIEW" & """" & ", IF(ROW($BB" & ActiveCell.Row & ")<MAX(IF($BB:$BB=" & """" & "OK" & """" & ", $A:$A)), IF(TODAY()-$AY" & ActiveCell.Row & ">=3, " & """" & "DROP" & """" & ", " & """" & "REVIEW" & """" & "), " & """" & "REVIEW" & """" & "), " & """" & """" & ")"
Alternately, Range.FormulaLocal property or Range.FormulaR1C1Local property can be used with your own semi-colon as the list separator.
Range("BC" & ActiveCell.Row).FormulaLocal = "=IF($BB" & ActiveCell.Row & "=" & """" & "REVIEW" & """" & ";IF(ROW($BB" & ActiveCell.Row & ")<MAX(IF($BB:$BB=" & """" & "OK" & """" & ";$A:$A));IF(TODAY()-$AY" & ActiveCell.Row & ">=3;" & """" & "DROP" & """" & ";" & """" & "REVIEW" & """" & ");" & """" & "REVIEW" & """" & ");" & """" & """" & ")"
Your doubling up double quotes within a quoted string is a bit verbose and xlR1C1 notation would save some steps. TEXT(,) is the same as "" in a formula; each produces the same zero-length string.
Range("BC" & ActiveCell.Row).Formula = "=IF($BB" & ActiveCell.Row & "=""REVIEW"", IF(ROW($BB" & ActiveCell.Row & ")<MAX(IF($BB:$BB=""OK"", $A:$A)), IF(TODAY()-$AY" & ActiveCell.Row & ">=3, ""DROP"", ""REVIEW""), ""REVIEW""), TEXT(,))"
'.FormulaR1C1
Range("BC" & ActiveCell.Row).FormulaR1C1 = "=IF(RC54=""REVIEW"", IF(ROW(R:R)<MAX(IF(C54:C54=""OK"", C1:C1)), IF(TODAY()-RC51>=3, ""DROP"", ""REVIEW""), ""REVIEW""), TEXT(,))"
The xlA1 and xlR1C1 formula rewrites above each produce the following when ActiveCell is on the second row.
=IF($BB2="REVIEW", IF(ROW($BB2)<MAX(IF($BB:$BB="OK", $A:$A)), IF(TODAY()-$AY2>=3, "DROP", "REVIEW"), "REVIEW"), TEXT(,))
As I mentioned in comments above, that formula would seem to be an array formula. In that case, use the .Formula comma based rewrite but change the Range.Formula property to Range.FormulaArray.
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 am trying to write a formula into a cell, using VBA. The code excerpt below delivers the correct formula, with correct references. But I keep getting a 1004 run-time error. I can't figure out what is triggering it. Hope this code excerpt is enough to reveal the answer, but if you need more just ask.
Set rg_sheet = ActiveWorkbook.Worksheets("RUNGLANCE")
for colNo = 2 to 8
for rowNo = 3 to 27
daySheetName = Cells(1,colNo)
rg_sheet.cells(rowNo,ColNo).formula = "=VLookUp(" & Chr(34) & "$A" & rowNo & Chr(34) & "), " & daySheetName & "!(" & chr(34) & "$A$3:$B$27" & Chr(34) & ", 2, False"
next rowNo
next colNo
Instead of
rg_sheet.cells(rowNo,ColNo).formula = "=VLookUp(" & Chr(34) & "$A" & rowNo & Chr(34) & "), " & daySheetName & "!(" & chr(34) & "$A$3:$B$27" & Chr(34) & ", 2, False"
try
rg_sheet.Cells(rowNo, colNo).Formula = "=VLookUp($A" & rowNo & ", " & daySheetName & "!" & "$A$3:$B$27" & ", 2, False" & ")"