In vba , only if there is an error with this line of code,…
- User
In vba , only if there is an error with this line of code, ActiveSheet.Range(selectedCellAddress).Select , I want ignore the error and select the first empty cell in column B
- Assistant
You can use the On Error Resume Next statement to ignore the error and then check if the selected cell is empty. If it is not empty, loop through the cells in column B until an empty cell is found, and select it. Here's an example code:
On Error Resume Next ActiveSheet.Range(selectedCellAddress).Select On Error GoTo 0 If Not IsEmpty(Selection) Then Dim lastRow As Long lastRow = Cells(Rows.Count, "B").End(xlUp).Row Dim i As Long For i = 1 To lastRow If IsEmpty(Cells(i, "B")) Then Cells(i, "B").Select Exit For End If Next i End IfNote that this code assumes that the selected cell is in the same worksheet as the code is running. If you want to select an empty cell in column B of a different worksheet, you need to specify the worksheet name in the Cells function.
- User
If there is no error, I want to omit the selection of the frst empty cell in column B
- Assistant
If there is no error and the selected cell is not empty, you do not need to do anything. Simply exit the subroutine or proceed with the rest of your code. Here's the updated code:
On Error Resume Next ActiveSheet.Range(selectedCellAddress).Select On Error GoTo 0 If Not IsEmpty(Selection) Then Dim lastRow As Long lastRow = Cells(Rows.Count, "B").End(xlUp).Row Dim i As Long For i = 1 To lastRow If IsEmpty(Cells(i, "B")) Then Cells(i, "B").Select Exit For End If Next i End If ' Proceed with the rest of your code hereNote that if the selected cell is empty, the code will still select the first empty cell in column B. If you want to avoid this, you can add an If statement to check if the selected cell is empty, and skip the loop if it is. Here's an example:
On Error Resume Next ActiveSheet.Range(selectedCellAddress).Select On Error GoTo 0 If Not IsEmpty(Selection) Then Dim lastRow As Long lastRow = Cells(Rows.Count, "B").End(xlUp).Row Dim i As Long For i = 1 To lastRow If IsEmpty(Cells(i, "B")) Then Cells(i, "B").Select Exit For End If Next i Else ' Code to execute if the selected cell is empty End If ' Proceed with the rest of your code here - User
In sheet Work Permit, i need a formula that will, Look at Column O, find all matches of Column O in column D of the sheet Permit DBS, and for all matches found in D, list downwards in Column O of the sheet Work Permit all the corresponding values in column A of the same row in the sheet Permit DBS without empty cells in the new list
- Assistant
You can use an array formula with the INDEX and IF functions to achieve this. Here's the formula you can use in cell O2 of the sheet Work Permit. Make sure to enter it as an array formula by pressing Ctrl+Shift+Enter:
=IFERROR(INDEX('Permit DBS'!A:A,SMALL(IF('Permit DBS'!D:D=O2,ROW('Permit DBS'!D:D)),ROWS($O$2:$O2))),"")This formula checks if there's an error in the formula and if so, it returns an empty string. The INDEX function returns the value in column A of the sheet Permit DBS, based on the row number generated by the SMALL function. The SMALL function generates a list of row numbers where the value in column D of the sheet Permit DBS matches the value in column O of the sheet Work Permit. The ROWS function is used to make the formula work as an array formula, by incrementing the row reference as the formula is copied down the column. Finally, the IF function checks if the value of the matched row is not empty, and it returns the value from column A of that row. If the value is empty, it returns an empty string.
- User
In sheet Work Permit, i need a formula that will, Look at 3, find all matches of 3 in column D of the sheet Permit DBS, and for all matches found in D, list downwards in Column O of the sheet Work Permit all the corresponding values in column A of the same row in the sheet Permit DBS without empty cells in the new list
- Assistant
To make the formula dynamic and refer to cell O3 as the lookup value, you can modify the formula as follows:
=IFERROR(INDEX('Permit DBS'!A:A,SMALL(IF('Permit DBS'!D:D=$O$3,ROW('Permit DBS'!D:D)-ROW('Permit DBS'!D$1)+1),ROWS($O$3:$O3))),"")This formula uses the absolute reference 3 to refer to the lookup value, and adjusts the row references in the formula accordingly. The row number generated by the SMALL function is adjusted by subtracting the row number of the first cell in column D of the sheet Permit DBS, and adding 1, to avoid returning zero. The ROWS function in the formula is adjusted to start from cell O3, and increment as the formula is copied down the column.
- User
it still only giving me the first value found when I drag the formula down
- Assistant
If the formula is returning only the first value, it may be because you did not enter it as an array formula. To enter the formula as an array formula:
- Select the range of cells where you want to enter the formula.
- Click on the formula bar and delete the existing formula.
- Paste the modified formula in the formula bar.
- Press and hold the Ctrl and Shift keys and press Enter.
- Once the formula is entered as an array formula, you can drag it down as needed.
If you have entered the formula as an array formula and it is still not working, please ensure you are changing the reference in the formula from
O2toO3when dragging it down. - User
I am dragging it down as an array but i still only shows the first value
- Assistant
If the formula is still not working, you may need to check the following:
-
Check if the value in cell O3 of the sheet Work Permit matches any value in column D of the sheet Permit DBS. If there is no match, the formula will not return any value.
-
Make sure that the values in column D of the sheet Permit DBS do not have any leading or trailing spaces or any other non-printable characters. You can use the TRIM function to remove any leading or trailing spaces in the values.
-
Make sure that there are no blank cells in column A of the sheet Permit DBS between the values that you want to retrieve using the formula. If there are any blank cells, the formula will return only the values above the blank cells.
-
Check if there are any other formulas or formatting in the cells below where you have entered the formula in the sheet Work Permit. If there are any other formulas or formatting, it may interfere with the results of this formula.
If you have checked all of the above and still cannot get the formula to work, you may need to provide more specific details about your data so that we can better understand the issue.
-
- User
Can i have a formula that will count cells in a column that have a value displayed and not be affected by the formula in the cells
- Assistant
Yes, you can use the COUNTA function to count non-blank cells in a column, regardless of whether the cells contain values entered directly or as a result of a formula. For example, if you want to count the number of non-blank cells in column A, you can use the following formula:
=COUNTA(A:A)This formula will count all non-blank cells in column A, regardless of whether the cells contain values entered directly or as a result of a formula. The formula will not be affected by any formulas in the cells.
- User
COUNTA is giving me a false count. I suspect the formula in the cells is affecting the count
- Assistant
Yes, if there are formulas in the cells, the COUNTA function will count cells that contain formulas as non-blank cells, even if the formula returns an empty string (""). To count only cells with visible values or results, you can use the SUBTOTAL function with function number 103. Here's an example formula:
=SUBTOTAL(103,A:A)This formula will count all non-blank cells in column A that have visible values or results, and will not count cells that contain formulas that return empty strings. The function number 103 is used to count visible cells only.
Note that the SUBTOTAL function excludes rows hidden by filters or other methods, so the count may change when you apply filters or hide rows manually.
- User
formulas are also affecting SUBTOTAL
- Assistant
If you have formulas in the cells that are affecting the result of the SUBTOTAL function, there are a few things you can try:
-
Convert the formulas to their values using copy-paste: You can copy the cells containing the formulas, then right-click on the destination cells, and select "Paste Special" > "Values" in the context menu. This will convert the formulas to their values, and the SUBTOTAL function will count based on the values only.
-
Use a helper column with a formula that evaluates the result of the original formula and returns either the value or an empty string. You can then use the SUBTOTAL function with function number 103 on the helper column.
For example, suppose you have formulas in column A that sometimes return an empty string when the formula result is blank. You can use a helper column with the following formula to evaluate the result of the original formula:
=IF(A1="", "", A1)This formula checks if the value in cell A1 is empty, and returns an empty string if it is. If the value is not empty, it returns the value in cell A1. You can then use the SUBTOTAL function with function number 103 on the helper column to count non-blank cells with visible values.
-
- User
I need a array formula that will search Sheet 'Service Schedules' Column H for dates that are 30 days before today. Get the values of Column H, Column J, Column K on the same row and Concatenate the result and list the results without spaces between the rows
- Assistant
You can use the following array formula to achieve this:
=TEXTJOIN("",TRUE,IF('Service Schedules'!H:H>=TODAY()-30,CONCAT('Service Schedules'!H:H,'Service Schedules'!J:J,'Service Schedules'!K:K),""))Here is how the formula works:
-
The IF function checks if the date in column H on the sheet 'Service Schedules' is greater than or equal to 30 days before today's date. If it is, the formula concatenates the values in column H, J, and K of the same row using the CONCAT function.
-
The CONCAT function concatenates the values in column H, J, and K of the same row in the format specified by the system.
-
The TEXTJOIN function concatenates the values returned by the IF function into a single cell without any delimiter.
-
The TRUE argument of the TEXTJOIN function tells Excel to ignore any blank values.
-
The formula is entered as an array formula by pressing Ctrl+Shift+Enter.
When you enter the formula in a cell, it will concatenate all the values that meet the specified condition and return one long string in a single cell. You can then use the "Wrap Text" option to view the results in multiple lines.
-
- User
I want the results for each match in H to be in a separate row without spaces between the rows when I drag the formula down
- Assistant
To display the results for each match in column H of the sheet 'Service Schedules' in a separate row without spaces between the rows, you can modify the formula as follows:
=IFERROR(INDEX(CONCATENATE('Service Schedules'!H:H,'Service Schedules'!J:J,'Service Schedules'!K:K),SMALL(IF('Service Schedules'!H:H>=TODAY()-30,ROW('Service Schedules'!H:H)-ROW('Service Schedules'!H$1)+1),ROW(A1))),"")Here's how this formula works:
-
The IF function checks if the date in column H on the sheet 'Service Schedules' is greater than or equal to 30 days before today's date. If it is, the formula returns the concatenated values in column H, J, and K in that row.
-
The INDEX function returns a value from an array. In this case, the array is the concatenated values in column H, J, and K of the sheet 'Service Schedules'. The ROW(A1) argument is incremented as the formula is copied down.
-
- User
Array works but the values of column H which are dates, are displayed in the array result as numbers. I would also like a double space between the concantated values
- Assistant
To display the values of column H as dates in the array result and add a double space between the concatenated values, you can modify the formula as follows:
=IFERROR(TEXT(INDEX(CONCATENATE('Service Schedules'!H:H," ",'Service Schedules'!J:J," ",'Service Schedules'!K:K),SMALL(IF('Service Schedules'!H:H>=TODAY()-30,ROW('Service Schedules'!H:H)-ROW('Service Schedules'!H$1)+1),ROW(A1))), "dd-mm-yyyy hh:mm:ss")&"","")Here's how this modified formula works:
-
The CONCATENATE function concatenates the values in column H, J and K of the sheet 'Service Schedules' with two spaces between each value.
-
- User
the date value is not being dispayed as dd-mm-yyyy
- Assistant
It is likely that your Excel settings use a different date format. To make sure that the date is formatted correctly, you can modify the formula to use the TEXT function to format the date as a string in the format "dd-mm-yyyy". Here's the modified formula:
=IFERROR(TEXT(INDEX(CONCATENATE('Service Schedules'!H:H," ",'Service Schedules'!J:J," ",'Service Schedules'!K:K),SMALL(IF('Service Schedules'!H:H>=TODAY()-30,ROW('Service Schedules'!H:H)-ROW('Service Schedules'!H$1)+1),ROW(A1))), "dd-mm-yyyy")&" ","")With this modification, the date will always be formatted as "dd-mm-yyyy", regardless of your Excel settings.
- User
dates still not formatted
- Assistant
I apologize for the confusion. It could be that the date value in column H is actually not a date value recognized by Excel. In that case, you can use the following formula to format the value in column H as a date before using the TEXT function to display it in the desired format:
=IFERROR(TEXT(INDEX(CONCATENATE(TEXT('Service Schedules'!H:H,"yyyy-mm-dd")," ",'Service Schedules'!J:J," ",'Service Schedules'!K:K),SMALL(IF('Service Schedules'!H:H>=TODAY()-30,ROW('Service Schedules'!H:H)-ROW('Service Schedules'!H$1)+1),ROW(A1))), "dd-mm-yyyy")&" ","")This formula formats the values in column H as a date using the TEXT function with the format "