Increment a number within a string in one cell #VBA - string

I am trying to match to sheets with same records and update one sheet based on another. The updated goes with and incremental of '1' in respective cell.
It was able to write it so the cells with values will be updated respectively.
The problem is that I can't figure it out how to increment a cell that contains a string. (some cells contain ">1", ">2" and so on) I am trying to increment those if needed to change to ">2" and ">3" and so on.
The moment I should paste the code is bolded.
Sub Increment()
For Each SnowCell In MySnowRange
For Each TrakerCell In MyTrakerRange
If TrakerCell.Value = SnowCell.Value Then
If TrakerCell.Offset(, 1).Value <> SnowCell.Offset(, 1).Value Then
TrakerCell.Offset(, 1).Value = SnowCell.Offset(, 1).Value
Select Case SnowCell.Offset(, 1).Value
Case "In Queue"
If Application.WorksheetFunction.IsNumber(TrakerCell.Offset(, 3).Value + 1) = True Then
TrakerCell.Offset(, 3).Value = TrakerCell.Offset(, 3).Value + 1
Else
**TrakerCell.Offset(, 3).Value = Split(TrakerCell.Offset(, 3).Value)**
Case "Assigned"
TrakerCell.Offset(, 4).Value = TrakerCell.Offset(, 4).Value + 1
Case "Work In Progress"
TrakerCell.Offset(, 5).Value = TrakerCell.Offset(, 5).Value + 1
Case "Pending"
TrakerCell.Offset(, 6).Value = TrakerCell.Offset(, 6).Value + 1
Case "Resolved"
TrakerCell.Offset(, 7).Value = "Resolved"
End Select
ElseIf TrakerCell.Offset(, 1).Value = SnowCell.Offset(, 1).Value Then
Select Case SnowCell.Offset(, 1).Value
Case "In Queue"
TrakerCell.Offset(, 3).Value = TrakerCell.Offset(, 3).Value + 1
Case "Assigned"
TrakerCell.Offset(, 4).Value = TrakerCell.Offset(, 4).Value + 1
Case "Work In Progress"
TrakerCell.Offset(, 5).Value = TrakerCell.Offset(, 5).Value + 1
Case "Pending"
TrakerCell.Offset(, 6).Value = TrakerCell.Offset(, 6).Value + 1
Case "Resolved"
TrakerCell.Offset(, 7).Value = "Resolved"
End Select
End If
Else
End If
Next
Next
End Sub

From what I can see, your comparison is working, it's your update that you need to resolve. Write a function that will return you an updated value. What you will need the function to do is first extract the numeric value from the string (have a look at these 2 post on how to do that: Post1 and Post2). Now that you have your numeric value, increment it as per your requirements. Next, replace the numeric value with your new updated value (this should be too hard to do: you have your old value and the new value, just use Replace to change it in the string). You can now use this function to assign the value to your Offset cells

Thanks!
Have done it this way and it works:
Case "In Queue"
If Application.WorksheetFunction.IsNonText(TrakerCell.Offset(, 3).Value) = True Then
TrakerCell.Offset(, 3).Value = TrakerCell.Offset(, 3).Value + 1
Else
a = Left(TrakerCell.Offset(, 3).Value, 1)
b = Right(TrakerCell.Offset(, 3).Value, Len(TrakerCell.Offset(, 3).Value) - 1)
c = b + 1
d = a & c
TrakerCell.Offset(, 3).Value = d
End If
:)!

If you know that there is always only one character before the number, then just:
Set c = TrakerCell(, 4) ' same as TrakerCell.Offset(, 3).Resize(1, 1)
c.Value = Left(c, 1) & (Mid(c, 2) + 1)
but if the number of non-numeric characters varies, then it can be changed a bit to:
Set c = TrakerCell(, 4)
L = Len(c) - Len(Str(Val(StrReverse(c)))) + 1 ' number of characters before the number + 1
c.Value = Left(c, L) & (Mid(c, L + 1) + 1)

Related

VBA code not entering else if and end if loop

I have the below loop written, however I am having issues with the code entering the else if loop and to have the loop until (end if) the ranges equal each other. Essentially I want the loop to add 2 to the cells that contain "Corporates" first and afterwards add 1 to the cells that have a the number 2.
Dim x As Long
Dim V As Variant
V = Cells(Rows.Count, 1).End(xlUp).Row
JV = Range("J" & Rows.Count).End(xlUp).Row
For x = 1 To V
If Cells(x, 2).Value = "Corporates" Then
Cells(x, 7).Value = Cells(x, 7).Value + 1
ElseIf Cells(x, 12).Value < "3%" And Cells(x, 2).Value = "Corporates" Then
Cells(x, 7).Value = Cells(x, 7).Value + 1
End If
Range("J" & JV + 3).Value = Range("J" & JV + 2).Value
Next x
End Sub
Use a nested If instead of ElseIf
I'm guessing that < "3%" should be < 0.03 ("3%" is text, not a number).
If Cells(x, 2).Value = "Corporates" Then
If Cells(x, 12).Value < 0.03 Then
Cells(x, 7).Value = Cells(x, 7).Value + 2
Else
Cells(x, 7).Value = Cells(x, 7).Value + 1
End If
End If

Visual Basic - for..next and formulas

I need to make Excel VBA script which inserts formula depending on cells values.
My current version returns only FALSE value
For i = 1 To 10 Step 1
If Cells(i, 2).Value > Cells(i, 1).Value Then Cells(i, 3).Value = Cells(i, 3).Formula = "=cells(i,1).value+cells(i,2).value"
If Cells(i, 2).Value < Cells(i, 1).Value Then Cells(i, 3).Value = Cells(i, 3).Formula = "=cells(i,2).value-cells(i,1).value"
Next i
I geuss you would like to do something like that
For i = 1 To 10 Step 1
If Cells(i, 2).Value > Cells(i, 1).Value Then Cells(i, 3).FormulaR1C1 = "=RC[-2]+RC[-1]"
If Cells(i, 2).Value < Cells(i, 1).Value Then Cells(i, 3).FormulaR1C1 = "=RC[-2]-RC[-1]"
Next i
First, it returns FALSE because you compare the formula of the cell with your string. Those are not equal, hence it returns FALSE. This would be solved by leaving out the =Cells(i,3).Formula part.
Second, it is important to be aware that certain VBA functionality cannot be used in your worksheet and vice versa. When you type '=Cells' in a cell you will note there is no Worksheet function called Cells. That's why the following will return '#Field!'
For i = 1 To 10 Step 1
If Cells(i, 2).Value > Cells(i, 1).Value Then
Cells(i, 3).Formula = "=cells(i,1).value+cells(i,2).value"
End if
If Cells(i, 2).Value < Cells(i, 1).Value Then
Cells(i, 3).Formula = "=cells(i,2).value-cells(i,1).value"
End if
Next i
The easiest thing to do is performing the calculation within VBA:
For i = 1 To 10 Step 1
If Cells(i, 2).Value > Cells(i, 1).Value Then
Cells(i, 3).Value = Cells(i, 1).Value + Cells(i, 2).Value
End if
If Cells(i, 2).Value < Cells(i, 1).Value Then
Cells(i, 3).Formula = Cells(i, 2).Value - Cells(i, 1).Value
End if
Next i
Some recommendations:
Instead of addressing a cell value multiple times, assign the value to a variable.
Try avoiding writing formulas to worksheets. This will slow down your code big time. If you must, then consider storing all the formulas in an array first and only write them to the worksheet when the entire array has been filled.
In this case you can avoid using VBA altogether.
Consider checking if the value of the cell is numeric before performing calculations.
If you insist on using a worksheet function, you could use Storax's suggestion.

Excel: How do I 'gather' values to display in another cell

I have two columns, Column A has a set of a few standard values and column B has all unique values. I'm only just experimenting with more complex ways of compiling data than the beginner level so I'm a bit at a loss.
I need to either have a lookup or create a macro that will list only the values in A (once each) but also display which values in B correspond to those in A
for example
A | B
va1|abc
va1|bcd
Va2|xyz
va3|zab
will show (in a single cell) the following
va1: abc, bcd
va2: xyz
va3: zab
Please help!
Option Explicit
Sub Test()
Dim i As Long, j As Long, k As Long
k = 1
For i = 1 To Cells(Rows.Count, 1).End(xlUp).Row
If Application.CountIf(Range("C:C"), Cells(i, 1).Value) = 0 Then
Cells(k, 3).Value = Cells(i, 1).Value
For j = 1 To Cells(Rows.Count, 1).End(xlUp).Row
If Cells(j, 1).Value = Cells(k, 3).Value And _
InStr(Cells(k, 4).Value, Cells(j, 2).Value) = 0 Then
If Cells(k, 4).Value = "" Then
Cells(k, 4).Value = Cells(j, 2).Value
Else
Cells(k, 4).Value = Cells(k, 4).Value & ", " & Cells(j, 2).Value
End If
End If
Next j
k = k + 1
End If
Next i
For i = 1 To Cells(Rows.Count, 3).End(xlUp).Row
Cells(i, 3).Value = Cells(i, 3).Value & ": " & Cells(i, 4).Value
Cells(i, 4).ClearContents
Next i
End Sub
Edited for single cell
In case your requirement is to "have the grouped data", and not exactly "have one single string per A", you can do this with a "pivot table" putting A and B in the row labels, like in the following picture:

Sum of all the iterations of a variable in VBA

I have the following code
Dim i As Integer
For i = 3 To 10
If Range("H3").Value = Cells(i, 2).Value And Range("I3").Value < Cells(i, 4).Value And _
Range("I3").Value >= Cells(i, 3).Value Then
Range("J3").Value = Cells(i, 5).Value
End If
Next i
I want the value of J3 to represent the sum of all the iterations and not just the last iteration if i. Can it be done?
While there are certainly better methods of adding up cells, for your particular method this should work.
Dim i As long, lTotal as long
For i = 3 To 10
If Range("H3").Value = Cells(i, 2).Value And Range("I3").Value < Cells(i, 4).Value And _
Range("I3").Value >= Cells(i, 3).Value Then
lTotal = Cells(i, 5).Value + lTotal
End If
Next i
Range("J3").Value = lTotal
Keep a running total of of your loop, then use the running total as your cell's value after you've finished the loop
Change this line
Range("J3").Value = Cells(i, 5).Value
To:
Range("J3").Value = Range("J3").Value + Cells(i, 5).Value

VBA Dynamic Range with Values Calculation

Hello I have the following problem:
As you can see in column A we have Dates, in column B there is always a "1" when a year changes from one to the next, I marked it in yellow. In column H are different values and in column I, I want to have only the FIRST value, which is greater (in this case) than 10% within one year (so in the period from one to one in column B). After that I want to have the next first value, which is >10% in the next year so next period from 1 to 1 in column B and so on.
Can anyone help me?
So far I programmed this, but it shows me all values >10% but not the first from each range to each range.
Sub ABC ()
With ThisWorkbook.Worksheets("Test")
rowCount = 2
Do While .Cells(rowCount + 1, 8).Value <> ""
If .Cells(rowCount, 2).Value = 0 And .Cells(rowCount, 8).Value >= 0.1 Then
.Cells(rowCount, 9).Value = .Cells(rowCount, 7).Value * 0.1 / 1.1
Else
.Cells(rowCount, 9).Value = ""
End If
rowCount = rowCount + 1
Loop
End With
End Sub
It seem the code is error on the cell address
Sub ABC ()
With ThisWorkbook.Worksheets("Test")
rowCount = 2
Do While .Cells(rowCount + 1, 8).Value <> ""
If .Cells(rowCount, 2).Value = 0 And .Cells(rowCount, 8).Value >= 0.1 Then
.Cells(rowCount, 9).Value = .Cells(rowCount, 8).Value * 0.1 / 1.1
Else
.Cells(rowCount, 9).Value = ""
End If
rowCount = rowCount + 1
Loop
End With
End Sub

Resources