Vlookup/Xlookup (loop?) data from another sheet, then paste as values - excel

Had no luck figuring out how to code this in VBA.
I have a list of names on ws2 starting in cell A5. The size of this list will change daily. I want to vlookup each item from data on ws1. Then would like paste the data as values into corresponding cell in column C.
Picture below is what ws2 looks like. That's the vlookup formula I would have used if done manually.
Could also consider xlookup if you think it's better. Lookup array would be column A on ws1, return array would be column E.
Hoping to keep the code as simple as possible since this is my first VBA project. Any comments/explanations would be much appreciated!
Thanks!

Tab names
If 'ws1' and 'ws2' are the tab names of the worksheets this should do it, note I've changed A:L to A:E in the formula as you aren't looking up anything past column E.
Sub Button1_Click()
Dim ws As Worksheet
Set ws = Sheets("ws2")
With ws.Range("C5:C" & ws.Range("A" & Rows.Count).End(xlUp).Row)
.Formula = "=VLOOKUP(A5, ws1!A:E,5,0)"
.Value = .Value
End With
End Sub
Variable names
If 'ws1' and 'ws2' are variable names referencing sheets then use this.
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Set ws1 = Sheets("Sheet1")
Set ws2 = Sheets("Sheet2")
With ws2.Range("C5:C" & ws2.Range("A" & Rows.Count).End(xlUp).Row)
.Formula = "=VLOOKUP(A5,'" & ws1.Name & "'!A:E,5,0)"
.Value = .Value
End With
End Sub
Codenames
Finally, if ws1 and ws2 are actually the codenames of the worksheets this should work.
Sub Button1_Click()
With ws2.Range("C5:C" & ws2.Range("A" & Rows.Count).End(xlUp).Row)
.Formula = "=VLOOKUP(A5,'" & ws1.Name & "'!A:E,5,0)"
.Value = .Value
End With
End Sub

Related

Excel VBA: Applying formula to multiple columns with a set row number

I've been wracking my brain to figure this out and I have a feeling that it's such an easy fix.
I have a formula that I need to apply to columns I to Z, and commencing row 3 until the last row of data in column C. I've managed to define what the "lastRow" is, but I cannot for the life of me work out how to apply the formula to columns I to Z.
Any help would be greatly appreciated! Thanks in advance :)
My VBA macro is below:
Sub Calculation()
Dim ws1 As Worksheet
Dim rng As Range
Dim lastRow As Long
Set ws1 = Sheets("Sheet 2")
With ws1
lastRow = .Range("C" & .Rows.Count).End(xlUp).Row
.Range("I3:I" & lastRow).Formula = "=1+2"
End With
End Sub
A small change in the Range
Sub Calculation()
Dim ws1 As Worksheet
Dim rng As Range
Dim lastRow As Long
Set ws1 = Sheets("Sheet 2")
With ws1
lastRow = .Range("C" & .Rows.Count).End(xlUp).Row
.Range("Z3:I" & lastRow).Formula = "=1+2"
End With
End Sub
Let's try and understand how Range works in VBA
The VBA Range Object represents a cell or multiple cells in your Excel worksheet.
A single cell : Range("A1")
A row : Range("1:1")
A column : Range("A: A")
For Contiguous Cells : Range("A1:C5")
For Non-Contiguous Cells : Range("A1:C5, F1:F5")
For Intersection of two ranges : Range("A1:C5 F1:F5")
(For intersection cell, remember there is no comma operator)

How to copy duplicates into another sheet?

I have the macro below runs (no errors) but no results are provided. I have an excel book where duplicates are sometimes found in column "E". Those identified as duplicates should be copied to sheet 2. I know my workbook has duplicates in column E, their just not being copied over.
Sub FilterAndCopy()
Dim wstSource As Worksheet, _
wstOutput As Worksheet
Dim rngMyData As Range, _
helperRng As Range
Set wstSource = Worksheets("Sheet1")
Set wstOutput = Worksheets("Sheet2")
Application.ScreenUpdating = False
With wstSource
Set rngMyData = .Range("a1:R" & .Range("a" & .Rows.Count).End(xlUp).Row)
End With
Set helperRng = rngMyData.Offset(, rngMyData.Columns.Count + 1).Resize(, 1)
With helperRng
.FormulaR1C1 = "=if(countif(c1,RC1)>1,"""",1)"
.Value = .Value
If Evaluate("=COUNTBLANK(" & .Address & ")") > 0 Then .SpecialCells(xlCellTypeBlanks).EntireRow.Copy Destination:=wstOutput.Cells(2, 1)
.ClearContents
End With
Application.ScreenUpdating = True
End Sub
Because of the cell notation you are using R1C1, your formula
.FormulaR1C1 = "=if(countif(c1,RC1)>1,"""",1)"
refers only to column A
If you want to change your formula to apply on column E, that would be
.FormulaR1C1 = "=if(countif(c5,RC5)>1,"""",1)"
I don't know if your dupplicates are on a single column basis or if should concatenate all your column to see if the entire row is a dupplicate. It might be more simple to apply a sql query connection on this. Pivot tables may also help. If you stick to vba, add additional line for each dupplicate formula and process it in the block of code that copy blank formula to the secondary sheet.

Print name of the sheet along with copied cell

I have this code where it loops through all the sheets in the workbook and copies the value in F9 of each sheet and pastes it in "Summary" sheet column A. How can I also print the sheet name in column B? So the value is next to the sheet name in the "Summary" sheet.
code:
Sub loopsheet()
Dim wks As Worksheet
For Each wks In ThisWorkbook.Worksheets
If Not wks.Name = "Summary" Then
wks.Range("F9:F" & wks.Cells(Rows.Count, "F").End(xlUp).Row).Copy _
Destination:=Worksheets("Summary").Cells(Rows.Count, "A").End(xlUp).Offset(1)
End If
Next
End Sub
Thank you
Create two variables to track the last rows of your sheets as you loop. This will help with readability in your code. The combination of these two variables can also help you deduce the size of the range where you need to drop your sheet name.
I believe cLR + pLR - 11 is the size of range. The offset is due to headers, LR offset, and the fact that you are starting your copy from the 9th row. After you run this, you may need to tweak it up or down one if i'm wrong.
Option Explicit
Sub LoopSheet()
Dim ws As Worksheet
Dim Summary As Worksheet: Set Summary = ThisWorkbook.Sheets("Summary")
Dim cLR As Long, pLR As Long
For Each ws In ThisWorkbook.Worksheets
If ws.Name <> Summary.Name Then
cLR = ws.Range("F" & ws.Rows.Count).End(xlUp).Row
pLR = Summary.Range("A" & Summary.Rows.Count).End(xlUp).Offset(1).Row
ws.Range("F9:F" & cLR).Copy Summary.Range("A" & pLR)
Summary.Range(Summary.Cells(pLR, 2), Summary.Cells(cLR + pLR - 11, 2)).Value = ws.Name
End If
Next ws
End Sub

Can I insert a variable into a string?

I'm trying to make a program in the Excel VBA that inserts a formula into a column of cells. This formula changes based on the contents of the cell directly to the left.
This is the code I have written so far:
Sub Formula()
Dim colvar As Integer
colvar = 1
Dim Name As String
Name = "Sample, J."
Do While colvar <= 26
colvar = colvar + 1
Name = Range("B" & colvar).Value
Range("C" & colvar).Value = "='" & Name & "'!N18"
Loop
End Sub
As you can see, I want to insert the variable Name between the formula strings, but Excel refuses to run the code, giving me a "application-defined or object-defined error."
Is there a way to fix this?
You will need some error checking in case the sheets don't actually exist in the workbook.
it looks like you are looping through column B that has a list of sheet names and want range N18 to display next to it.
Something like
Sub Button1_Click()
Dim Lstrw As Long, rng As Range, c As Range
Dim Name As String
Lstrw = Cells(Rows.Count, "B").End(xlUp).Row
Set rng = Range("B1:B" & Lstrw)
For Each c In rng.Cells
Name = c
c.Offset(, 1) = "='" & Name & "'!N18"
Next c
End Sub
Or you can just list the sheets and show N18 next to it, run this code in a Sheet named "Sheet1"
Sub GetTheSh()
Dim sh As Worksheet, ws As Worksheet
Set ws = Sheets("Sheet1")
For Each sh In Sheets
If sh.Name <> ws.Name Then
ws.Cells(ws.Rows.Count, "A").End(xlUp).Offset(1) = sh.Name
ws.Cells(ws.Rows.Count, "A").End(xlUp).Offset(0, 1) = sh.Range("N18")
End If
Next sh
End Sub
Thank you to everyone for your help! I actually found that I had just made a silly error: the line Do While colvar<=26 should have been Do While colvar<26. The cells were being filled, but the error manifested because one cell was being filled by a nonexistent object.
I did decide to use the .Formula modifier rather than .Value. Thank you to Jeeped for suggesting that.

Method range of object _global failed

I simply want to use the value in C2 to populate the rest of column C with the same value all the way to the bottom of the data in the sheet.
Sub Testfill1()
'
' Testfill1 Macro
'
'
Sheets(1).Select
lngEndRow = lastCellBlock
Range("C2:C" & lngEndRow).FormulaR1C1 = "1"
End Sub
I simply want to use the value in C2 to populate the rest of column C with the same value all the way to the bottom of the data in the sheet.
This should do what you want:
Sub FillDown()
Range("C2", "C" & rows.Count).FillDown
End Sub
If that doesnt work or doesnt answer your question let me know
This will find the last row in column C with a value and copy whatever is in C2 down to that cell.
Sub CopyDown()
Dim ws As Worksheet
Dim lastRow As Long
Set ws = ThisWorkbook.Sheets("Sheet1")
lastRow = ws.Range("C" & ws.Rows.Count).End(xlUp).Row
ws.Range("C2").Copy ws.Range("C2:C" & lastRow)
End Sub
Try below code
Sub Testfill1()
Dim lastRow As Long
With Sheets("sheet1")
lastRow = .Range("C" & .Rows.Count).End(xlUp).Row
.Range("C2:C" & lastRow).FormulaR1C1 = "1"
End With
End Sub
I simply want to use the value in C2 to populate the rest of column C with the same value all the way to the bottom of the data in the sheet.
I am interpreting "bottom of the data" to mean the extents of your data and not the absolute bottom of the worksheet (the latter being C1048576 in Excel 2007/2010/2013). It isn't clear on whether C2 contains a formula or not so that should likely be left alone and its value stuffed into the remaining cells from C3 down to the extents of the data.
Range("C3:C" & cells(rows.count, 3).end(xlUp).row) = Range("C2").value

Resources