hei everyone,
am i missing something here?
i'm trying to compare two cells which are both formatted the same.
the way i compare it is such (never mind the "i" variable, the code is inside a for loop):
If SomeSheet.Cells(i, col).Value <> SomeOtherSheet.Cells(i, 4).Value Then
result = False
End If
I have tried converting the values to doubles, but my macro still sees them as different even thought the values are identical.
When I debug the code and try to see the actual values in the immediat window, they are ideed the same:
? SomeSheet.Cells(i, col).Value
310289286,463803
? SomeOtherSheet.Cells(i, 4).Value
310289286,463803
does anyone have any idea?
thanks!
EDIT: As suggested bu FaneDuru in the comments, I tried to round the values and even though i get the same output in the immediate window, the comparrison is correct now. Don't know why. Thanks!
If result has been defined as a boolean variable, the default value will be false. So unless there is another part of the code where result is set to true, It wil always return false.
Maybe try something like this:
If SomeSheet.Cells(i, col).Value = SomeOtherSheet.Cells(i, 4).Value Then
result = True
End If
Related
I am having an issue with VLookup in my VBA code. When I didn't have the range as a dynamic size using CurrentRegion it worked flawlessly, now for whatever reason, it only works for a single loop over and fails at the first VLookup. I have tried with and without the with block. I have a variable that will replace the 5 in the for loop and works but have kept it as its original so that isn't affecting it.
I apologize if this is a duplicate but I could not find an answer. Any help would be appreciated.
Set jobTypeData = Worksheets("JobType").Range("A1").CurrentRegion
For i = 1 To 5
jobTypeTemp = Worksheets("Employees2").Cells(i + 1, 16).Value
jobRoleTemp = Worksheets("Employees2").Cells(i + 1, 17).Value
'Creates variables relevant to the job type
With Worksheets("JobType")
minHours = CDec(WorksheetFunction.VLookup(jobTypeTemp, jobTypeData, 2))
maxHours = CDec(WorksheetFunction.VLookup(jobTypeTemp, jobTypeData, 3))
minShift = CDec(WorksheetFunction.VLookup(jobTypeTemp, jobTypeData, 5))
maxShift = CDec(WorksheetFunction.VLookup(jobTypeTemp, jobTypeData, 6))
shiftGap = CDec(WorksheetFunction.VLookup(jobTypeTemp, jobTypeData, 7))
End With
There is more code under this but it all works a-okay and worked fine before using the dynamic data size.
The code that works does not change the contents or size of jobTypeData.
I have also checked the values VLookup returns and they are all correct. The only thing I can think of is that it can't find the second jobType however I have checked and they are identical (no hidden spaces).
The answer appears to be related to the way VLookup uses 'approximate match'.
I always thought VLookup would still provide the exact match if it was an option and because I had strict data validation I never thought using approximate match would be a problem. However, turns out, it's not just mildly unpredictable, but actually unreliable.
After checking it was assuming "Role Name" was a closer match to "Team Manager" than "Team Manager". Upon changing it to using 'exact match', everything worked fine. No changes to the code which worked before either.
Well, lesson learned...
When you have an exact match ALWAYS use (no matter how confident you are, use me as an example):
VLOOKUP(arg1, arg2, arg3, FALSE)
Not:
VLOOKUP(arg1, arg2, arg3)
I have this code:
If cells(3,11).value="c:users\flap-fis.txt"
This condition is getting failed every time either we have file location of or not.
How can I write the code how to check specific cell value with static string?
Do you know how to debug VBA projects? Best thing to do for such a case is putting a breakpoint on that particular line and use the "immediate" window, where you can ask ? <var> in order to know the value of a variable, as in these examples:
? Cells(3, 11).Value = "c:users\flap-fis.txt"
False
? Cells(3, 11).Value
c:\users\flap-fis.txt
=> Mind the extra backslash, as already mentioned by PEH.
I am trying to remove duplicate values except the first one.
My solution to this was to conditionally format all duplicates, then, working backwards, clear contents of the formatted cells. This would mean that the first one will stop being formatted once all duplicates are removed.
What I have been trying:
For i = LaC To 5 Step -1
LR = ws.Cells(Rows.Count, LaC).End(xlUp).Row
For j = 2 To LR
cond = (ws.Cells(j, i).DisplayFormat.Interior.ColourIndex.Value)
If cond = 22 Then
ws.Cells(j, i).ClearContents
End If
Next
Next
Basically, if I try ws.Cells(j, i).DisplayFormat.Interior.ColourIndex in the immediate window, it returns 22.
However, if I try this code, I get error:
Object doesn't support this property or method (Error 438)
Any assistance would be greatly appreciated.
If it is not necessary to use VBA you can get rid of duplicates in the GUI via “Data” → “Data Tools” → “Remove Duplicates”. Or, in current versions of Excel O365, you can use the UNIQUE-function like this
UNIQUE(A:A)
assuming your source data is in row A. This will keep up with changing data.
Check this line more carefully:
cond = (ws.Cells(j, i).DisplayFormat.Interior.ColourIndex.Value)
ColorIndex is a Property, not an Object. As such, ColorIndex.Value will return an error. Note that you haven't included the .Value when testing in the Immediate Window, and it works as expected:
?ws.Cells(j, i).DisplayFormat.Interior.ColourIndex 'This will work
?ws.Cells(j, i).DisplayFormat.Interior.ColourIndex.Value 'This will err
Also, there is no need for you to put it in brackets, either:
cond = ws.Cells(j, i).DisplayFormat.Interior.ColourIndex
Ans: I was writing Colour instead of Color.
I have scoured the web and this site looking for an answer on this, so I would really appreciate some help.
I'm creating a VBScript to do some modifications to a user-specified Excel spreadsheet. I have the first part of my script working fine, but the second part is driving me nuts. I need it to search the first column for a value and, if found, delete the row. Right now I'm not worrying about the deletion statement--I'm doing testing by seeing if I can get the For Each statement to run properly as well as the If Then statement. Here's the specific block of code:
For Each cell in objSheet.Columns("A:A").Cells
Set cell = objSheet.Columns("A:A").Cells
If cell.Value = "60802400040000" then
cell.font.bold = True
End If
Next
I have tried many variations of this and cannot find the right combination. Initially I was getting an "Object Required" messages, and after reading a number of posts, found that I needed to put in a Set statement for cell, which I did. Now I am getting a Mismatch Type error message.
The funny thing is, before I put in the Set statement, the code would execute, but it would throw the Object Required error when I closed the spreadsheet. After adding it, the error for the Type Mismatch pops up immediately.
Most examples I keep finding on the web are for VBA, and I try to modify them for VBS, which I don't know very well. Any assistance anyone can give me will be greatly appreciated.
You are redefining cell, cell is defined automatically in the For Each statement.
Delete this line
Set cell = objSheet.Columns("A:A").Cells
This is an example from Help, unfortunately Help doesn't have any examples that uses For Each, only For x = n to n and other means. For Each is the right thing to do.
Set r = Range("myRange")
For n = 1 To r.Rows.Count
If r.Cells(n, 1) = r.Cells(n + 1, 1) Then
MsgBox "Duplicate data in " & r.Cells(n + 1, 1).Address
End If
Next n
For vba to vbs, you have to create the object and use, as some objects are automatically available in VBA (like app object) - Set exceldoc = CreateObject("c:\blah\blah.xls) then to use Set r = exceldoc.worksheets(0).range("MyRange").
Also you have to use constant values not names as vbscript can't look them up.
I am trying to perform some operations over some data, but they are not working and I need to find a way to ignore those cells which don´t meet all the requirements.
Basically, I have a column where some cells have text + numbers in their content and other have only text. I search inside all of them and split TEXT in one column and NUMBER in another one. Then, I run a macro to find the matching text to each one in other column.
But when I try to split TEXT from NUMBERS, I search for the first "(", cause my number format is (10.10.10), but if it is not found, the cell value appears: #VALUE! (ok, it is expected cause the character was not found). Here is the problem: if I run my macro over "#VALUE! it crashs and don´t finish its execution.
I have tried to use
On Error GoTo
in my macro code, but for some reason it doesn´t not handle the "Run time error: Type Mismatch".
For contadorOr = 2 To colO
For contadorDes = 2 To colA
On Error GoTo cont
If InStr(1, Cells(contadorDes, colunaDestino).Value, Cells(contadorOr, colunaOrigem).Value) Then
If InStr(1, Cells(contadorOr, colunaOrigem + 4).Value, Cells(contadorDes, colunaDestino + 1).Value) Then
Cells(contadorOr, colunaOrigem + 5).Value = "Mesma versão"
End If
Exit For
End If
Next contadorDes
cont: Next contadorOr
Any suggestions? I can think of ignoring this error (when it´s happen, my variable contadorOr is incremented and go to no next value) or any way to avoid #VALUE! returned by my functions, but haven´t had success doing that.
Thanks in advance.
Instead of using error handling options you could check if cell which you are going to check/process doesn't return error. This is quite simple as presented below:
If IsError(Cells(contadorDes, colunaDestino).Value) Then
'to do anything if there is error
'usually...do nothing
Else
'do what you want if there is no error
End if