Why does Excel suddenly find something in a cell? - excel

I have a vba function that receives a row from a workbook and looks in come columns for data, setting variables or not depending on what is found. For rows 2-16, everything works fine; empty cells get skipped. Suddenly on row 17, a cell which seems empty triggers the first if-condition.
I've tried adding an additional check for cells that contain an empty string, and in the worksheet itself I checked CODE(H17) which was #VALUE
Function calcID(r As Long) As Variant
If (Not IsEmpty(allProps.Cells(r, 8))) Or (Not allProps.Cells(r, 8).Value = "") Then
MsgBox "Found ID: " & allProps.Cells(r, 8).Value & " in allProps row " & r
calcID = CDate(allProps.Cells(r, 8).Value)
ElseIf Not IsEmpty(allProps.Cells(r, 9)) Or Not allProps.Cells(r, 9).Value = "" Then
MsgBox "Found reverse ID: " & allProps.Cells(r, 9).Value & " in allProps row " & r
calcID = CDate(allProps.Cells(r, 9).Value)
Else
calcID = ""
End If
End Function
I use CDate elsewhere and it works fine. Ultimately the error shows up once I'm inside the if because I think CDate doesn't have a string to work with.

I think you only want to process the cells if they are both non-empty AND not equal to emptystring. This will prevent strange behavior if a cell is somehow not empty but is equal to emptystring.
Change your logical ORs to ANDs.
Function calcID(r As Long) As Variant
If (Not IsEmpty(allProps.Cells(r, 8))) And (Not allProps.Cells(r, 8).Value = "") Then
MsgBox "Found ID: " & allProps.Cells(r, 8).Value & " in allProps row " & r
calcID = CDate(allProps.Cells(r, 8).Value)
ElseIf Not IsEmpty(allProps.Cells(r, 9)) And Not allProps.Cells(r, 9).Value = "" Then
MsgBox "Found reverse ID: " & allProps.Cells(r, 9).Value & " in allProps row " & r
calcID = CDate(allProps.Cells(r, 9).Value)
Else
calcID = ""
End If
End Function

Some of the value in the cell which starts with = is taken as formula in excel.
For eg : =test is taken as a formula with error like this in excel.
I do had a similar issue and sorted it out by comparing the value in the cell and trimming off unwanted = in cell value.
Please make sure you are not getting any value in that cell which makes excel think that it is a formula or cell reference.

Related

Adding freshly created formula into new module

I've just created a brand new macro. Took function down below from internet (all credits goes to trumpexcel.com), code down below
Function CONCATENATEMULTIPLE(Ref As Range, Separator As String) As String
Dim Cell As Range
Dim Result As String
For Each Cell In Ref
Result = Result & Cell.Value & Separator
Next Cell
CONCATENATEMULTIPLE = Left(Result, Len(Result) - 1)
End Function
Then I proceed to extract data from various columns and into the one (my table is 20 rows x 10 columns)
Sub conact_data()
Dim i As Integer
For i = 2 To Cells(Rows.Count, "A").End(xlUp).Row
Cells(i, "M").Value = Cells(i, "A").Value & " " & _
Cells(i, "B").Value & " / " & Cells(i, "D").Value & "; "
Next i
End Sub
Thanks to that I've got combined data from column A, B and D, so its 20 rows. All I want to do now is to concatenate data from M2:M21 using CONCATENATEMULTIPLE function therefore I try various approach (I want this huge line in P2 cell) like :
Cells(2, 16).Value = CONCATENATEMULTIPLE (M2:M21, " ")
or
Range("P2") = "CONCATENATEMULTIPLE (M2:M21, " ")"
I don't really know how to apply that
Secondly, I'd like withdraw the Cells(i, "B").Value as percentage. Can I do that in one line like Cells(i, "B").NumberFormat="0.00%".Value (which is not working for me obviously) else I need to copy column B into another column with number format and then combine the new column, properly formatted instead of column B?
Thanks in advance
Percent format: Range("B" & i).NumberFormat = "0.00%"
CONCATENATEMULTIPLE
In VBA, CHR(32) = " "
In Excel, CHAR(32) = " "
With that being said...
'Value
Range("P2").Value = CONCATENATEMULTIPLE(Range("M2:M21"), CHR(32))
'Formula
Range("P2").Formula = "=CONCATENATEMULTIPLE(M2:M21, CHAR(32))"
You should really qualify all of your ranges with a worksheet
Say your workbook has 10 sheets. When you say Range("P2"), how do we (VBE) know what sheet you mean? Objects need to be properly qualified. Sometimes this is not a huge issue, but when you are working across multiple sheets, not qualifying ranges can lead to some unexpected results.
You can qualify with a worksheet a few ways.
Directly: ThisWorkbook.Sheets("Sheet1").Range("P2").Copy
Or use a variable like so
Dim ws as Worksheet: Set ws = ThisWorkbook.Sheets("Sheet1")
ws.Range("P2").Copy
Now there is no room for ambiguity (potential errors) as to the exact location of Range("P2")
First of all, remove your ConcatenateMultiple() code, and instead use Excel worksheet function CONCAT(), which takes a range and a delimiter as parameters.
Here is how you can handle the percentage issue and supply a default for non-numeric items. I've also cleaned up the way you reference your data range.
Sub concat_data()
Dim rngRow As Range, vResult As Variant
Const DEFAULT = 0 'Can also be set to a text value, eg. "Missing"
For Each rngRow In [A2].CurrentRegion.Rows
If IsNumeric(rngRow.Cells(, 4)) Then vResult = rngRow.Cells(, 4) * 100 & "%" Else vResult = DEFAULT
Range("M" & rngRow.Row) = rngRow.Cells(, 1) & rngRow.Cells(, 2) & "/" & vResult & ";"
Next
[M2].End(xlDown).Offset(1).Formula = "=CONCAT(M2:M" & [M2].End(xlDown).Row & ",TRUE,"" "")"
End Sub
I'm not a fan of hard-coding range references, like the [A2] or Range("M"), but will leave that for another time.

VBA if conditions

I wrote if condition which is shown below, it looks for value "Current Status:" in row A and copy B value from that row to other sheet, if not not found "0" is placed in a cell, it works fine. Sometimes value "Current Status:" might be in a different cell than A18, it might show up in the range from A16 to A20, how can I modify that code to find it within the range and copy corresponding value?
If ws.Range("A18") = "Current Status:" Then
.Range("V" & NewRow) = ws.Range("B18")
Else
.Range("V" & NewRow) = "0"
End If
Just put your code in a For loop... or use VLookup like Scotty suggested. It's basically the same thing. A For loop is more flexible but less optimized (VLookup is faster). They both run on the order of fractions of a μs/cell.
For Each c In Range("A16:A20")
If c.Value2 = "Current Status:" Then
.Range("V" & NewRow) = c.Offset(0, 1)
Exit For
Else
.Range("V" & NewRow) = "0"
End If
Next
If using a For loop, this is a little bit more code than what's above but a better structure...
'Define a value holder variable where it's scope makes sense
Dim NewValue As String
'... other code here ...
'Default: NewValue = ""
NewValue = ""
For Each c In Range("A16:A20")
If c.Value2 = "Current Status:" Then
NewValue = c.Offset(0, 1)
'Exit For is optional in this case. It matters if
'there are multiple matches... do you want first or last result?
Exit For
End If
Next
'Assign NewValue to cell
.Range("V" & NewRow) = NewValue
Use Vlookup:
.Range("V" & NewRow) = "0"
On Error Resume Next
.Range("V" & NewRow) = Application.WorksheetFunction.VLookup("Current Status:", ws.Range("A:B"), 2, False)
On Error GoTo 0
This will put 0 in the cell then try to replace it with the value returned from the vlookup. If "Current Status:" is not found in column A on ws then it will throw an error and be ignored leaving 0 in the cell.
If the value is found it will return the value in Column B and put that in place of the 0

VBA 255 Maximum Characters in 1 Cell

I have a simple macro which adds the contents of each row in an excel sheet to a text file, with delimiters in between each cell value. This is done by running a for loop which iterates through each row and at the end of each iteration the values are added to the end of a String variable.
Each ROW can have a lot of characters in it - I have not noticed any issues with that. However, when 1 single cell contains more than 255 characters, the concatenation fails. I am not sure if it is because of String limitations (I don't think that is the case), or if it is the Trim, Join, or Index functions that contains this limitation, or if it something else. Any help in getting some more insight would be appreciated.
The line in question ('R' refers to the row/iteration number):
stringVariable = stringVariable & vbNewLine & Application.Trim(Join(Application.Index(Cells(R, "A").Resize(, 25).Value, 1, 0), "|"))
The error is:
Run-time error '13': Type mismatch
The problem is with the Application.Index. How to debug?
Let's have the active sheet with any values in row 1, all with less than 255 chars. But in one of this cells in row 1, for example in C1, should be the formula:
=REPT("c",255)
Now split the code into parts:
Sub test()
r = 1
v2DArray = Cells(r, "A").Resize(, 25).Value
index1DArray = Application.Index(v2DArray, 1, 0)
joinString = Join(index1DArray, "|")
stringVariable = Application.Trim(joinString)
MsgBox stringVariable
End Sub
This will work until you change the formula to =REPT("c",256). Now it will fail at Application.Index.
Instead of the Application.Index you could do the following:
Sub test2()
r = 1
v2DArray = Cells(r, "A").Resize(, 25).Value
ReDim v1DArray(LBound(v2DArray, 2) To UBound(v2DArray, 2)) As String
For i = LBound(v2DArray, 2) To UBound(v2DArray, 2)
v1DArray(i) = v2DArray(1, i)
Next
joinString = Join(v1DArray, "|")
stringVariable = Application.Trim(joinString)
MsgBox stringVariable
End Sub
After experimenting using different combinations of the already present functions, I found that the macro finishes without issues when Index is not used.
In the end I decided to add the value of each cell to the end of the string, one at a time. Solution:
For i = 1 To numberOfColumns
If i < numberOfColumns Then
stringVariable = stringVariable & Trim(Cells(R, i).Value) & "|"
Else
stringVariable = stringVariable & Trim(Cells(R, i).Value)
End If
Next i
stringVariable = stringVariable & vbNewLine

Concatenating SumIfs in VBA

I'm trying to get this segment of code to execute. This is a simplified version of the code. I've included the relevant code. I'm trying to concatenate strings and named ranges into a SumIfs formula, but I get error 1004 "Application-defined or Object-defined error." I have a working line of code above this problem section that is similar with the exception of doing a sum function, instead of sumif. Any idea how to get this code to execute? Thank you.
Dim wb As Workbook
Dim ara As Worksheet
Dim inv As Worksheet
Dim ARBlock As Range
Dim Invoices As Range
Dim AgedDays As Range
Set wb = ThisWorkbook
Set ara = wb.Sheets("AR Aging")
Set inv = wb.Sheets("Invoices")
Set ARBlock = ara.Range("a6")
Set Invoices = inv.Range("a6", inv.Range("a6").End(xlDown))
Set AgedDays = Invoices.Offset(0, 6)
'Populate A/R age buckets
For i = 6 To ARBlock.Rows.Count + 6
With ARBlock(i - 5, 1).Offset(0, 3)
.Value = "=SumIfs(" & Invoices.Offset(0, 4).Address & "," & _
Invoices.Address & "," & ARBlock(i - 5, 1).Value & "," & _
Invoices.Offset(0, 6).Address & ","" <= "" & &O30)"
End With
Next i
End Sub
The line beginning with ".value" is where I'm getting the error message. P.S.: I need the cell to contain the concatenated formula, as opposed to the output value.
UPDATE 1:
As some suggested I updated the .value line to:
.Offset(0, 3).Formula = "=SumIfs(Invoices.Offset(0, 4).Address,Invoices.Address,ARBlock.cells(i - 5, 1).Value)"
I'm still getting the same error. Some auditing I've done:
Removing the "=" before "Sumifs" allows the code to run fine; pasting in the formula into the target cell as text. In this form, my output for i=1 goes to ARBlock.cells(1,1), as it should.
I also used Debug.Print to view all of the components of the formula:
Debug.Print ARBlock.Cells(i - 5, 1).Address
'output $A$6
Debug.Print ARBlock.Cells(i - 5, 1).Value
' output International Business Machines
Debug.Print Invoices.Offset(0, 4).Address
'output $E$6:$E$255
Debug.Print Invoices.Address
'output $A$6:$A$255
I suspected the issue might be that the range dimensions might have been off, but this is not the case. My next suspicion is that the output International Business Machines needs to be in " " for the formula to read it correctly. I hardcoded in
""International Business Machines""
to see if this would fix the formula, but I keep getting the same error once I add the "=" back in. The formula syntax is correct, the dimensions are the same between the sum range and criteria range. Anyone else have any ideas?
.Offset(0, 3).Formula = "=SumIfs(Invoices.Offset(0, 4).Address,Invoices.Address,ARBlock.cells(i - 5, 1).Value)"
Change to:
.Offset(0, 3).Formula = "=SumIfs(" & Invoices.Offset(0, 4).Address & ", " & Invoices.Address & ", " & chr(34) & ARBlock.Cells(i - 5, 1).Value & chr(34) & ")"
EDIT: Added quotes chr(34) around your string!
Your ARBlock(i - 5, 1).Value most likely is an empty cell, which messes the SUMIFS formula as it builds it with to consecutive commas

storing a cell reference in a variable and accessing that cell's value

i'm trying to get the value of one cell in a column (column B). this cell is in the same row as the last cell which has a value in another (column A)
maxrowina = ws.Range("A1").Offset(ws.Rows.Count - 1, 0).End(xlUp).Row
maxcellinb = "B" & str(maxrowina)
MsgBox (ws.Range(dbhobcell).Value)
but i keep on getting a runtime error: 91.
how can i do this?
Two things :)
1) You don't need str(maxrowina). You can use this
maxcellinb = "B" & maxrowina
2) Shouldn't MsgBox (ws.Range(dbhobcell).Value) be
MsgBox (ws.Range(maxcellinb).Value)
Complete Code
maxrowina = ws.Range("A1").Offset(ws.Rows.Count - 1, 0).End(xlUp).Row
maxcellinb = "B" & maxrowina
MsgBox (ws.Range(maxcellinb).Value)
Just a little tighter on the MaxRow code:
MaxRowInA = ws.Range("A" & Rows.Count).End(xlUp).Row
MsgBox ws.Range("B" & MaxRowInA).Value
Tigher still:
MsgBox ws.Range("A" & Rows.Count).End(xlUp).Offset(,1).Value

Resources