I have an application that populates an Excel file with data. One of the cells being populated has Cell Validation on it based on a list. When the populated form is opened, I am seeing that my application populates the cell, the value is being seen as invalid, despite the value being in the list.
I am populating the cell value in this manner:
mainSheet.Cells[rowNum, colNum] = cellValue;
Does anyone know what information I am missing to set a cell value to a valid value?
I solved the problem myself. The code was populating the cell with a STRING value. The linked list to the drop down consisted only of INTEGER values. STRING <> INTEGER so the DV routines were reporting a failure. Populating the cell with an INT value resulted in success.
Related
I have in my userform listbox which display 10 columns. All rows have an individual ListIndex.
I would like to display f.e. in Msgbox value after select some row and click button "Show".
But this value is not avalible in listbox. So, I thought that all rows have individual ListIndex, I can use VLOOKUP for finding my target.
For example: I selected row no. 1 then ListIndex is 1 and this is my Look up Value. In my database sheet I have the same individual ID values.
Then of course, I have to declarate range, number of column and parameter "False".
Theoretically, I expect the result after that but it doesn't work.
My code:
Dim indexno As Long 'this is my delcaration for finding Look up Value
Dim myVLookupResult As Long 'this is my declaration for VLookup Result
indexno = ListBoxResult.ListIndex 'my Lookup Value is dynamic and depend of selected row
myVLookupResult = Application.VLookup(indexno, Worksheets("DataBase").Range("A1:J100"), 5, False)
MsgBox myVLookupResult 'should display result of VLOOKUP
But the result is error: Run-Time error: 13 - Type mismatch.
I guess the problem is with convert type of Dim from int to string.
Someone could support me, please? Thanks in advance!
The lookup value of a VLOOKUP worksheet function must be of the same data type as the data in which it is to be found. In your code the lookup value is of Long data type. If the column in which you are looking for it has text, the number you are looking for will not be found. So, you change the lookup value to Variant and hope that Excel will be able to work out what you want. But the better way is to examine your data column and look up the type of value you actually have there.
Next, given you are looking for a number which you have assigned to a Variant and Excel, in consequence, managed to find the string-equivalent of that number, that would be the functions return value, a text string. In most cases Excel is quite generous in this sort of situations but if it does complain about "Data Type" then it's because you are trying to assign a text string to a variable of Long data type.
Other than that, as #Michal Palko already pointed out, the ListBox's ListIndex is 0-based. If the numbers in your worksheet are 1-based the return of VLOOKUP won't be "totally different" but it will be from the adjacent row and therefore unexpected.
But I want to alert you to another possibility. As you know, you can load many columns into your list box. You can show some of the columns and hide others but you can access them all with code like this:-
With ListBox1
Debug.Print .List(.ListIndex, 3)
End With
This snippet will print out the value of the 3rd column in the row of ListIndex. You might also assign this value to a variable and, perhaps, have no need for VLOOKUP.
I want to clear the value of a specific cell. Currently my script runs roughly as follows:
cell_list = worksheet.range(1, 1, len(rows), column_length)
for cell in cell_list:
cell.value = rows[cell.row - 1][cell.col - 1]
worksheet.update_cells(cell_list)
I want just overwrite my old Data to save some API calls, therefore I don't use a worksheet.clear() before. Some of the new values could be None, these cells should be cleared then.
My Problem is:
If I have e.g in cell B2 the value "a" and I set now to B2 to the value "None" (cell.value = None) and I update the Worksheet "a" will stay in B2. So it didn't update the cell, the old value stay.
I could set B2 to an empty string, but the problem with that is, that that is not an empty cell, therefore If I want to sort after it (in the Browser) it will list it at the top because it is an empty string and not empty. If I then clear the cell manually in the browser and sort it again, it sorts it how I want to.
Updated description:
I have a cell list that I want to update with new values in my table. It can happen that a cell contains a value before, but the updated cell should not contain a value anymore.
So the old value of A1 is e.g. "5" and the new value for A1 should be nothing.
The problem is, if I assign the value None to the cell in the cell list, the cell will keep the old value after the update. So the value is still "5".
If I pass an empty string into the cell for updating, the new cell is empty, as desired. But now there is the problem that sorting doesn't work properly for me, so this is not an option for me. I could also clear the whole table before, but I try to avoid this to save API calls.
So I'm looking for a way to tell a cell that it does not contain a value
I think that the reason of your issue is worksheet.update_cells(cell_list). In gspread, it seems that the default value of ValueInputOption is RAW. I think that by this, such error occurs. So in order to avoid this, please modify as follows.
From:
worksheet.update_cells(cell_list)
To:
worksheet.update_cells(cell_list, value_input_option='USER_ENTERED')
By this modification, I think that "" which is the empty string can be used for clearing the cells.
References:
update_cells(cell_list, value_input_option='RAW')
ValueInputOption
I am trying to store all the values of an excel column in an array.
set rangeDate to {value of range "A14:A100"}
repeat with date in rangeDate
if (date as string is equal to "01/01/2001") then
log "It works"
end if
end repeat
In my Excel I do have an exact date of 01/01/2001 formatted in the specified columns. When I remove the range and it is just cell A14 (where the date is) it works. But when I include the range A14:A100 it doesn't work.
I am new to applescript, I guess that it doesn't store the values as array values and instead a string object? Any help would be appreciated
You have 4 issues :
1) value of range should not be between {}, but between ()
2) 'Date' is a reserved word in Applescript, so you should not use it as the variable in the loop. I replaced it with 'myDate'.
3) instead of converting your date to string to compare with "01/01/2001", it is quicker to keep comparing 2 dates, and then, compare with the date "01/01/2001"
4) I think it is a bug (at least with my Excel version), but the rangeDate variable is not a list of dates as expected, but for me a list of list : {{01/02/01},{02/02/01},………} Therefore, each member of 'rangeDate' is not a date, but a list made on one item which is a date ! I am not sure, but it could also be that range definition could be a list of ranges... So I am using item 1 of sub list.
Anyway, script bellow is working :
tell application "Microsoft Excel"
activate
tell active sheet of document 1
set rangeDate to (value of range "A14:A100")
repeat with mydate in rangeDate
set TheDate to item 1 of mydate
if TheDate = (date "lundi 1 janvier 2001 00:00:00") then
log "It works"
end if
end repeat
end tell
end tell
Quickly getting the values of a range of cells is great news! But even better is that you can fill in the values of a range by defining the value of that range. This is SO MUCH FASTER than doing it one cell at a time.
When I tried getting the value of a column (a range of cells), I received a list of lists. Each item in the list had only one value - that is the value of the cell.
To speed up complex operations, once you've got the list of values, take the process out of the "tell Excel" block and let AppleScript do the calculations. Then turn the result back into a list of lists and define the value of the range in Excel.
I had a problem reading ranges with some cells containing #VALUE! (failed formulas). I didn't find a solution on the Internet, so I thought it would be a good idea to share my solution here. Comments & improvement are surely welcome. I'm inclined to think there is a more straightforward solution to the problem than this. :)
Getting all values with value of range can lead to a problem messing up the output of the script. AppleScript doesn't consider a cell's content "#VALUE!" (= missing values) a value since it is, well, missing. Therefore the script doesn't include the cell's content in the list of values. This obviously messes up the cell order in the values list, since it has less items than the actual range has cells. In this situation it is quite impossible to return each value to its original cell in the workbook. Adding ”of ranges” to the code includes all cells with missing values solving the problem.
N.B. The values will be displayed as a one-dimensional array. Handling multi-column ranges requires more work. Nonetheless the missing values are included.
set celVals to (value of ranges of range "A1:A4")
E.g. {2.2.2022, 1.1.2011, missing value, 3.3.2033}
In order to return the values back to the workbook it is required to build back the list of lists. A missing value will be written to its cell as an empty string. Of course the original (failed) formula can be written instead, if needed.
N.B. again. This code applies to one column situation only. A little more is needed to put back a multi-column range. I'm sure you'll manage. :D
set returningCelVals to {}
repeat with i from 1 to count of celVals
set end of returningCelVals to {item i of celVals}
end repeat
set value of range ("A1:A4") to returningCelVals
EDIT: I knew there is a better solution. Here it is:
set celVals to string value of range "A1:A4"
String value gives a two-dimensional array of values and error messages of the range. String value gives also e.g. cell's currency symbols, so it is perhaps not suitable to all situations.
I am new to this forum and also new to programming.
I am trying to create a If condition in VBA where if the excel file has the required value then the file should get attached with a specific email or else no file should be attached ( in case the cell value is blank) and a different email should appear in the Email body.
I am trying to use "NULL" to represent the blank cell value. Is it correct?
Sample of my code -
if sheets("Hello").range("A2").value = Null, then
.attachments.remove
else
.attachments.add "C:\filename.xlsx
End if
If Sheets("Hello").Range("A2").Value = Null Then
is not the correct way to check for an empty cell.
One of the ways to test for an empty cell would be:
If Worksheets("Hello").Range("A2").Value = "" Then
which tests whether the cell, when converted to a String, is an empty string. (An equivalent statement to that would be
If Worksheets("Hello").Range("A2").Value = vbNullString Then
which can save you a few bytes of memory.)
A better way to test would be to use the actual IsEmpty function:
If IsEmpty(Worksheets("Hello").Range("A2").Value) Then
which tests to see whether the cell's value has a data type of Variant/Empty.
I have excel file that I need to locate a cell at.
This cell might have attributes like formatting, data validations rules etc. but no value.
I need to retrieve it even if the cell has (currently) no value since I want to maintain all the cell attributes and only set it a value.
Is it possible?
The code:
public static Cell GetSpreadsheetCell(WorksheetPart worksheetPart, string addressName)
{
return worksheetPart.Worksheet.Descendants<Cell>().
Where(c => c.CellReference == addressName).FirstOrDefault();
}
It seems this is not a real a problem.
Data validation is a side object that store references to cells it applies to. When a formula is inserted the cell always contain some value...