vba vlookup fill to last row - vba

I am new to this VBA and need some help with my code. I manage to get my code to vlookup from last row in column O but I dont know how to fill it to match last row of column E.
My goal is vlookup from last row of O fill to last row of E
Dim JPNpart, PartNumber, myRange, LastRow As Long
LastRow = Range("E" & Rows.Count).End(xlUp).Row
JPNpart = "[JPN_part.xlsx]Sheet1"
Sheets("Sheet1").Select
Range("O2").Select
Selection.End(xlDown).Offset(1, 0).Select
PartNumber = ActiveCell.Offset(0, -13).Address
myRange = "'" & JPNpart & "'!A:G"
Range("O2").Select
Selection.End(xlDown).Offset(1, 0).Select
ActiveCell.Formula = "=VLOOKUP(" & PartNumber & "," & myRange & ", 7, FALSE)"
'how i do to make this formula fill till last row
Range("P2").Select
Selection.End(xlDown).Offset(1, 0).Select
ActiveCell.Formula = "=VLOOKUP(" & PartNumber & "," & myRange & ", 2, FALSE)"
Range("E2").Select
Selection.End(xlDown).Select
Thanks for your help.

You could have figured it out by cleaning the Select/Selection and Activate/ActiveCell.
Here is your code cleaned of that and made more understable :
Dim JPNpart As String, PartNumber As String, myRange As String, LastRow As Long
JPNpart = "[JPN_part.xlsx]Sheet1"
myRange = "'" & JPNpart & "'!A:G"
With ThisWorkbook.Sheets("Sheet1")
'For column O
LastRow = .Range("O" & .Rows.Count).End(xlUp).Row
PartNumber = "B" & LastRow + 1
.Range("O" & LastRow).Offset(1, 0).Formula = "=VLOOKUP(" & PartNumber & "," & myRange & ", 7, FALSE)"
'For column E
LastRow = .Range("E" & .Rows.Count).End(xlUp).Row
PartNumber = "B" & LastRow + 1
.Range("E" & LastRow).Offset(1, 0).Formula = "=VLOOKUP(" & PartNumber & "," & myRange & ", 7, FALSE)"
End With 'ThisWorkbook.Sheets("Sheet1")

Related

count how many rows are consolidated in vba

I have created a macro that consolidates rows that contain the same value in column D and provides the average of the rows consolidated. I am trying to write a line of code inside the code provided below, which will count the individual rows that have been consolidated and paste the result next to the consolidated row (column Q) as it can be sheen form the pictures. Picture 1 contains the initial table and picture 2 contains the consolidated table.
any ideas? Much appreciated!
UPDATE!
These are the updated pictures
The whole process is PERFECT until the row Q (it was the last column before the update). I added three more columns to the destination table and one more to the source table.. if it is possible, I want for the column R the macro to consolidate the rows and print their averaged Gross WFR delivered to the column R ONLY if the column I of the row is 0. Also, I want the macro to count these rows (containing 0) that it consolidates (just like it does for column Q) and print the number in column S. Finally, IF it is possible to count the number of these rows (containing 0) that are out of TARGET and print the number in column K. what I mean by out of TARGET is that for these rows K(values)-E(values)>3%.
FINAL UPDATE OF THE CODE
Dim ws As Worksheet
Dim dataRng As Range
Dim dic As Variant, arr As Variant
Dim cnt As Long
Set ws = Sheets("1")
With ws
lastrow = .Cells(.Rows.Count, "D").End(xlUp).Row 'get last row in Column D
Set dataRng = .Range("D2:D" & lastrow) 'range for Column D
Set dic = CreateObject("Scripting.Dictionary")
arr = dataRng.Value
For i = 1 To UBound(arr)
dic(arr(i, 1)) = dic(arr(i, 1)) + 1
Next
.Range("M2").Resize(dic.Count, 1) = Application.WorksheetFunction.Transpose(dic.keys) 'uniques data from Column D
.Range("Q2").Resize(dic.Count, 1) = Application.WorksheetFunction.Transpose(dic.items)
cnt = dic.Count
For i = 2 To cnt + 1
.Range("N" & i & ":P" & i).Formula = "=SUMIF($D$2:$D$" & lastrow & ",$M" & i & ",E$2:E$" & lastrow & ")/" & dic(.Range("M" & i).Value)
.Range("R" & i).Formula = "=IF(INDEX($I$2:$I$" & lastrow & ",MATCH($M" & i & ",$D$2:$D$" & lastrow & ",0))=0,N" & i & ",0)"
.Range("S" & i).Formula = "=IF(INDEX($I$2:$I$" & lastrow & ",MATCH($M" & i & ",$D$2:$D$" & lastrow & ",0))=0,Q" & i & ",0)"
.Range("T" & i).Formula = "=IF($S" & i & ">0,SUMPRODUCT(($D$2:$D$" & lastrow & "=$M" & i & ")*(($J$2:$J$" & lastrow & "-$E$2:$E$" & lastrow & ")>3%)),0)"
Next i
.Range("N" & i & ":T" & i).Formula = "=SUM(N2:N" & cnt + 1 & ")"
.Range("N2:T" & .Cells(.Rows.Count, "M").End(xlUp).Row + 1).Value = .Range("N2:T" & .Cells(.Rows.Count, "M").End(xlUp).Row + 1).Value
End With
Try this:
Sub Demo()
Dim ws As Worksheet
Dim dataRng As Range
Dim dic As Variant, arr As Variant
Dim cnt As Long
Set ws = ThisWorkbook.Sheets("Sheet4") 'change Sheet4 to your data sheet
Application.ScreenUpdating = False
With ws
lastRow = .Cells(.Rows.Count, "D").End(xlUp).Row 'get last row in Column D
Set dataRng = .Range("D2:D" & lastRow) 'range for Column D
Set dic = CreateObject("Scripting.Dictionary")
arr = dataRng.Value
For i = 1 To UBound(arr)
dic(arr(i, 1)) = dic(arr(i, 1)) + 1
Next
.Range("M2").Resize(dic.Count, 1) = Application.WorksheetFunction.Transpose(dic.keys) 'uniques data from Column D
.Range("Q2").Resize(dic.Count, 1) = Application.WorksheetFunction.Transpose(dic.items) 'count of shipment
cnt = dic.Count
For i = 2 To cnt + 1
.Range("N" & i & ":P" & i).Formula = "=SUMIF($D$2:$D$" & lastRow & ",$M" & i & ",E$2:E$" & lastRow & ")/" & dic(.Range("M" & i).Value)
Next i
.Range("N2:P" & .Cells(.Rows.Count, "M").End(xlUp).Row).Value = .Range("N2:S" & .Cells(.Rows.Count, "M").End(xlUp).Row).Value
End With
Application.ScreenUpdating = True
End Sub
Assumption: Your data is in range Column D:ColumnG and want output in Column M:ColumnQ.
EDIT :
Sub Demo()
Dim ws As Worksheet
Dim dataRng As Range
Dim dic As Variant, arr As Variant
Dim cnt As Long
Set ws = ThisWorkbook.Sheets("Sheet5") 'change Sheet4 to your data sheet
Application.ScreenUpdating = False
With ws
lastRow = .Cells(.Rows.Count, "D").End(xlUp).Row 'get last row in Column D
Set dataRng = .Range("D2:D" & lastRow) 'range for Column D
Set dic = CreateObject("Scripting.Dictionary")
arr = dataRng.Value
For i = 1 To UBound(arr)
dic(arr(i, 1)) = dic(arr(i, 1)) + 1
Next
.Range("M2").Resize(dic.Count, 1) = Application.WorksheetFunction.Transpose(dic.keys) 'uniques data from Column D
.Range("Q2").Resize(dic.Count, 1) = Application.WorksheetFunction.Transpose(dic.items)
cnt = dic.Count
For i = 2 To cnt + 1
.Range("N" & i & ":P" & i).Formula = "=SUMIF($D$2:$D$" & lastRow & ",$M" & i & ",E$2:E$" & lastRow & ")/" & dic(.Range("M" & i).Value)
.Range("R" & i).Formula = "=IF(INDEX($I$2:$I$" & lastRow & ",MATCH($M" & i & ",$D$2:$D$" & lastRow & ",0))=0,N" & i & ","""")"
.Range("S" & i).Formula = "=IF(INDEX($I$2:$I$" & lastRow & ",MATCH($M" & i & ",$D$2:$D$" & lastRow & ",0))=0,Q" & i & ","""")"
.Range("T" & i).Formula = "=IF(ISNUMBER($S" & i & "),SUMPRODUCT(($D$2:$D$" & lastRow & "=$M" & i & ")*(($K$2:$K$" & lastRow & "-$E$2:$E$" & lastRow & ")>3%)),"""")"
Next i
.Range("N2:T" & .Cells(.Rows.Count, "M").End(xlUp).Row).Value = .Range("N2:T" & .Cells(.Rows.Count, "M").End(xlUp).Row).Value
End With
Application.ScreenUpdating = True
End Sub
EDIT 2 :
Instead of
.Range("N2:T" & .Cells(.Rows.Count, "M").End(xlUp).Row).Value = .Range("N2:T" & .Cells(.Rows.Count, "M").End(xlUp).Row).Value
write
.Range("N" & i & ":T" & i).Formula = "=SUM(N2:N" & cnt + 1 & ")"
.Range("N2:T" & .Cells(.Rows.Count, "M").End(xlUp).Row + 1).Value = .Range("N2:T" & .Cells(.Rows.Count, "M").End(xlUp).Row + 1).Value
EDIT 3 :
Sub Demo_SO()
Dim ws As Worksheet
Dim dataRng As Range
Dim dic As Variant, arr As Variant
Dim cnt As Long
Set ws = ThisWorkbook.Sheets("Sheet5") 'change Sheet4 to your data sheet
Application.ScreenUpdating = False
With ws
lastRow = .Cells(.Rows.Count, "D").End(xlUp).Row 'get last row in Column D
Set dataRng = .Range("D2:D" & lastRow) 'range for Column D
Set dic = CreateObject("Scripting.Dictionary")
arr = dataRng.Value
For i = 1 To UBound(arr)
dic(arr(i, 1)) = dic(arr(i, 1)) + 1
Next
.Range("M2").Resize(dic.Count, 1) = Application.WorksheetFunction.Transpose(dic.keys) 'uniques data from Column D
.Range("Q2").Resize(dic.Count, 1) = Application.WorksheetFunction.Transpose(dic.items)
cnt = dic.Count
For i = 2 To cnt + 1
.Range("N" & i & ":P" & i).Formula = "=SUMIF($D$2:$D$" & lastRow & ",$M" & i & ",E$2:E$" & lastRow & ")/" & dic(.Range("M" & i).Value)
.Range("R" & i).Formula = "=IF(INDEX($I$2:$I$" & lastRow & ",MATCH($M" & i & ",$D$2:$D$" & lastRow & ",0))=0,N" & i & ",0)"
.Range("S" & i).Formula = "=IF(INDEX($I$2:$I$" & lastRow & ",MATCH($M" & i & ",$D$2:$D$" & lastRow & ",0))=0,Q" & i & ",0)"
.Range("T" & i).Formula = "=IF($S" & i & ">0,SUMPRODUCT(($D$2:$D$" & lastRow & "=$M" & i & ")*(($K$2:$K$" & lastRow & "-$E$2:$E$" & lastRow & ")>3%)),0)"
Next i
.Range("N" & i & ":T" & i).Formula = "=SUM(N2:N" & cnt + 1 & ")"
.Range("N2:T" & .Cells(.Rows.Count, "M").End(xlUp).Row + 1).Value = .Range("N2:T" & .Cells(.Rows.Count, "M").End(xlUp).Row + 1).Value
End With
Application.ScreenUpdating = True
End Sub

Type mismatch (Error 13) in Loop

Line 4 is messing my loop up with a type mismatch! What am I doing wrong?
For i = 4 To 8
j = 20 + i
Col = Columns(j)
Range("'" & Col & "3'").FormulaR1C1 = "=IF(RC[-11]=0,0,(IF(SUMIF(R3C2:R" & lRow & "C2, RC2,R3C" & i & ":R" & lRow & "C" & i & ")>RC[-11]*1000000, SUMIF(R3C2:R" & lRow1 & "C2, RC2,R3C" & i & ":R" & lRow & "C" & i & ")- RC[-11]*1000000,0)))"
Next i
Try this:
For i = 4 To 8
Cells(3, 20 + i).FormulaR1C1 = "=IF(RC[-11]=0,0,(IF(SUMIF(R3C2:R" & lRow & "C2, RC2,R3C" & i & ":R" & lRow & "C" & i & ")>RC[-11]*1000000, SUMIF(R3C2:R" & lRow1 & "C2, RC2,R3C" & i & ":R" & lRow & "C" & i & ")- RC[-11]*1000000,0)))"
Next i
By using Cells or Range on their own Excel will assume you want to reference the active worksheet, in the active workbook. It's a much better idea to specify exactly which workbook/ worksheet you want the code to run on. E.g.:
For i = 4 To 8
ThisWorkbook.Worksheets("Sheet1").Cells(3, 20 + i).FormulaR1C1 = "=IF(RC[-11]=0,0,(IF(SUMIF(R3C2:R" & lRow & "C2, RC2,R3C" & i & ":R" & lRow & "C" & i & ")>RC[-11]*1000000, SUMIF(R3C2:R" & lRow1 & "C2, RC2,R3C" & i & ":R" & lRow & "C" & i & ")- RC[-11]*1000000,0)))"
Next i
You're doing many errors.
First, col is a column, not a (range address) string. You cannot concatenate a column to a string.
Second, you should not enclose a range address with single-quotes (').
What you probably wanted to do is:
Cells(3, j).Formula = ...

how to initialise my counter in vba excel

I have a problem with my vba project.
My workbook has 4 sheets (Draft, cky, coy and bey), in the sheet "draft i have all my data and i want to reorganise them. the columns "G" of the sheet "draft" contains the values (cky, coy and bey).
I want my macro to go through the colums and copy all the cells that have the same value and paste them in their corresponding sheet starting at the cell (A2), for exemple: i want the macro to copy all the data that have "cky" and paste it in the sheet "cky" starting at the cell A2 and so on/
Below you can see what i have done so far:
Sub MainPower()
Dim lmid As String
Dim srange, SelData, ExtBbFor As String
Dim lastrow As Long
Dim i, j, k As Integer
lastrow = ActiveSheet.Range("B30000").End(xlUp).Row
srange = "G1:G" & lastrow
SelData = "A1:G" & lastrow
For i = 1 To lastrow
If InStr(1, LCase(Range("E" & i)), "bb") <> 0 Then
Range("G" & i).Value = Mid(Range("E" & i), 4, 3)
ElseIf Left(Range("E" & i), 1) = "H" Then
Range("G" & i).Value = Mid(Range("E" & i), 7, 3)
Else
Range("G" & i).Value = Mid(Range("E" & i), 1, 3)
End If
Next i
'Sorting data
Range("A1").AutoFilter
Range(SelData).Sort key1:=Range(srange), order1:=xlAscending, Header:=xlYes
'Spreading to the appropriate sheets
j = 1
For i = 1 To lastrow
If Range("G" & i).Value = "CKY" Then
Sheets("CKY").Range("A" & j & ":E" & j).Value = Sheets("Draft").Range("C" & i & ":G" & i).Value
ElseIf Range("G" & i).Value = "BEY" Then
Sheets("BEY").Range("A" & j & ":E" & j).Value = Sheets("Draft").Range("C" & i & ":G" & i).Value
ElseIf Range("G" & i).Value = "COY" Then
Sheets("COY").Range("A" & j & ":E" & j).Value = Sheets("Draft").Range("C" & i & ":G" & i).Value
End If
j = j + 1
Next i
End Sub
Thank you to help
best regards
Use this refactored code in the For Loop and it should work for better for you:
For i = 1 To lastrow
Select Case Sheets("Draft").Range("G" & i).Value
Case is = "CKY","COY","BEY"
Dim wsPaste as Worksheet
Set wsPaste = Sheets(Range("G"& i).Value)
Dim lRowPaste as Long
lRowPaste = wsPaste.Range("A" & .Rows.COunt).End(xlup).Offset(1).Row
wsPaste.Range("A" & lRowPaste & ":E" & lRowPaste).Value = _
Sheets("Draft").Range("C" & i & ":G" & i).Value
End Select
Next i

How to specify a range of cells

I am trying to parse data from multiple workbooks with multiple worksheets into a single summary worksheet or workbook. So far I have been able to collect data from the specified cells, however I would like to include a range of cells for example ("A2:B20"). How can I specify this in looping process?
Option Explicit
Sub GetMyData()
Dim myDir As String, fn As String, sn As String, sn2 As String, n As Long, NR As Long
'***** Change Folder Path *****
myDir = "C:\attach"
'***** Change Sheetname(s) *****
sn = "Title"
sn2 = "Monday"
fn = Dir(myDir & "\*.xlsx")
Do While fn <> ""
If fn <> ThisWorkbook.Name Then
With ThisWorkbook.Sheets("Sheet10")
NR = .Cells(Rows.count, 1).End(xlUp).Row + 1
'Pick cells from worksheet "Title"
With .Range("A" & NR)
.Formula = "='" & myDir & "\[" & fn & "]" & sn & "'!B4"
.Value = .Value
End With
With .Range("B" & NR)
.Formula = "='" & myDir & "\[" & fn & "]" & sn & "'!B5"
.Value = .Value
End With
With .Range("C" & NR)
.Formula = "='" & myDir & "\[" & fn & "]" & sn & "'!B6"
.Value = .Value
End With
With .Range("D" & NR)
.Formula = "='" & myDir & "\[" & fn & "]" & sn & "'!B7"
.Value = .Value
End With
With .Range("E" & NR)
.Formula = "='" & myDir & "\[" & fn & "]" & sn & "'!A1"
.Value = .Value
End With
With .Range("F" & NR)
.Formula = "='" & myDir & "\[" & fn & "]" & sn & "'!A2"
.Value = .Value
End With
'pick cells from worksheet "Monday"
With .Range("G" & NR)
.Formula = "='" & myDir & "\[" & fn & "]" & sn2 & Range("A1:C57")
End With
End With
End If
fn = Dir
Loop
ThisWorkbook.Sheets("Sheet10").Columns.AutoFit
End Sub
You can do Either
Col_1 = "A"
Col_2 = "B"
i = 2
j = 20
Range(Col_1 & i,Col_2 & j)
or
Col_1 = "A"
i = 2
j = 20
Range(Col_1 & i).Resize(j-i+1,2)
Hope this helps
There are a couple of ways to do this, supposing you want a continuous range:
Pass that exact string to the Range function. e.g. Range("A3:C10")
Pass the "first" cell as the first argument and the "last cell" as the second argument. e.g. Range("A3", "C10")

VBA Macro Search Difference in Range

I need to check the range from A1 to AY1, B1 to BY1.... from sheet 1 and compare them with the range from A1 to AY1, B1 to BY1.... from sheet 2 and highlight in yellow the difference in sheet 1.
Sub copyWorkingFileToConsolidated()
Dim i As Integer
i = 5
Do While Sheet2.Range("A" & i).Value <> ""
Sheet3.Range("A" & i & ":D" & i).Value = Sheet2.Range("A" & i & ":D" & i).Value
Sheet3.Range("F" & i & ":Y" & i).Value = Sheet2.Range("E" & i & ":X" & i).Value
Sheet3.Range("A" & i & ":Y" & i).Interior.ColorIndex = 0
i = i + 1
Loop
getResourceLevel
End Sub
Please help
Thanks