Suppose there are 3 columns. In 1 column number (a1), in the second name (b1), in the third description (c1). I want to make another 1 table, so that the data would spread to it, but with 1 condition. If in the 3 column, in one cell more than 500 characters, then continue to copy the data from the rows to the next cell. (For example, there is a line a2, b2, c2 In cell c2, 600 symbols are obtained, then the remaining characters are transferred to one cell, c3 and copy the text from cells a2, b2) Is it possible to trace it? To make, some counter, that he would count the symbols in the cell.
enter image description here
table
tabletest1
Well, i think this image will be self explanatory
First table contains your original data. Right upper one contains the results, and bottom one contains the formula
The formula =IF((LEN(C2) > 5);MID(C2;6;500);" ") checks the length of the text inside a cell, if its greater than 5 (you can modify this of course), then set the value of the cell to a substring (from 6th character) of the original text.
VB Macro
After some tries, i got a VB macro which makes that
Sub copyTable()
Dim colRange As Variant
colRange = Array(1, 2, 3) 'Columns where your data is'
Dim destColRange As Variant
destColRange = Array(5, 6, 7) 'Columns where you want data be copied'
n = UBound(colRange) - LBound(colRange)
i = 2 'Initial row'
newI = i
maxLen = 10 'Maximum size allowed in one cell'
While Not (Cells(i, colRange(0)) Is Nothing) And (Cells(i, colRange(0)) <> "")
Text = Cells(i, colRange(n))
Do
For j = 0 To n - 1
Cells(newI, destColRange(j)) = Cells(i, colRange(j))
Next j
Cells(newI, destColRange(j)) = Mid(Text, 1, maxLen)
Text = Mid(Text, maxLen + 1)
newI = newI + 1
Loop Until Len(Text) <= 0
i = i + 1
Wend
End Sub
Variables explanation
colRange: Indexes of columns with origin data
destColRange: Indexes of columns where data will be written
i: Initial row
maxLen: Maximum length allowed per cell
Note that this snippet only cares about size of the last cell, if any other has more length it will copy them as they are.
when you run it you got something like
I've never made a VB code, so don't be critic, it surely can be better
Related
I have a chart with a text-column with numerous entries per cell.
Entries are separated with “;”.
Entries have the format “xy 00/00” (e.g. “AB 03/18”).
I need Excel to find and give in the next column a specific entry I predefine per row (above the column, example below).
Only the first two and last two characters are defined, the characters in the middle can be whatever (e.g. “AB ??/18”).
A cell can have more than one entry with the definition of “AB ??/18” (e.g. “AB 03/18” & “AB 08/18” etc.).
I need to know, if there are more than 1 of this predefined entries.
If I change the search box to “ZZ ??/12”, it should overwrite the previous defined search and give me back only the ZZ… ones.
For example:
Screenshot Chart
I tried a formula, but it gives me the first AB…, not the rest.
If it is only possible to give back the amount of the searched text in the cell above, that would also be ok.
Your screenshot doesn't seem entirely consistent with your objective, i.e.
the pattern AB ##/18 can be found 3 times in the string
blabla WF 12/23; AB 08/18; AB 09/18; AB 08/18
but your count column registers only 1 result (for AB 08/18)- there is also a match in the 1st row (for AB 12/18), but there you have a count of 0...
The code below assumes that the 4 data cells from your screenshot are in the range A3:A6 and that they are not part of a table
Sub txtMatching()
Dim results As String, cell As Range, incidence As Integer, pattern As String, pos As Integer, temp As String
pattern = "AB ##/18"
For Each cell In Range("A3:A6")
pos = 1
If cell.Value Like "*" & pattern & "*" Then
Do
pos = InStr(pos, cell.Value2, Mid(pattern, 1, InStr(1, pattern, "#") - 1))
If pos = 0 Then Exit Do
temp = Mid(cell.Value2, pos, Len(pattern))
If temp Like pattern Then
results = results & temp & "; "
incidence = incidence + 1
End If
pos = pos + Len(pattern)
Loop While pos < Len(cell.Value2)
cell.Offset(0, 1).Resize(1, 2).Value2 = Array(Mid(results, 1, Len(results) - 2), incidence)
results = vbNullString
incidence = 0
Else
cell.Offset(0, 2).Value2 = 0
End If
Next cell
End Sub
I want to put a Sum formula in a row of a table, in columns C to H, but the code I’ve come up with somehow doesn’t work. The situation is as follows:
the number of the 1st row of the table varies (the 1st column is
always B)
the number of the 1st row in the formula varies, but is always the 3rd row of the table
the number of the row that should contain the formula varies, but in the macro I calculate that number relative to the 1st row of the table
the number of the last row in the formula varies, but is always 1 less than the number of the row that should contain the formula
To be more specific and hopefully more clear, let’s say that:
the number of the first row of the table = startnum
then the number of the 1st row in the formula = startnum+3
the number of the row that should contain the formula = startnum+x
then the number of the last row in the formula = startnum+x-1
Trying to find out what my code could be, I recorded a macro. Based on that I have tested the following code:
With Worksheets("A&N")
.Range("C16:H16").Formula = "=SUM(C7:C15)"
End With
This works fine, but as I’ve described, the numbers 16, 7 and 15 are actually variable.
I’ve tried to translate this code to my situation, and made this code:
Set rngOpmaak = Range(rngTabel.Cells(startnum + x, 2), rngTabel.Cells(startnum + x, 7))
rngOpmaak.Formula = "=SUM(“C” & startnum + 3 & “:C” & startnum + x -1)"
When I run the macro I get the message that the second line can’t be compiled. I’ve seen solutions on this site that to me look exactly like my code, so I don’t understand what’s wrong with mine.
I’ve also tried:
rngOpmaak.FormulaR1C1 = "=SUM(R" & startnum + 3 & "C:R" & startnum + x -1 & "C)"
But with startnum=2 (1st row of the table) the formula becomes =SUM(C$3:C$5) to =SUM(H$3:H$5) (without the quotations) instead of =SUM(C4:C6) to =SUM(H4:H6).
Can anyone help me with what the line of code should be? All suggestions are much appreciated.
You'll need to adjust this to your situation, as I didn't use the same variables you did (preferring more expressive ones):
Sub test()
Dim output As Range, dataStart As Integer, dataEnd As Integer, rowsOfData As Integer, nCols As Integer
' Row 1: Table headers
' Row 2: 1st row of data
' Row 3: more data
' Row 4: more data
' Row 5: last row of data
' Row 6: Summary row (location of formula)
dataStart = 2
dataEnd = 5
rowsOfData = 1 + dataEnd - dataStart
nCols = 4
Set output = Range(Cells(dataEnd + 1, 1), Cells(dataEnd + 1, nCols))
output.FormulaR1C1 = "=SUM(R[-" & rowsOfData & "]C:R[-1]C)"
End Sub
The generated formula uses relative references, as it was given offsets ([]) from the current cell, rather than absolute R/C values, e.g.
R2C:R5C -> A$2:A$5 no matter where in column A the formula is entered
R[-4]C:R[-1] -> A2:A5 if the formula is entered in A6.
I have a spreadsheet in which there are multiple rows that have three columns (K, L M) that contain text (inserted manually from a dropdown). The inserted text includes a 'score'. For the row shown in the image that score is 3 + 2 + 2 = 7.
What I'd like to be able to do is to have that score automatically calculated and shown in column N. I'm happy to do the score extraction given the text, but I'm completey unfamiliar with Excel's object model, and how to write a VBA macro that can be triggered across all of the rows. I assume it would be passed a range somehow, or a string designating a range, but how to do that is beyond me just now. Perhaps I just need a formula? But one that calls a function to strip non-numerical data from the cell?
Any help appreciated.
Put this formula in N2 cell and drag it all the way down.
=LEFT(K2, FIND("-", K2) - 2) + LEFT(L2, FIND("-", L2) - 2) + LEFT(M2, FIND("-", M2) - 2)
For more information see reference. It sum all numbers, that are present before the hyphen (-) in a cell.
Try:
N2 = LEFT(TRIM(K2),1) + LEFT(TRIM(L2),1) + LEFT(TRIM(M2),1)
As I said in comments, this solution does not scale so well if it is more than three columns and / or the scores are more than single digit [0-9]
A VBA solution to do all of your rows and enter the values into Column N:
Sub foo()
Dim ws As Worksheet: Set ws = Sheets("Sheet1")
'declare and set your worksheet, amend as required
LastRow = ws.Cells(ws.Rows.Count, "K").End(xlUp).Row
'get the last row with data on Column A
For rownumber = 1 To LastRow 'loop through rows
For i = 11 To 13 'loop through columns
strValue = ws.Cells(rownumber, i).Value 'get text string from cell
pos = InStr(strValue, " -") 'find the dash - in cell
If pos > 0 Then 'if dash found
Value = Value + Val(Left(ws.Cells(rownumber, i).Value, pos - 1)) 'remove everything after number
End If
Next i
ws.Cells(rownumber, 14).Value = Value 'write value to column N
Value = 0
Next rownumber
End Sub
This should be very straightforward but for some reason I'm having issues. The code should look at a particular cell, if it meets the criteria of the IF statement, write the data to another cell.
dim i as integer
i = 1
For i = 1 To 674
If Sheets("Provider Data").Cells(i + 3, 60).Value2 = 8 Then
Sheets("Sheet1").Cells(2 + i, 1).Value2 = Sheets("Provider Data").Cells(i + 3, 3).Value2
End If
Next i
For some reason the data wont write to the cell. I've debugged with message boxes, and the If statement correctly selects the cells that match 8, but for some reason the contents of
sheets("Provider Data").cells(i+3,3).value2
aren't written to the cell. What am I missing? I should mention the data to be written to the cell from (i+3,3) are strings
I need to merge cells using a formula so that the cells only merge when cells on another tab are filled.
I have 2 tabs with the same amount of columns in each. I want cells a1-d1 to merge in tab 1 when cells a1-d1 in tab 2 are filled and for the value of d1 in tab 2 to be inputted into the newly merged cells in tab 1.
this is what I have:
Excel VBA Methods and Function (Excel Macros) overview
Since you want to change cells i do not believe that you can use a formula (even not a user defined one). Therefore i wrote an excel vba macro for your problem.
FirstRows(): Is the starting point. It loops over 10 rows and calls the other methods
CheckEmptyCellValues(curRow): This method checks for empty cells in tab2 (sheet 2 in excel)
MergeCells(curRow) takes the current row as a number (any integer from 1 to max amount of rows) and merges the cells from column 1 to 4 on Sheet 1 (the first sheet in excel)
Fully working demo tested with 4 columns and 10 rows
Sub FirstRows()
Sheets(1).Select
For curRow = 2 To 11
Merge = CheckEmptyCellValues(curRow)
If Merge = 4 Then
MergeCells (curRow)
cellValue = Sheets(2).Cells(curRow, 4).Value
Sheets(1).Cells(curRow, 1).Value = cellValue
End If
Next
End Sub
Sub MergeCells(curRow)
Sheets(1).Select
Range(Cells(curRow, 1), Cells(curRow, 4)).MergeCells = True
End Sub
Function CheckEmptyCellValues(curRow)
Merge = 0
Sheets(2).Select
For i = 1 To 4
If Len(Cells(curRow, i).Value) > 0 Then
Merge = Merge + 1
End If
Next
CheckEmptyCellValues = Merge
End Function
Below you can see the result. The values from sheet 2 haven been copied to sheet 1 (second image). In Sheet 1 the Cells in a row are merged (in row 2 from Cell A2 up to Cell D2 (A2-D2 is now just one cell) if in the first image (sheet 2) every cell (from column a to column d) in a row had a value.
Bugs in the modified code
There are a few things in the modifiend code that are not possible or could lead to a wrong understanding
Function CheckEmptyCellValues(curColumn)
Merge = 0
Sheets(2).Select
For i = A To d
If Len(Cells(curColumn, 11).Value) > 0 Then
Merge = Merge + 1
End If
Next
CheckEmptyCellValues = Merge
End Function
The line For i = A To d is not possible. If you want to use a loop you have to use numbers: For i = 1 To 4 this would repeat the code between For and Next4 times starting with 1
This line Cells(curColumn, 11).Value is technical correct but misleading. Excel uses the first value after (for the row-index and the second value for the column-index. Both values have to be a number: Cells(4,2).Value returns the Cell value from the 4th. row and the second Column (in the Excel Gui the Cell B4)
Try changing this line For i = A To d to this For i = 1 To 4 and see if that returns the wished result.
Bugs part 2
In your other modification you have some of the same bugs:
The loop For curColumn = A to d needs numbers instead of letters (unless A and d were a variable filled with a number but according to your code sample this is not the case
The line cellValue = Sheets(2).Cells(curColumn, d).Value has the same bug, if d is just the letter d and not something like d = 4 than you can not use it in a loop.
This is the code from your comment:
Sub FirstRows()
Sheets(1).Select
For curColumn = A To d
Merge = CheckEmptyCellValues(curColumn)
If Merge = d Then
MergeCells(curColumn)
cellValue = Sheets(2).Cells(curColumn, d).Value
Sheets(1).Cells(curColumn, d).Value = cellValue
End If
Next
End
Sub Sub MergeCells(curColumn)
Sheets(1).Select
Range(Cells(curColumn, 1), Cells(curColumn, d)).MergeCells = True
End Sub
Be carefull it is not running.