Dear Community I need your help and I dont know how to solve this problem.
I need to compare the "Team(A)" "Number(D)" (here it is 1)
with the number "12(E)"
in an extra row (F)
When I have done this, the next "Team(A) "Number(D)" (here it is 2)
with the number "10(E)"
in an extra row (F) under the other and so on.
It should look like this:
Row(F)
1 - 12
2 - 10
3 - 12
4 - 16
Is there any solution ? I cant adress the column because it could be variable.
can you help me please. By the way, I am new at VBA.
Thank you so so much :)
You could make it simple and work with "Selection".
To use my example-script you would have to mark the column with the first value and then start the makro.
(The second value would always have to be one column to the right of it and two rows below.)
Sub TeamCompare()
Dim c As Range
Dim a As String
Dim b As String
For Each c In Selection
If Not c.Value = "" Then
a = c.Value
b = ActiveSheet.Cells(c.Row + 2, c.Column + 1).Value
ActiveSheet.Cells(c.Row, c.Column + 2).Value = a & " - " & b
End If
Next c
End Sub
Related
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
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
I have a spreadsheet with a load of random text and numbers in column A like so:
Column A
Row 1 = 471806121601 5205569 - 0007 Standard White Toilet Tissue 27
Row 2 = 471814121601 5206177 - 0014 Premium White Toilet Tissue 6
Row 3 = 471814121601 5206178 - 0007 Premium White Toilet Tissue 27
Row 4 = 471806121601 5206180 - 0014 Premium Kitchen Towel 2x75l 6
I have about 2000 lines in total. In each cell, is a Purchase order number (12 digits) and an item number next to it (7 digits).
I am trying to extract the po number and put it into column B and extract the item number and put it into column C
Column B Column C
471806121601 5205569
471814121601 5206177
471814121601 5206178
471806121601 5206180
Here is my code:
Option Explicit
Sub main()
Dim cell As Range
Dim arr As Variant, arrElem As Variant
With Worksheets("Orders") '<--| change "Strings" to your actual worksheet name
For Each cell In .Range("A1", .Cells(.Rows.Count, "A").End(xlUp))
arr = Split(Replace(cell.Value, " ", " "), " ") '<--| change "A"'s to your actual relevant column index
For Each arrElem In arr
If IsNumeric(arrElem) Then
If Len(arrElem) = 12 Then cell.Offset(0, 1).Value = arrElem
End If
Next arrElem
Next cell
End With
Dim cell2 As Range
Dim arr2 As Variant, arrElem2 As Variant
With Worksheets("Orders") '<--| change "Strings" to your actual worksheet name
For Each cell2 In .Range("A1", .Cells(.Rows.Count, "A").End(xlUp))
arr2 = Split(Replace(cell2.Value, " ", " "), " ") '<--| change "A"'s to your actual relevant column index
For Each arrElem2 In arr2
If IsNumeric(arrElem2) Then
If Len(arrElem2) = 7 Then cell2.Offset(0, 3).Value = arrElem2
End If
Next arrElem2
Next cell2
End With
End Sub
This code does work. However it takes absolutely ages and only does one line at a time...Slowly.
Is there a quicker way of doing this? Thanks
If your PO and IN are always the same length in col B put
=MID(A2, 1, 12)
And in col C
=MID(A2, 14, 7)
However if your number change but are always the first two swap the above for,
=MID(A2,1,FIND(" ",A2,1)-1)
And
=MID(A2, FIND(" ", A2, 1)+1, 7)
Respectively.
just use split(string,delimiter)(0) and (1) why replace the space, just use that as the delim. If Row # is in, then use (1) and (2), or you could consider split(split(input,"-")," ") maybe a little faster, not sure though. Also, once you're done no need to complete the loop, so consider, do until with flags rather than for next, although exit for is available
Formula wise, it could be done using something like this
=MID(D1,FIND("é",SUBSTITUTE(D1," ","é",3)),FIND("é",SUBSTITUTE(D1," ","é",4))-FIND("é",SUBSTITUTE(D1," ","é",3)))
and
=MID(D1,FIND("é",SUBSTITUTE(D1," ","é",4)),FIND("é",SUBSTITUTE(D1," ","é",5))-FIND("é",SUBSTITUTE(D1," ","é",4)))
I'm going to start my problem with an example, as otherwise it will be too difficult to explain.
A B C D E (ID)
1 word letter 1
test blabla
other
2 word letter 2
number
3 test true 3
4 other false 4
5 word letter Yes 5
6 word letter Yes 6
7 test letter 7
What's the goal?
If I'll try to explain the goal in words, it will be hard to understand; it's also hard to explain :) Anyway I also added my code, and if you are good with VBA you will understand the code better then the text.
IF a value (in this example: word) is also in other rows THEN we needs to check column C, AND IF there is a value in column C that we find more then once in column C (in this example: letter), we needs to recheck if in column A the value also appears more then once.
So I already made a SUB, AND IT WORKS ! :) BUT not if there are more values in a cell. So in the example when there is only 1 value in a cell, as in row 6 & 7, column D returns : YES
Here is my code so far.
Sub duplicates()
Dim source As Range
Dim source2 As Range
For Each source In Range("A1", Range("A" & Rows.Count).End(xlUp))
If source.Value <> "" Then
For Each source2 In Range("A1", Range("A" & Rows.Count).End(xlUp))
If source.Value = source2.Value And source.Offset(0, 4).Value <> source2.Offset (0, 4).Value Then
If source.Offset(0, 2).Value = source2.Offset(0, 2).Value Then
source.Offset(0, 3) = "Yes"
End If
End If
Next source2
End If
Next source
End Sub
So, we should return: YES in row 1 & 2 as well. Hope you understand my goal.
Hope someone can help.
My proposal is as follows:
A) additional function would check each element of each cell passed to the function as array:
Function AnyEqual(ColA, ColB) As Boolean
Dim itemA, itemB
For Each itemA In ColA
For Each itemB In ColB
If itemA = itemB Then
AnyEqual = True
Exit Function
End If
Next
Next
End Function
B) Some changes are made in your code- put it inside For Each source2 loop instead your inner code:
If AnyEqual(Split(source, Chr(10)), Split(source2, Chr(10))) And _
source.Offset(0, 4).Value <> source2.Offset(0, 4).Value Then
If AnyEqual(Split(source.Offset(0, 2), Chr(10)), _
Split(source2.Offset(0, 2), Chr(10))) Then
source.Offset(0, 3) = "Yes"
End If
End If
Based on data you provided it seems it works fine. I hope it is what you were looking for as it was a bit complicated to understand your needs.
I've got a workbook where I have one worksheet which contains a lot of data.
My goal is to create a macro that inserts a formula in a separate sheet to copy the data from the first sheet. Lets call the first sheet "Numbers1" and the second sheet "TidyNumbers1".
In the sheet "TidyNumbers1" I want to loop through each cell from column A to M and rows 1 to 60. So I've got a macro that so far looks like this:
Sub updateFormulasForNamedRange()
Dim row, col, fieldCount As Integer
colCount = 13
RowCount = 60
For col = 1 To colCount
For row = 1 To RowCount
Dim strColCharacter
If col > 26 Then
strColCharacter = Chr(Int((row - 1) / 26) + 64) & Chr(((row - 1) Mod 26) + 65)
Else
strColCharacter = Chr(row + 64)
End If
Worksheets("TidyNumbers1").Cells(row, col).Formula = "=IF(Numbers1!E" & col & "<>0;Numbers1!" & strColCharacter & row & ";"")"
Next row
Next col
End Sub
But the formula is supposed to looks like this for Column A, row 2:
IF(Numbers1!E2<>0;Numbers1!A2;"")"
And the formula in Column A, row 3 should look like this:
IF(Numbers1!E3<>0;Numbers1!A3;"")"
Formula in Column B, row 2 should look like this:
IF(Numbers1!E2<>0;Numbers1!B2;"")"
In other words, the formula looks to see if the value in Column E, row % is anything but 0 and copies it if conditions are met.
But, I see that I need to translate my integer variable Row with letters, because the formula probably needs "A" instead of 1. Also, I get a 1004 error (Application-defined or object-defined error) if I just try to use:
Worksheets("Numbers1").Cells(row, col).Formula = "=IF(Numbers1!E" & row & "<>0;Numbers1!" & col & row & ";"")"
I clearly see that the integer row should be translated to letters, if that's possible. Or if anyone has any other suggestions that might work. Also, the 1004 error is unclear to me why happens. I can define a string variable and set the exact same value to it, and there's no error. So it's probably the formula bar that whines about it I guess?
Here is a former post of mine containing functions for conversion of column numbers to letters and vice versa:
VBA Finding the next column based on an input value
EDIT: to your 1004 error: Try something like this:
=IF(Numbers1!E" & row & "<>0,Numbers1!A" & row & ","""")"
(use ; instead of ,, and "" for one quotation mark in a basic string, """" for two quotation marks).
Would not it be easier to get the cell address with the Cells.Address function?
For example:
MsgBox Cells(1, 5).Address
Shows "$E$1"
Best Regards