scan col for matching text and paste results in another column - excel

I need to scan Col B for any values that have the same left characters that are found in cell AL2, the length of the string is found in AR1. If the left value in Col B matches the value in AL2 I need to copy the values in that row from Col B to col G. copied into Col At, starting a AT6 and continuing down until there until all values in Col C have been checked. First picture is data that will be scanned, second picture is what I want the macro to spit out
Here is the modified macro that I recorded. I am getting an runtime 13 error on the IF statement. Any ideas on how to clean this up ?
Sub GenerateSummaryPage()
'
' scans B column for combiner box numbers
Application.ScreenUpdating = False
Dim dlen As String
Worksheets("HR-Cal").Activate
dlen = Worksheets("HR-Cal").Range("AR2")
r = ActiveCell.Row
For lrow = Cells(Cells.Rows.Count, "B").End(xlUp).Row To 7 Step -1
'checks curent row for box name in cell AL2
'copies values from col B to col G in that row to into Col AT, starting at AS6
If Left(Cells(lrow, "B"), dlen) = ActiveSheet.Range("AL2").Text Then Range("B" & r).Rows.Select
'If Left(Cells(lrow, "B"), dlen) = Range("AL2").Value Then Range("B" & r & ":G" & r).Select
Selection.Copy
Range("AT100000").End(xlUp).Offset(1, 0).PasteSpecial (xlPasteAll)
Next lrow
Application.ScreenUpdating = True
'insert rows for inverter headers
End Sub

You should try this.
If Left(Cells(lrow, "B"), dlen) = ActiveSheet.Range("AL2").Text Then
Range(Cells(lrow, "B"), Cells(lrow, "G")).Value = Range(Cells(lrow, "AT"), Cells(lrow, "AY")).Value
End if
If this doesn't work you, before the if you could try to check the value of lrow and dlen to make to sure they have a correct value.
Let me know if that works for you

Assuming you don't need formatting, I think you want something like this...
Dim ws As Worksheet
Set ws = ThisWorkbook.WorkSheets("HR-Cal")
dlen = 2 'change this
lrow = ws.Cells(Cells.Rows.Count, "B").End(xlUp).Row
For i = 7 To lrow
'checks curent row for box name in cell AL2
'copies values from col B to col G in that row to into Col AT, starting at AS6
If Left(Cells(i, "B"), dlen) = ws.Range("M2").Text Then 'change M2
'Range("B" & i).Rows.Select
'If Left(Cells(lrow, "B"), dlen) = Range("AL2").Value Then Range("B" & r & ":G" & r).Select
'Selection.Copy
Range(Cells(i, 10), Cells(i, 15)).Value = Range(Cells(i, 2), Cells(i, 7)).Value 'change columns
'Range("AT100000").End(xlUp).Offset(1, 0).PasteSpecial (xlPasteAll)
End If
Next i

Related

Several If statements and retrieve data in VBA

I have a question regarding this post (VBA copy rows that meet criteria to another sheet)
His script:
Sub LastRowInOneColumn()
'Find the last used row in a Column: column A in this example
Worksheets("Sheet2").Activate
Dim LastRow As Long
With ActiveSheet
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
End With
MsgBox (LastRow)
For i = 1 To LastRow
If Worksheets("Sheet2").Cells(i, 1).Value = "X" Then
ActiveSheet.Row.Value.Copy _
Destination:=Hoja1
End If
Next i
End Sub
I wonder if there are multiple conditions in just 1 If statement. The idea is to copy and paste data to another sheet if column1 = "X", or column2 ="Y", or (column1 = "X" and column2 = "Y").
I changed by myself and run, but the output sheet looks quite weird.
If Worksheets("Sheet2").Cells(i, 1).Value = "X" Or _
Worksheets("Sheet2").Cells(i, 2).Value = "Y" Or _
(Worksheets("Sheet2").Cells(i, 1).Value = "X" And _
Worksheets("Sheet2").Cells(i, 2).Value = "Y") Then

Selection of Continued filled Cells and Calculation of MAX,MIN,AVG

Hope You are all Safe
I'm trying to calculate MAX, MIN and AVG Values of filled cells which are continued without blank cell (As you can see it in the left side of the sample image ).
I'm facing problem in selecting these randomly placed cells and calculate the above values and also "From" and "To" values of respective range.
Please let me know how to do it. So far I've constructed following code
Dim Cel As Range
Dim lastrow As Long
Dim destSht As Worksheet
Set destSht = Worksheets("Final")
With Worksheets("Source")
lastrow = .Range("B" & .Rows.Count).End(xlUp).Row
For Each Cel In .Range("C2:C" & lastrow)
If .Cells(Cel.Row, "C") <> "" Then
Cel.Offset(0, -1).Copy Destination:=destSht.Cells(destSht.Rows.Count, 1).End(xlUp).Offset(0, 1)
'It will give "From" Column
'' Plz suggest for "To" Column
Range("G5").Select
ActiveCell.FormulaR1C1 = "=MAX(RC[-4]:R[4]C[-4])" 'It will give values "MAX" Column
Range("H5").Select
ActiveCell.FormulaR1C1 = "=MIN(RC[-5]:R[4]C[-5])" 'It will give values "MIN" Column
Range("I5").Select
ActiveCell.FormulaR1C1 = "=AVERAGE(RC[-6]:R[4]C[-6])" 'It will give values "AVG" Column
End If
Next
Did some quick, which should work.
I don't know what you want to do in the "Final" worksheet, so haven't focused on that line.
Logic is to have one big loop (For i...) that go through the whole Column C. When a value is found in column C (If .Cells(i, "C") <> "" Then), we perform a "small loop" (For j = i To lastrow + 1) to check next empty cell to decide the "small group" range. When that range is decided we perform the To, From, MAX, MIN and AVG formulas, which has to be dynamic.
Option Explicit
Sub trial()
Dim lastrow As Long
Dim destSht As Worksheet
Dim i As Long, j As Long
Set destSht = Worksheets("Final")
With Worksheets("Source")
lastrow = .Range("B" & .Rows.Count).End(xlUp).Row
For i = 2 To lastrow + 1 'loop whole range (column C)
If .Cells(i, "C") <> "" Then 'If column C is not empty then
For j = i To lastrow + 1 'Loop "group" range to find next empty cell. Start from current loop i to last row and add one row to get to next empty cell.
If .Cells(j, "C") = "" Then 'When next empty cell is found (i.e. end of small group range) then apply formulas
.Cells(i, "E").Value = .Cells(i, "B").Value 'From
.Cells(i, "F").Value = .Cells(j - 1, "B").Value 'To
.Cells(i, "G").Formula = "=MAX(C" & i & ":C" & j - 1 & ")" 'MAX
.Cells(i, "H").Formula = "=MIN(C" & i & ":C" & j - 1 & ")" 'MIN
.Cells(i, "I").Formula = "=AVERAGE(C" & i & ":C" & j - 1 & ")" 'AVG
Exit For
End If
Next j
End If
Next i
End With
End Sub
Result:

Copy ranges and multiply in loop

Ive been searching left and right but seem to only find bits and pieces. i'm unable to combine these into the solution i need.
My workbook has a list of items on the first sheet, the partnumbers in column A have to be searched for in Column A of a second sheet and if they exist there, those rows need to be copied to a third sheet.In steps i'm looking to do the following:
Column A of sheet1 (called "input") has several partnumbers.
After clicking CommandButton2 on sheet1, all partnumbers in Column A (starting in cell A5)should be searched for in Column A of sheet3 (called "partlists", starting in A2).
If found here, for all the respective rows where the partnumbers match: columns C to G("partlists") should be copied to sheet2("picklist") column A below the last row, the value in column E("picklist") has to be multiplied with the value in Column E("input") AND columns G to K("input") copied to the respective rows column G("Picklist")
If not found on "partlists", copy entire row from "input" to "picklist" below last row.
So far i've got the following code:
Sub InputPickMatch()
Dim LR As Long, i As Long, lngNextRow As Long, LookUpListInput As Range, LookUpListParts As Range
Set LookUpListInput = Sheets("Input").Range("A:A") 'lookup list Input
Set LookUpListParts = Sheets("Partlists").Range("A:A")
With Sheets("Input")
LR = .Cells(Rows.Count, "A").End(xlUp).Row 'last row
For i = 5 To LR
If IsError(Application.Match(.Cells(i, "A").Value, LookUpListParts, 0)) Then
.Range(Cells(i, "A").Address(), Cells(i, "D").Address()).Copy
Sheets("Picklist").Select
lngNextRow = Sheets("Picklist").Range("A" & Rows.Count).End(xlUp).Row + 1
Sheets("Picklist").Range("A" & lngNextRow).PasteSpecial _
Paste:=xlPasteValues
.Range(Cells(i, "F").Address(), Cells(i, "K").Address()).Copy
Sheets("Picklist").Range("E" & lngNextRow).PasteSpecial _
Paste:=xlPasteValues
End If
Next i
End With
With Sheets("Partlists")
LR = .Cells(Rows.Count, "A").End(xlUp).Row 'last row
For i = 3 To LR
If IsNumeric(Application.Match(.Cells(i, "A").Value, LookUpListInput, 0)) Then
.Range(Cells(i, "C").Address(), Cells(i, "G").Address()).Copy
Sheets("Picklist").Select
lngNextRow = Sheets("Picklist").Range("A" & Rows.Count).End(xlUp).Row + 1
Sheets("Picklist").Range("A" & lngNextRow).PasteSpecial _
Paste:=xlPasteValues
'Sheets("Picklist").Cells(lngNextRow, "E") = Sheets("Input").Cells(LookUpListInput, "E") * .Cells(i, "G") 'NOT WORKING: Multiply row from lookuplist column E with .Cells(i, "G")
'Sheets("Input").Range(Cells(LookUpList, "G").Address(), Cells(LookUpListInput, "K").Address()).Copy 'NOT WORKING: Copy row from lookuplist column G:K
'Sheets("Picklist").Range("F" & lngNextRow).PasteSpecial 'Paste Picklist column G
End If
Next i
End With
End Sub
It's working ok up to where i try to multiply and copy from the lookup list.
Hopefully someone can help
I got it guys
Sub InputToPicklist()
Dim LR As Long, i As Long, lngNextRow As Long, LookUpListInput As Range, LookUpListParts As Range
Dim Matchres As Variant
Set LookUpListInput = Sheets("Input").Range("A:A")
Set LookUpListParts = Sheets("Partlists").Range("A:A")
With Sheets("Input")
LR = .Cells(Rows.Count, "A").End(xlUp).Row
For i = 5 To LR
If IsError(Application.Match(.Cells(i, "A").Value, LookUpListParts, 0)) Then
lngNextRow = Sheets("Picklist").Range("A" & Rows.Count).End(xlUp).Row + 1
Sheets("Picklist").Range(Cells(lngNextRow, "A").Address(), Cells(lngNextRow, "D").Address()).Value = .Range(Cells(i, "A").Address(), Cells(i, "D").Address()).Value
Sheets("Picklist").Range(Cells(lngNextRow, "E").Address(), Cells(lngNextRow, "J").Address()).Value = .Range(Cells(i, "F").Address(), Cells(i, "K").Address()).Value
End If
Next i
End With
With Sheets("Partlists")
LR = .Cells(Rows.Count, "A").End(xlUp).Row 'last row
For i = 3 To LR
If IsNumeric(Application.Match(.Cells(i, "A").Value, LookUpListInput, 0)) Then
lngNextRow = Sheets("Picklist").Range("A" & Rows.Count).End(xlUp).Row + 1
Sheets("Picklist").Range(Cells(lngNextRow, "A").Address(), Cells(lngNextRow, "E").Address()).Value = .Range(Cells(i, "C").Address(), Cells(i, "G").Address()).Value
Matchres = Application.Match(.Cells(i, "A").Value, LookUpListInput, 0)
Sheets("Picklist").Cells(lngNextRow, "E") = Sheets("Input").Cells(Matchres, "F") * .Cells(i, "G") 'Multiply row from lookuplist column E with .Cells(i, "G")
Sheets("Picklist").Range(Cells(lngNextRow, "F").Address(), Cells(lngNextRow, "J").Address()).Value = Sheets("Input").Range(Cells(Matchres, "G").Address(), Cells(Matchres, "K").Address()).Value 'Copy row from lookuplist column G:K
End If
Next i
End With
Sheets("Input").Range("A5:K138").ClearContents
End Sub
First
Dim Matchres As Variant
and calling it
Matchres = Application.Match(.Cells(i, "A").Value, LookUpListInput, 0)
Does the trick

VBA- Need to loop through only visible cells

Trying to copy specific cells only from filtered rows, but this part of code keeps copying all data even from hidden cells.
Sheets(Wss).Range("A1").AutoFilter _
Field:=8, _
Criteria1:="Vesztesg", _
VisibleDropDown:=False
For j = 2 To Sheets(Wss).Range("A2").End(xlDown).End(xlDown).End(xlUp).Row - 1
Sheets("EBS Posting template").Range("C" & i) = Sheets(Wss).Range("E" & j)
Sheets("EBS Posting template").Range("O" & i + 1) = Sheets(Wss).Range("F" & j)
Sheets("EBS Posting template").Range("G" & i + 2) = Sheets(Wss).Range("J" & j)
i = i + 3
Next j
To loop through visible cells only, you need to loop through a range.
So instead of For i loop we use For Each that will let you do that. We add .SpecialCells(xlCellTypeVisible) to that range.
For each element in the range we declare a variable, cl. which we can use to extract data from.
Sheets(Wss).Range("A1").AutoFilter _
Field:=8, _
Criteria1:="Vesztesg", _
VisibleDropDown:=False
Dim cl As Range
For Each cl In Range(Cells(2, 1), Cells(Sheets(Wss).Range("A2").End(xlDown).End(xlDown).End(xlUp).Row - 1, 1)).SpecialCells(xlCellTypeVisible) 'Apply visible cells only
j = cl.Row 'row number of current cl value.
Sheets("EBS Posting template").Range("C" & i) = Sheets(Wss).Range("E" & j)
Sheets("EBS Posting template").Range("O" & i + 1) = Sheets(Wss).Range("F" & j)
Sheets("EBS Posting template").Range("G" & i + 2) = Sheets(Wss).Range("J" & j)
i = i + 3
Next cl 'loop to next cl
If you are using a counter to the rows, even if the row is hidden, it is still going to give you that row value.
You would require additional lines to make sure that row was not hidden.
Possibly just looping through the visible cells would suffice.
By the way: I am not sure where your values are ending up, you will have to edit that in your code.
The code will loop through the visible cells in column E, check the sheet name in the code as well, I wasn't sure about that either.
Sub Button2_Click()
Dim wss As Worksheet, sh As Worksheet, LstRw As Long, rng As Range, c As Range, Lr As Long
Set wss = Sheets("Sheets(Wss)")
Set sh = Sheets("EBS Posting template")
With wss
LstRw = .Cells(.Rows.Count, 8).End(xlUp).Row
.Range("A1").AutoFilter Field:=8, Criteria1:="Vesztesg"
Set rng = .Range("E2:E" & LstRw).SpecialCells(xlCellTypeVisible)
For Each c In rng.Cells
With sh
Lr = .Cells(.Rows.Count, "C").End(xlUp).Row + 1
.Cells(Lr, "C").Value = c
.Cells(Lr, "O").Value = c.Offset(, 1)
.Cells(Lr, "G").Value = c.Offset(, 5)
End With
Next c
.AutoFilterMode = False
End With
End Sub

Need help, If statement not providing desired result. Values in the comparing cells are the same but the if argument works half the time

Need help, If statement not providing desired result. Values in the comparing cells are the same but the if argument works half the time. Code provided below
Sub autofilter1()
For b = 1 To 4
' Last row of unique values - Unique Tab
lr = Sheets("Unique").Cells(Rows.Count, b).End(xlUp).Row
'Tabs = c
ws_count = ActiveWorkbook.Worksheets.Count
For c = 2 To ws_count
'Last row of column A
lr1 = Sheets(c).Cells(Rows.Count, 1).End(xlUp).Row
For i = 2 To lr
'Execution of auto filter program
Sheets(c).Range("A1:A" & lr1).autofilter Field:=1, Criteria1:=Sheets("Unique").Range("A" & i)
'Last row of Filtered visible cells
lr2 = Sheets(c).Cells(Rows.Count, 4).End(xlUp).Row
'Below line selects entire range of visible cells
'Sheets("Assets").Range("D2:D" & lr2).SpecialCells(xlCellTypeVisible).Select
'Selection of Cell to identify aggregate address 1) Range definition, 2) sub-class aggregate cell identifier
With Sheets(c).autofilter.Range
Range("D" & .Offset(1, 0).SpecialCells(xlCellTypeVisible)(1).Row).Select
End With
'Dynamic Sum function home
Selection.Offset(0, 1).Select
'First cell below primary (only in case of multiple sub accounts, else primary is account)
SS = Selection.Offset(1, -1).Address
'Final cell of dynamic autofilter range
SE = ("D" & lr2)
Rng = Sheets(c).Range(SS & " : " & SE).SpecialCells(xlCellTypeVisible)
ActiveCell = Application.WorksheetFunction.Sum(Rng)
If ActiveCell.Value = ActiveCell.Offset(0, -1).Value Then
ActiveCell.Offset(0, 1) = "True"
ActiveCell.Offset(0, 1).Font.Bold = True
ActiveCell.Offset(0, 1).Interior.Color = 5296274
Else
ActiveCell.Offset(0, 1) = "False"
ActiveCell.Offset(0, 1).Font.Bold = True
ActiveCell.Offset(0, 1).Interior.Color = 255
End If
Next i
Next c
Next b
End Sub

Resources