I would like to group data in my Excel in a automatic way (by using a macro)
For example, in my column A, I have fields tuck as this:
in A1 I have " 1.0.1",
in A2 " 1.0.3",
in A3 " 1.0.5",
in A4 " 1.1.1",
in A5 " 1.1.2".
I would like to group these data so as to have two groups: 1.0 (packaging A1, A2 and A3) and 1.1 (packaging A4 and A5).
The problem is that the data which can vary (addition of one given 1.0.7 for example which it will be necessary to group(include) in 1.0)
So, it would be necessary to read all the fields of the column A and to make a regroupage by comparing the first 3 characters of every fields, and so to group what is identical.
It is possible to make that in Visual Basic ?
sub regrouper()
Worksheets.Add.Name="group1"
Worksheets.Add.Name="group2"
Dim g1Row As Integer
Dim g2Row As Integer
Dim splitter(0 to 4) As String
g1Row = g2Row = 2
For Each cell in Worksheets(1).Range("A1:A" & Range("A65536").End(xlup).Row)
splitter = Split(cell.text, ".")
If splitter(1) = 0 then
Worksheets("group1").Range("A" & g1Row).Value = cell.Value
g1Row = g1Row + 1
ElseIf splitter(1) = 1 then
Worksheets("group2").Range("A" & g2Row).Value = cell.Value
g2Row = g2Row + 1
Else
MsgBox("data not in group 1 or 2")
End If
Next cell
End Sub
not tested but should get you a little closer
Related
I am trying to make a macro in VBA-Excel that allows me to generate a simple sum (using a + sign) in a specific cell; keeping all the numbers in the formula of the cell. I mean i need that the output formula "hold" the values/adding formulas that are in a certain range. The numbers should be added automatically to the + formula. An special restriction is that in the event that there is no number in a cell of the range, the formula should not incorporate "0".
For example The data shown below are to numbers or sums within a Range (A1:A7):
A1: 5
A2: 7,5 --> =5+2,5
A3:
A4: 1
A5: 1 --> =3-2
A6:
A7: 5,5
Where A2: =5+2,5 and A5: =3-2. The other terms are just numbers.
Range of the example = A1:A7
Formula Output of the macro on B1 (or Cells(1, 2)) should be:
=5 + 5 + 2,5 + 1 + 3 - 2 + 5,5 (20)
The pseudo code should be like:
for i = 1 to 7 -> Cells(1, 2).FormulaLocal = if Cell(i,1) is a number (or an existing adding formula with values), add to the current formula. if its blank or 0, skip to next i (or row).
Output: formula "= Value1(5) + formula2(5+2,5) + Skip0Value3() +
Value4(1) and so on..
I already tried the following code with partially positive results:
Sub summanual()
Dim i As Integer
Dim y As String
For i = 1 To 7
If Cells(i, 1) <> 0 Then
y = Cells(i, 1)
Cells(1, 2).FormulaLocal = Cells(1, 2).FormulaLocal & "+" & y
End If
Next i
If Cells(1, 2) <> 0 Then
Cells(1, 2).FormulaLocal = "=" & Cells(1, 2).FormulaLocal
Else
Cells(1, 2) = ""
End If
End Sub
This works only for positives values. Negatives values are added
like "+-" to the output formula.
The output formula only stores the results of the origin cell. Ex:
A2 is 5+2,5 but it is added like 7,5. It should be split. Same with
A5.
Is fine to declare "y" variable as String in this case?
Any other optimization? Maybe there is a completely different
approach with better and faster result? Just new with vba-excel
Any help would be appreciated
Thanks
I have a very simple question that I can't find the answer to.
Here is an example of what I'd like to do:
Sample Data
If a row in column A = 'Sandwiches', I would like column B to display 'It's a Sandwich'
If a row in column A = 'Wraps', I would like column B to display 'It's a Wrap'
etc. etc.
So far, I am able to do this for the first row. I'm having trouble creating a for loop to loop through all the available cells.
Here is what I have so far (was thinking of adding Else If statements for different values later):
Current If Statement
If you prefer the use of VBA code instead a formule as Ozgun Senyuva suggested. You can try this code:
Sub Food_and_thing()
Dim myLine As Double
Dim myFood As String
Set SavedData = Sheets("SavedData")
myLine = 2
With SavedData
Do While .Cells(myLine, 1) <> "" 'Assume that Category is in column A, and Generated Asset Name is in column B
myFood = " an other thing"
If .Cells(myLine, 1) = "Sandwiches" Then
myFood = " a Sandwich"
ElseIf .Cells(myLine, 1) = "Wraps" Then
myFood = " a Wrap"
ElseIf .Cells(myLine, 1) = "Salads" Then
myFood = " a salad"
End If
.Cells(myLine, 2) = "It's" & myFood
myLine = myLine + 1
Loop
End With
End Sub
Akash you do not need to write vba codes for this. You may use a lookup table and apply Vlookup formula as #BigBen said.
Apart from this, this formula can make you happy if you ignore some spelling errors. Enter this in cell B2 and paste along the way down.
="It's " & A2
Could someone give me some pointers on how to write this formula right?
Sub WriteFormulaTextAndNumbersDependentOnI()
Dim xNumber As Integer
xNumber = InputBox ("Choose I")
For I = 1 To xNumber
Worksheets("Sheet1").Cells(1, 1 + I).Formula = "= & 2,10 + 0,01 * (I - 1)'" & Sheets(I + 1).Name & "'!B12"
Next I
End Sub
The point here is to write a name in different cells, with a calculated number (2.10 + 0.01*(I - 1)) and text from another sheet:
Lets say we have in Sheet2:
B12 = Hello World
Lets say we have in Sheet3:
B12 = You are cool!
We should then get in Sheet1:
I = 1 would give a value/text in cell B2, with the value/text: "2.10+0.01*(1-1) B12" = "2.10 Hello World"
I = 2 would give a value/text in cell C2, with the value/text: "2.10+0.01*(2-1) B12" = "2.11 You are cool!"
An so on.
Any suggestions?
Appreciate any suggestions
//GingerBoy
use:
Concatenate() worksheet function to mix numbers and text in the same string
Text() worksheet function to format a number to a string
as follows:
Worksheets("Sheet1").Cells(2, 1 + I).Formula = "=concatenate(TEXT(2.10 + 0.01*(" & I & "-1),""0.00""), "" "", " & Sheets(I + 1).Name & "!B12)"
see that:
I used Cells(2, 1 + I) to write in in B2, C2 ... as per your narrative
I used dots (.) as decimal separator: you may want to change all dots into commas as per your decimal separator conventions
I want to change the following
123456789A1
to
123-456-789 A1
Background:
In Format Cells, I used this:
000-0000-00 00
And that works if everything in the cell is a number,
12345678911
will become
123-4567-89 11
But as soon there is a letter, it breaks it.
How can I change the type to ignore letters?
this is an example :
Sub test()
a = [A1]
b = Left(a, 3) & "-" & Mid(a, 4, 3) & "-" & Mid(a, 7, 3) & " " & Right(a, 2)
[A2] = b
End Sub
But want you 3 or 4 numbers in the seconde position?
if A1=123456789A1 then in A2 : 123-456-789 A1
Cell number formatting only works on numbers.
If you don't mind changing the values into text strings, you could format them in VBA using the Format function. For example:
Option Explicit
Sub FMT()
Dim R As Range, C As Range
Const sFMT As String = "###-####-## ##"
Set R = Intersect(Range("A:A"), ActiveSheet.UsedRange)
For Each C In R
C.Value = Format(C.Text, sFMT)
Next C
End Sub
You will need to change the range arguments to match what you need. And you could also use this in an event-triggered macro to do it automatically.
I have received a worksheet in Excel which contains kids names and video time tags all in one column, and I need to sort this into a logical format so I can use it. However, the list has no separators.. So I am hoping someone could help me out with a VBA Excel macro.
Below is an example (shortened) string, lets say this is in Cell A1.
" Sandy 25:1132:27Giorgio
09:1114:7Anne Marie 32:10David 17:48Marty
04:3506:1010:3613:1014:32Sandy (2) 04:30Brian 13:4714:37"
I would ideally like for the string to be split up into cells as follows
Cell A2 Sandy
Cell A3 25:11
Cell A4 32:27
Cell A5 Giorgio
Cell A6 09:11
Cell A7 14:7
Cell A8 Anne Marie
Cell A9 32:10
Cell A10 David
Cell A11 17:48
Cell A12 Marty
Cell A13 04:35
Cell A14 06:10
Cell A15 10:36
Cell A16 13:10
Cell A17 14:32
Cell A18 Sandy (2)
Cell A19 04:30
Cell A20 Brian
Cell A21 13:47
Cell A22 14:37
I have tried using some basic "find" and "len" formulas but no luck..
Doesn't do exactly what you want - but it may help get you going in a direction... Hopefully it'll turn out to be the right one...
I pasted your string into cell A1 in my worksheet and then wrote this code in a module in the sheet:-
Function parseText(ByVal text As String, ByVal domain As Integer) As String
Dim returnValue As String
Dim colon As Integer
Dim soFar As Integer
soFar = 0
text = Trim(text)
While soFar < domain
colon = InStr(text, ":")
While (Mid(text, colon + 5, 1) = ":")
colon = colon + 5
Wend
returnValue = Mid(text, 1, colon + 2)
While Not (IsNumeric(Right(returnValue, 1)))
returnValue = Left(returnValue, Len(returnValue) - 1)
Wend
text = Replace(text, returnValue, "")
soFar = soFar + 1
Wend
parseText = returnValue
End Function
Function parseDomain(ByVal domain As String) As String
Dim returnValue As String
Dim part As String
While Len(domain) > 0
part = ""
If InStr(domain, ":") > 0 Then
part = Mid(domain, InStrRev(domain, ":") - 2, 5)
returnValue = part & "~" & returnValue
domain = Left(domain, Len(domain) - Len(part))
End If
If part = "" Then
returnValue = Trim(domain) & "~" & Left(returnValue, Len(returnValue) - 1)
domain = ""
End If
Wend
parseDomain = returnValue
End Function
Function pullPiece(ByVal block As String, ByVal piece As Integer) As String
Dim returnValue As String
Dim pieces() As String
pieces = Split(block, "~")
If piece > UBound(pieces) + 1 Then
returnValue = ""
Else
returnValue = pieces(piece - 1)
End If
pullPiece = returnValue
End Function
This bit is complicated to explain...
In the image below the formula in A14 is the content of cell A4. The formula in A15 is the content of cell A5, etc. all the way down to A10. These formulae break out the block of text for each name.
The formula in B14 is the content of B4. This cell can then be copied down the range to B10 so that the references change to A4 thru A10. These formulae reformat the text with tildes so that the text is easier to split up (later).
The formula in C14 is the contents of C4. This cell can be copied down to C10. This pulls the name from the block it relates to. The second parameter is the "piece" number - 1=name, 2=time1, 3=time2, etc.
The formula in D14 is the contents of cells D4 and pulls the first time out of the block it relates to. I haven't put the definition for the other formulae - but hopefully you can see the pattern on how they are used.
Drop me a message if you want any clarification.
The string is difficult to parse due to the timestamps not appearing to follow a similar format. For example most follow the format 00:00, however the value you wish to place in cell A7 only has a single digit following the colon. I have therefore created some code to get you started in the right direction, but this works on the assumption of the format 00.00 and currently doesn't parse text following numbers. But if you perform a bit more research I'm sure you can complete from this point:
Public Sub TestCode()
Dim strTest As String, strModify() As String, strNew() As String, x As Long
strTest = "Sandy 25:1132:27Giorgio 09:1114:7Anne Marie 32:10David 17:48Marty 04:3506:1010:3613:1014:32Sandy (2) 04:30Brian 13:4714:37"
strModify = Split(strTest)
ReDim strNew(0 To 0)
strNew(0) = strModify(0)
For x = 1 To UBound(strModify)
If Left(strModify(x), 1) Like "[A-Z]" Then
ReDim Preserve strNew(0 To (UBound(strNew) + 1))
strNew(UBound(strNew)) = strModify(x)
ElseIf Left(strModify(x), 1) Like "[0-9]" Then
Do Until InStr(1, strModify(x), ":") = 0
ReDim Preserve strNew(0 To (UBound(strNew) + 1))
strNew(UBound(strNew)) = Left(strModify(x), InStr(1, strModify(x), ":") + 2)
strModify(x) = Right(strModify(x), Len(strModify(x)) - (InStr(1, strModify(x), ":") + 2))
Loop
Else
strNew(UBound(strNew)) = strNew(UBound(strNew)) & " " & strModify(x)
End If
Next x
For x = 0 To UBound(strNew)
Range("A1").Offset(0, x).Value = strNew(x)
Next x
End Sub
To help you understand the code in order to modify, this is basically splitting the original string wherever there is a space (result placed in array called strModify). It then checks the first character in the string to see if it is a letter, number or other character. Based on this information it will place the individual components of the string in to a new array variable called strNew. It then simply reads this array and places each item in to the next available cell.
I hope this helps you begin. Once you have a solution please post your final code on here in order to help others who may have a similar problem.