Excel: Output values from one column into one cell separated by semicolon - excel

I have a spreadsheet with one hundred rows. I have one column with a value in each cell.
For Example:
ABC
TRE
TED
CAR
I want an output that has all the values in one cell separated by a semicolon.
Any idea how to do this? I am new in Excel.
Output all in a single cell:
ABC; TRE; TED; CAR;

If your inputs are in cells A1, B1, C1 and D1 and your output formula for instance is E1 then use formula:
=CONCATENATE(A1,";",A2,";",A3,";",A4)

Alternatively (using the cells from the previous answer) you can try:
=A1&";"&B1&";"&C1&";"&D1

I figured it out. If anyone needs it. This writes to the first cell in the B column after it has been stored in the total variable.
Option Explicit
Sub Macro1()
Dim r As Range, cell As Range, mynumber As Long
Set r = Range("A1:A118")
Dim total As String
mynumber = 0
For Each cell In r
mynumber = mynumber + 1
total = total + cell.Value + ";"
Next
Cells(1, 2) = total
End Sub

Related

Excel 3D Reference: Delineate string results from range of Worksheets

Hopefully I can explain this right.
Looking to combine cells of text strings from multiple worksheets into one master worksheet.
Basically 3-D References. But formatted into rows and columns. And referencing a range of worksheets so new worksheets can be added or removed in between the bookends.
Desired output:
Column 1
Column 2
Column 3
WS01 Cell B1
WS02 Cell B1
WS03 Cell B1
WS01 Cell B2
WS02 Cell B2
WS03 Cell B2
WS01 Cell B3
WS02 Cell B3
WS03 Cell B3
Input: Strings from B1:B3 (should become matching rows separated into columns for each linked worksheet)
Each worksheet ('Worksheet 01:Worksheet 03') follows same format:
Column B
WS## Cell B1
WS## Cell B2
WS## Cell B3
Attempts:
=CONCAT('Worksheet 01:Worksheet 03'!B1:B3)
Result:
WS01 Cell B1WS01 Cell B2WS01 Cell B3WS02 Cell B1WS02 Cell B2WS02 Cell B3WS03 Cell B1WS03 Cell B2WS03 Cell B3
Please let me know what you think. Thank you for your time.
You can use:
=HSTACK(Sheet1:Sheet3!B1:B3)
Even though the answer below works fine, please look at this answer by JvdV that is far easier to use:
https://stackoverflow.com/a/74077560/12634230
=LET(
c,CONCAT(Sheet1:Sheet4!B1:B3),
q,SEQUENCE(LEN(c)/36,3,,12),
TRANSPOSE(MID(c,q,12)))
c uses your CONCAT formula to retrieve a concatenation of all values.
q calculates a sequence by the length of c divided by the length of text for the 3 values per Sheet (3* length 12 = 36) by 3 with steps of the length of each value (12).
This sequence is used in the MID function and needs the result to be transposed to meet your requirements:
If a Sheet will be added, changing the Sheet names in c will change the result to show the values from that Sheet as well. No further adjustments of the formula are required.
And if the number of outputs per sheet, or string length may change in future you could define these as variables too:
=LET(c,CONCAT('Worksheet 01:Worksheet 03'!B1:B3),
stringlength,12,
stringcount,3,
q,SEQUENCE(LEN(c)/(stringlength*stringcount),stringcount,,stringlength),
TRANSPOSE(MID(c,q,stringlength)))
#P.b just posted a formula approach, but as an alternative here's a VBA user-defined formula which returns an array. The only tricky part is getting the 3D reference in the UDF, since there's no structure or type equivalent to that in VBA: if you try to get it directly from the argument you just get an error.
Building from: https://www.excelforum.com/excel-programming-vba-macros/476283-user-defined-function-receiving-a-range-as-parameter.html
Function MyUDF(v)
Dim c As Range, f, arr, arrWs, rngAddr
Dim arrout, indx1, indx2, i As Long, r As Long, data
On Error Resume Next
Set c = Application.Caller
On Error GoTo 0
If c Is Nothing Then
f = "=myudf(Sheet1:Sheet3!A1:A3)" 'for testing purposes (adjust as needed)...
Else
f = c.Formula 'read the formula from the calling cells
End If
f = Mid(f, 8, Len(f) - 8) 'parse out the parens and formula name
arr = Split(f, "!") 'get an array from splitting on !
arrWs = Split(arr(0), ":") 'get the start/end worksheet names
indx1 = ThisWorkbook.Worksheets(arrWs(0)).Index
indx2 = ThisWorkbook.Worksheets(arrWs(1)).Index
rngAddr = arr(1) '...and the range address
'size the output array
ReDim arrout(1 To Range(rngAddr).Rows.Count, 1 To 1 + (indx2 - indx1))
For i = indx1 To indx2 'loop over the worksheets
data = ThisWorkbook.Sheets(i).Range(rngAddr).Value
For r = 1 To UBound(data)
arrout(r, i) = data(r, 1)
Next r
Next i
MyUDF = arrout 'return the array
End Function

How can I count cells in a row but only count adjacent cells once in Excel

Hi I have an spreadsheet with the following structure:-
I need help to figure out a formula or VBA function, to calculate the Activity Block Count column.
This is the count of non blank, contiguous cells in the date columns. For example Project 5 has 4 weeks of activity but only 3 continuous blocks of activity.
Any idea warmly welcomed ;-). I'm working at the weekend and it's driving me insane!
Thanks
Jonathan
Just walk through the cells in the range, noting changes in whether the cell(s) are populated or not.
Function countActivityBlock(rng As Range) As Long
Dim r As Range, bNum As Boolean
If Not IsEmpty(rng.Cells(1)) Then
bNum = True
countActivityBlock = 1
End If
For Each r In rng
If Not IsEmpty(r) And Not bNum Then
bNum = Not bNum
countActivityBlock = countActivityBlock + 1
ElseIf IsEmpty(r) And bNum Then
bNum = Not bNum
End If
Next r
End Function
SpecialCells(xlCelltypeConstants, xlNumbers) does not work within a UDF.
The formula way of doing this is using FREQUENCY as follows
=SUM(--(FREQUENCY(IF(D2:P2<>"",COLUMN(D2:P2)),IF(D2:P2="",COLUMN(D2:P2)))>0))
Must be entered as an array formula using CtrlShiftEnter
See This and This

how to insert n number of blank rows between existing rows alternatively

Can anyone suggest me procedure or a formula to insert n blank rows in between a existing data?
for example:
I have data from A1 to A50, now I have insert n number of blank rows between each row.
input:
required output:
Consider this screenshot:
In cell C3 enter the number of rows you want to insert between cells from column A. This cell has the range name "RowsToInsert". Alternatively, you can use $C$3 in the formulas below
The formula in cell E2 is
=INDEX(A:A,CEILING(ROW(A1)/(RowsToInsert+1),1))
or type the first value manually. The formula in E3 is as follows, copied down:
=IF(CEILING(ROW(A2)/(RowsToInsert+1),1)=CEILING(ROW(A1)/(RowsToInsert+1),1),"",INDEX(A:A,CEILING(ROW(A2)/(RowsToInsert+1),1)))
When you change the value in cell C3, blank rows are added accordingly. Copy the formula down as far as required. Then copy column E and use Paste Special > Values to paste only the results.
I would use VBA to achieve what you require and this is the code I would propose
Dim n As Long 'number of blanks
Dim dataLen As Long ' number of used rows
Dim currentRow As Long
Dim i, j As Long
dataLen = Sheet1.Range("A" & Rows.Count).End(xlUp).Row
n = 3 'number of blanks
currentRow = 2 'start insert at row 2
For i = 1 To dataLen
For j = 1 To n
Sheet1.Rows(currentRow).Insert
Next j
currentRow = currentRow + n + 1 'offset the next row to start insertion
Next i
With formula It will be little complicated I guess.
Is this what you want?
=IF(ROW(B1)-(9*TRUNC(ROW(B1)/9,0))=1,"one",IF(ROW(B1)-(9*TRUNC(ROW(B1)/9,0))=4,"two",IF(ROW(B1)-(9*TRUNC(ROW(B1)/9,0))=7,"three","")))

if / then formula over many rows

I am trying to produce a code (simple for some) but I am inexperienced and would appreciate help.
Code to look at cell ("J1") and doing an if / then for a result in ("K1"), but I want to have this duplicated to look at the range J2 to J10 cells , to give the result in the range K2 to K10 cells as well.
The code below works for single row formula:
Sub Check()
Dim DDDD As Date, result As String
DDDD = Range("j1").Value
If DDDD >= Date Then
result = "Future"
Else
result = "Now"
End If
Range("k1").Value = result
End Sub
You can use a For Each loop and the Offset method, like this:
Sub Check()
Dim DDDD As Date, result As String
Dim cell as Range
For Each cell in Range("J1:J10")
DDDD = cell.Value
If DDDD >= Date Then
result = "Future"
Else
result = "Now"
End If
' Change value at the right side of the cell
cell.Offset(0,1).Value = result
Next cell
End Sub
But using VBA for this is really overkill. You could just put this formula in K1:
=IF(J1 >= TODAY(), "Future", "Now")
... and then copy/drag that formula down to the other cells below it.
Here is a piece of code that loops through Column J, and the result will be placed at Column K.
Sub Check()
Dim DDDD As Date
Dim result As String
Dim row, NumofRows As Integer
' use NumfofRows as a Dynamic value to decide how many rows of data you want to look at column J
NumofRows = 10
For row = 1 To NumofRows
If Cells(row, 10) >= Date Then
result = "Future"
Else
result = "Now"
End If
Cells(row, 11).Value = result
Next
End Sub
In the future, take a look at the rules of etiquette that make your questions both fun and enticing to answer. For your question, it will be more efficient to deal with Ranges rather than individual cells with dates in them. The For Each...Next loop can cycle through each cell in the range. Then, an If...Then...Else...End If loop can make a decision given various conditions. The Offset(0, 1) method can be used inside the If...Then loop to place results in the column to the right. The following code does what you seem to be asking:
Sub Check()
' Declare your variables
Dim MyDates As Range
Dim result As String
' First, grab all the values in the J2:JX column, where
' X can be any number of dates you choose. Note that
' you must "Set" Object variables, but you must "Dim"
' other types of variables.
Set MyDates = Range(Range("J2"), Range("J2").End(xlDown))
' Loop through every cell in the dates column and give the
' offset column (column K) a string value if the condition
' is met.
Dim Cell As Range
For Each Cell In MyDates
If Cell >= Date Then
result = "Future"
Cell.Offset(0, 1).Value = result
Else
result = "Now (or past)"
Cell.Offset(0, 1).Value = result
End If
Next Cell
End Sub
The End(xlDown) method emulates selecting a cell and pressing Ctrl+Down arrow. Also, note that this code does not handle mistakes at all. For example if you accidentally enter a date as text, the procedure will produce an incorrect result. Also, depending on the time you enter data, you could get confusing results. I've attached an image of the results in Excel here:

Use CELL on an array to return string made of types of the cells

Lets say we have 5000 rows with random values (blanks, numbers, characters). I need to show type of all the cells in these 5000 rows in a single cell using a formula. Is it actually possible? I've tried to CONCATENATE(CELL("type";array)) and ctrl+shift+enter but it didn't work (it returns the type of the first cell in the array).
If you want to know, this is for finding a cell with text rather than values or blanks in a very big file. Maybe you have a better solution.
Thanks in advance.
UPD: thanks for macros but I can't use them in this workbook, I need a formula-solution.
UPD: I've got how to do it with conditional formatting => new rule => use a formula to determine... => use ISTEXT('first cell of the range') formula
But still, is it possible to create the formula?
The best way to go about this is to use MACROs
Here is my sample code:
Sub Button2_Click()
numRows = 10 ' Number fo rows to loop through, in your case 5000
'loop through each cell located in column 1
'Check its type
'Concatenate each one in 1 cell on the 8th column
For i = 1 To numRows
Sheet1.Cells(1, 8).Value = Sheet1.Cells(1, 8).Value & TypeName(Sheet1.Cells(i, 1).Value) & ","
Next i
End Sub
You can adapt this small user defined function to your needs.
Say we are looking at cells in the first row, from A1 through D1 and we want to concatenate their types into a single cell. Enter the following UDF() in a standard module:
Public Function KonKaType(rIN As Range) As String
Dim r As Range
For Each r In rIN
s = "cell(""type""," & r.Address(0, 0) & ")"
KonKaType = KonKaType & Evaluate(s)
Next r
End Function
and then in the worksheet cell E1 enter:
=KonKaType(A1:D1)

Resources