Search for value in column B, replace column D & E of that row - vba

New to VBA so go easy on me.
I have a spreadsheet in the below format:
I require VBA that will search column B for the term "LBR-0001", then replace the contents of column D in that row with the value "25", and column E in that row with "AUD". The row containing "LBR-0001" will change so this cannot be hard coded to a particular row.
Currently I am using the code below but it is far too slow to complete since it searches up to 10,000 rows.
For i = 1 To 10000
With Sheets("LPP_Previous_Month")
If .Range("B" & i).Value = "LBR-0001" Then _
.Range("D" & i).Value = "25"
.Range("E" & i).Value = "AUD"
End With
Next i
Any improvements would be greatly appreciated.

Use the worksheet's Match function to quickly locate a matching row in column B. Use that row number to set the values in columns D and E.
dim m as variant
With Sheets("LPP_Previous_Month")
m = application.match("LBR-0001", .range("B:B"), 0)
if not iserror(m) then
.cells(m, "D") = 25
.cells(m, "E") = "AUD"
end if
End With
Use Application.Match (not worksheetfunction.match) and return the result to a variant. This is the only way to reliably test using IsError.

Related

copy cell content based if adjacent cell meets criteria

I have a series of matrices consisting of 7 columns with a varied number of rows. I want the company names that are in column 2 of the matrix if the corresponding data in column 4 is "CM" aggregated into one cell per matrix (lets say B3:B98 for all the different matrices) with a space in between the different names. Please see the below picture for an example of what the matrices look like
The end result is that all the company names in Column E will be aggregated in B3 if the cell on the same row in column G is "CM", the next matrix beginning in Column M in B4 and so on.
I am having zero success in getting my if statement to recognize "CM" in the cell content, or aggregating the results using the Join statement. Any help is much appreciated.
Edits:
The objective is to have all the underwriters on a particular security aggregated in one cell, so that the cell can be easily searched in another part of the sheet for the presence of a particular underwriter.
The code below, as you can likely tell, does not work. I hit a wall as I could not get it to distinguish between cells that contained "CM" and those that did not. (I know that the code below would not aggregate the result in any cell, only copying the result into column B, as I said, it is a work in progress that has stalled.)
Dim Ws5 As Worksheet: Set Ws5 = Worksheets(5)
'turn off some Excel functionality so code runs faster
Application.ScreenUpdating = False
Application.DisplayStatusBar = False
Application.EnableEvents = False
ActiveSheet.DisplayPageBreaks = False
'Compiles the managers in the matrices into a column on the MgrMatrix sheet to be used
'for the entry sheet column of underwriters.
Dim CoL As Range: Set CoL = Ws5.Range("D3:K104")
Dim CeL As Range
For Each CeL In CoL.Columns(4)
If CeL.Text = "CM" Then
CeL.Offset(0, -5) = "CM"
Else
CeL.Offset(0, -5) = CeL.Offset(0, -2).Value
End If
Next
Edit: Using urdearboy's code, i modified it to work for multiple matrices on the same sheet in the below way. This version doesn't have the same finesse as his did, as this version relies on all matrices containing the same number of columns and not exceeding 100 rows.
For i = 7 To 857 Step 9
For y = 3 To 100
If Cells(y, i) = "CM" Then
s = s & Cells(y, i).Offset(0, -1).Value & " "
End If
Next y
If s = "" Then
s = "Sole Lead"
End If
Cells(Rows.Count, 2).End(xlUp).Offset(1, 0) = Trim(s)
s = ""
Next i
Paste code in VBE within Sheet 5 (Or whatever sheet you want to run this on).
The string, s, will build itself as it loops through your column checking for "CM" matches.
As is, the code will add commas between each new value added like, so, and, so, and then remove the last coma at the end before displaying the final string like, so, and, so
Option Explicit
Sub TextCM()
Dim i As Long, s As String
For i = 3 To Range("G" & Rows.Count).End(xlUp).Row
If Range("G" & i) = "CM" Then
s = s & Range("E" & i).Value & ", " 'Remove & ", " if you do not want the comma + space
End If
Next i
Range("B2") = Left(s, Len(s) - 2) 'Change to Range("B2") = s to not delete last character in string
End Sub
You should be able to figure out how to extend this to multiple tables (matrices?) no problem.

VBA copy row from sheet1 to sheet2 based on keyword

My code does what I want, but it copies it to column A in sheet 2. I would like it to put the data in starting at Column B if possible.
Sub EFP()
Dim keyword As String: keyword = Sheets("Results").Range("B3").Value
Dim countRows1 As Long, countRows2 As Long
countRows1 = 3 'the first row of my dataset in the Data tab
endRows1 = 500 'the last row of my dataset in the Data tab
countRows2 = 6 'the first row where I want to start writing the found rows
For j = countRows1 To endRows1
If Sheets("Data").Range("B" & j).Value = keyword Then
Sheets("Results").Rows(countRows2).Value = Sheets("Data").Rows(j).Value
countRows2 = countRows2 + 1
End If
Next j
End Sub
If you copy and paste whole rows, they will always start in column A. If you want the result to start in column B, you need a different approach, for example
Sheets("Results").Range("B" & countRows2 & ":Z" & countRows2).Value = Sheets("Data").Range("A" & j & ":Y" & j).Value

VBA - Add new Column then COPY cells and then SUM cells

I am attempting to create a new column that consolidates multiple values into a single Column. The code below has no problems creating a new column exactly where I need it.
Within each newly created column however, I would like my code to Sum the previous 5 cells within the same row. It works currently, but is static(The new column is G in the below example, and it sums B:F). I think I need some sort of Offset function built into it to allow the Range it sums to be dynamic (Previous 5 columns), as it loops through the entire sheet.
Additionally, I would love it to copy the Header information from rows 1-9 from the column to the left of the new column, into the new column, and place the word "Combo" into Row 10 of the new column.
Office 2013
Any help with this endeavor would be great.
Thanks
Dim currentColumn As Integer
Dim columnHeading As String
For currentColumn = Sheets("SLTData").UsedRange.Columns.Count To 2 Step -1
columnHeading = Sheets("SLTData").UsedRange.Cells(10, currentColumn).Value
'CHECK WHETHER TO KEEP THE COLUMN
Select Case columnHeading
Case "PD.G0100", "PD.G0500", "PD.G0800", "PD.G0900", "PD.G1000", "PD.G0300", "PD.G0400", "PD.G0150", "PD.G0600"
'Do nothing
Case Else
If InStr(1, _
Sheets("SLTData").UsedRange.Cells(10, currentColumn).Value, _
"Homer", vbBinaryCompare) = 0 Then
Sheets("SLTData").Columns(currentColumn).Insert
For i = 11 To Cells(Rows.Count, "B").End(xlUp).Row
Range("G" & i) = WorksheetFunction.Sum(Range("B" & i), Range("C" & i), Range("D" & i), Range("E" & i), Range("F" & i))
Next i
End If
End Select
Next
Instead of Range("G" & i) use Cells(row, column) (both row and column are integer). To sum up the 5 cells left of it: Cells(row, column) = WorksheetFunction.Sum(Range(Cells(row, column-5), Cells(row, column-1)))

excel VBA : how to skip blank cells between 2 cells that contain values?

I am working out a button that can auto sum value at column C that column A = column B
like the picture :
PIC:
I can only copy the value in column C (that the word in column A = column B) to column E so far.
the code
Private Sub CommandButton2_Click()
Dim i As Integer, q As Integer
q = 2
For i = 3 To 100
Range("E" & q).Value = Range("b" & 3).Value
If Range("B" & i).Value = "A-RDL1" And Range("c" & i).Value = "OPEN" Then
Range("E" & i).Value = Range("d" & i).Value
End If
Next i
End Sub
the question 1) is how can I skip the blanks E9 to E17, so the numbers can be continuous? (AFTER CLICK THE BOTTON)
question 2) is it possible to auto sum the Numbers in column E instead of show each?
Thanks a lot and sorry for my poor English...
1) Yes, you can skip those, just carry out a check in the cell value and compare to empty string: Range("").Value2 = "". I personally prefer to do it like this though, to avoid false positives: Len(Trim(Range("").Value2)) = 0.
2) Yes, you can do that. just declare an Integer variable or two and use that to carry out a running count of your values.

Search String contain specific text in column and return result from the same row of another column

is there a way in excel formula instead of vba script to search for specific text in a column and return result from the same row of another column. If this can't be done in formula, how this done in VBA?
Example:
(Column A) Hostname
UK-Aaron-01
UK-Bob-01
UK-AaronOoi
UK-XP-Peter
UK-XP-Jack
UK-Brandon
(Column B) Hostname Department
==This will be the place where the result would be
(Column C) User
Aaron
Acciai
Achilles
Acton
AaronOoi
Adarsh
Aba
Abbas
Bob
Abeeku
Abhay
Abner
Absolom
Peter
Acoose
Adagio
Brandon
(Column D) Department
Marketing
IT
Sales
HR
Marketing
IT
HR
arketing
Sales
HR
Marketing
IT
HR
Marketing
IT
Sales
The result in (Column B) should be:
Marketing
Marketing
IT,HR (If UK-AaronOoi match 2 names "Aaron" & "AaronOOi" then it will be separated with coma)
HR
NotFound (If no match then return not found)
Sales
The hostname,User & department contain data more than 4k
Aaron, in VBA you can do this, but i have minimized the hypothesis in order to make it more simple:
suppose you only have in raw A:
UK-Aaron-01
UK-Bob-01
UK-AaronOoi
the VBA code will be like this:
Sub find_department_by_name()
Dim i As Integer
For i = 1 To 3:
If Cells(i, 1).value Like "*-Aaron-*" Then
Cells(i, 2).value = "Marketing"
Else:
Cells(i, 2).value = "Not Found"
End If
Next
End Sub
when you run it you will have in column B:
Marketing
Not Found
Not Found
i hope this is what you are looking for (of course you need to customize it to your needs)
I finally able to work on the solution with simple guideline from toufikovich and additional Google for the rest of the answer. This is my first time doing VBA script and it was hard to start with so excuse if it looks newbie :)
Private Sub Run()
Dim lastRowB As Integer
Dim lastRowC As Integer
Dim i As Integer
Dim k As Integer
With ActiveSheet
lastRowB = .Cells(.Rows.Count, "A").End(xlUp).Row 'Get Row last number in A with data
End With
With ActiveSheet
lastRowC = .Cells(.Rows.Count, "C").End(xlUp).Row 'Get Row last number in C with data
End With
For k = 2 To lastRowB: 'Run a loop for each hostname in Column B
For i = 2 To lastRowC: 'Run a lopp for each user in column C
If Cells(k, 1).Value Like "*" & Cells(i, 3).Value & "*" Then 'Check if Cell in A is like Cell in C with wildchard
Cells(k, 2).Value = Cells(k, 2).Value & "," & Cells(i, 4).Value 'Check if there is more than 1 similar User match saperate them by coma ","
If Left(Cells(k, 2).Value, 1) = "," Then ' If the string start with coma ","
Cells(k, 2).Value = Right(Cells(k, 2).Value, Len(Cells(k, 2).Value) - 1) ' Then remove the first text
End If
End If
Next
Next
End Sub
The most simple way would be:
vlookup = vlookup("*desiredtextstring*",a1:a11,1,0)
Alternatively, to return desired text spread across you can replace vlookup with hlookup