I am trying to use VBA to create a description that includes the Month/Year, text, and the value of a cell. I have tried two methods and neither one is working for me.
First i tried this:
ActiveCell.FormulaR1C1 = _
"=CONCATENATE(Format(DateAdd("m", -1, now), "mmmyy"), " My Text ",RC[-19])"
Which gives a
run-time error 1004.
Next I tried
Range("X10").Value = Format(DateAdd("m",-1,now), "mmmyy") & " My Text " & E10
Which output the date and the text but not the value of the cell.
I'm not sure where I'm going wrong with this. Searching on here got me to this point, but I'm stuck.
Thanks for any help.
'Let: X be your Workbook index, Y be your sheet index, YY be your row index, XX be your column index
Sub macro2()
MsgBox Month(Date) & "/" & Year(Date) & " " & Workbooks(X).Sheets(Y).Cells(YY, XX).Value
End Sub
All of these may be stored into a string variable for portability across your project.
Good luck!.
Related
I need to extract the current cell in VBA without passing it in parameter of the function.
I mean, for example, if the formula is in cell B35, I want to extract the address (line = 35, column = 2), in the VBA code of the formula.
I thought the active cell would do it, but no, it extracts the address of the cell where the cursor is.
Do you know how I could do it?
Thanks in advance for help.
I think you mean Application.Caller which is the object that calls VBA. It can be used inside a function to return the cell calling the UDF, not the ActiveCell
Application.Caller property (Excel)
An easy example would be this:
Public Function ThisCell() As String
ThisCell = "ROW:" & Application.Caller.Row & " - COLUMN:" & Application.Caller.Column
End Function
If you type now in any cell =ThisCell() it will return its row and column:
Notice the output is different in all of them, even if they use the same UDF with no arguments.
A similar one to Foxfire And Burns And Burns' answer, but no vba needed.
Apply this formula, then copy it and paste as values.
="line = "&ROW()& " column= "&COLUMN()
Try below codes
Sub ExAddress()
MsgBox "Row: " & ActiveCell.Row & vbNewLine & _
"Column: " & ActiveCell.Column
End Sub
I'm trying to trim the headings of a table with VBA code because some of them have spaces in front that varies every month, which makes it difficult for coding.
When I break down the code and run it step by step it works fine but when I run the whole macro it removes some of my headings and replace them with info from a different sheet row or just "column 1", "column 2", etc.
I believe I'm missing some code reference when it calls the (" & .Address & ") selection?
It's replacing the headings from a sheet where the cell is active. (If it was the last cell to click on before running the macro).
I've tried just using the Trim function, but because it's an array for the range, it doesn't work, and someone suggested to use the "evaluate" function.
I've tried using the trim function as a WorksheetFunction as well but it gave me an error "Run-time error 13" Type-mismatch". Which was on the following code:
With wsDispoData.ListObjects("Table_DispoData").HeaderRowRange.EntireRow
.Value = WorksheetFunction.Trim(.Value)
End With
This is the current code I'm using that replaces wrongly.
Trim headings
With wsDispoData.ListObjects("Table_DispoData").HeaderRowRange.EntireRow
.Value = Evaluate("IF(ISTEXT(" & .Address & "),TRIM(" & .Address & "),REPT(" & .Address & ",1))")
End With
Expected results should be for example:
Current headings: " SOH" and " Compo"
Trimmed: "SOH" and "Compo"
I would just enumerate through the header row and check each value
Dim Cell As Range
For Each Cell In wsDispoData.ListObjects("Table_DispoData").HeaderRowRange
Cell.value = WorksheetFunction.Trim(Cell.value)
Next Cell
After a query is completed, I am inserting the following formula into the sheet with the query data using the vba code below. All works great on Excel Office365 but if the version of Excel is 2016 standalone the formula fails with a #NAME error as this function is not available in that version. I have some users that are stuck with it.
I know that a formula array could replace this, but I am not sure how to do this and insert it with code, as well as what the most efficient formula is that could replace this one.
=IF(OR(ISERROR(MAXIFS(Consumed!D:D,Consumed!B:B,A2)),
MAXIFS(Consumed!D:D,Consumed!B:B,A2)=0),"",
MAXIFS(Consumed!D:D,Consumed!B:B,A2))
Any help appreciated.
strInsertFormula = "=IF(OR(ISERROR(MAXIFS(Consumed!D:D,Consumed!B:B,A2)),MAXIFS(Consumed!D:D,Consumed!B:B,A2)=0),"""",MAXIFS(Consumed!D:D,Consumed!B:B,A2))"
With Sheet3
.Range("Individual_Bottles").Columns(.Range("EndRng").Offset(0, 1).Column).Insert Shift:=xlToRight
.Range("EndRng").Offset(-1, 1).Cells(1, 1).Value = "Last Drank"
.Range("EndRng").Offset(0, 1).Formula = strInsertFormula
.Range("EndRng").Offset(0, 1).NumberFormat = "yy/mm/dd"
End With
You can use the Excel Array formula =MAX(IF(B:B=A2,D:D,"")) to find the conditional maximum (you can add the extra error checks etc. around this). This will work in all versions from 2016 and earlier.
If you type this into a cell, you do need to press the usual Crtl+Shift+Enter (known as CSE).
If you want to enter this array formula via code, you need to set the FormulaArray property of the range to the above, NOT the Formula property. So your code should read:
.Range("EndRng").Offset(0, 1).FormulaArray = strInsertFormula
where strInsertFormula is the array formula I mentioned above.
#JohnF Well I got it working thanks to John F. I had expected that I would have to use a loop but was hoping not to. In the end I did, as I believe the FillDown method requires the range to be selected and I did not want to do that. Here is the final code
ii = .Range("EndRng").rows.Count
ConsRows = Consumed.Range("Consumed").rows.Count
For i = 1 To ii
strInsertFormula = "=IF(IFERROR(MAX(IF(Consumed!B1:B" & ConsRows & "=" & _
.Range("A1").Offset(i, 0).Address & _
",Consumed!D1:D" & ConsRows & ","""")),0)>0,MAX(IF(Consumed!B1:B" & ConsRows & "=" & _
.Range("A1").Offset(i, 0).Address & _
",Consumed!D1:D" & ConsRows & ","""")),"""")"
.Range("EndRng").Offset(0, 1).Cells(i, 1).FormulaArray = strInsertFormula
Next
End If
I am trying to put some vba code together to check if the contents in every cell from L2 down to the last row of data (until a blank cell is found) does not contain the string '8254' The number codes in column L are 27 characters long and always contain '8254' as the last 4 digits. So if I can verify '8254' in every cell then the format/code is correct (some number codes are incorrect and need to be investigated). If the string '8254' is not present in one or more of the cells, display a MsgBox warning there are error(s) in the column.
I have been trying to make the below code work, however I am new to vba and need some help
Does anyone know how I can do this?
Thanks
Sub CheckCodes()
'Check column 'L' for Non Standard Number Codes
Dim code As Range
For Each code In Range("L2", Range("L" & Rows.Count).End(xlUp))
If VBA.Right$(VBA.Trim$(code), 4) <> "8254" Then
MsgBox "Non Standard Number Codes Found! " & vbNewLine & "Check Number Codes ", , "ADVISORY!"
Exit Sub
End If
Next code
End Sub
I found the issue was that the code was checking a number of blank cells down to line L1000 and causing the issue. I modified as per below so it only checks to the bottom of the data and it is working fine. Thanks for all your kind help and comments.
Sub CheckCodes()
'Check column 'L' for Non Standard Number Codes
Dim code As Range
For Each code In Range("L2", Range("L" & Rows.Count).End(xlUp))
If VBA.Right$(VBA.Trim$(code), 4) <> "8254" Then
MsgBox "Non Standard Number Codes Found! " & vbNewLine & "Check Number Codes ", , "ADVISORY!"
Exit Sub
End If
Next code
End Sub
Using right function (assumes check is that 8234 are always last four digits):
Sub CheckCodes()
Dim code As Range
For Each code In Range("L2:L1000")
If VBA.Right$(VBA.Trim$(code), 4) <> "8234" Then
MsgBox "Non Standard Number Codes Found! " & vbNewLine & "Check Number Codes ", , "ADVISORY!"
Exit Sub
End If
Next code
End Sub
I'm trying to insert a concatenated string into a cell using VBA. Here's the formula I want to insert:
="ObjectID(" & E152 & ")"
here's what I'm trying, but I can't get it to work:
ActiveCell.FormulaR1C1 = "=""ObjectID("" & RC[-1] & ")"
I've tried "")" and ")"" and a bunch of other combinations, but I cant get it to work.
How do I do this?
here's how I did it:
ActiveCell.FormulaR1C1 = "=""ObjectID("" & RC[-1] & "")"""
Try this:
Sub InputConcatenatedString()
FormulaStr = """ObjectID(""&E2&"")"""
Range("A2").Formula = "=" & FormulaStr '--Modify A2 as needed.
Range("A2:A162").FillDown '--Modify affected range as needed.
End Sub
You just have to change the address of the initial cell as well as the end cell in the desired range. I just assumed that your data starts at A2 and ends at A162. :)
Let us know if this helps.
try this:
Range("A2:A162").Formula = "=""ObjectID("" & E2 & "")"""
Assuming you want to put your values in A2:A162.
Change to whatever range you got.