Vlookup range with a defined variable - vba

I'm consolidating data from several excel files, I have 22 files to consolidate; however, I need to specifically look up for a cell value.
In my VBA code I declared a variable called file=int(1) and I'm doing a Loop to open and consolidate each excel file to a consolidated file. All of my excel files are numbered from 1 to 22.
What I want to to is Vlookup, but the range of the Vlookup I want it to have the stored value in file while doing the loop.
For example
file = Int(1)
Do Until file = 22
ActiveCell.Formula = "=IFERROR(VLOOKUP($I$6,'[file.xls]Sheet1'!$B$4:$V$27,MATCH($F$26,'[file.xls]Sheet1'!$B$3:$V$27,0),FALSE),0)"
ActiveCell.Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
ActiveCell.Select
Selection.Offset(1, 0).Select
file = file + 1
Can you please help me?
Thanks!

Nice tidy little loop! OK, I think I have an idea what you're trying to do here. You want to have the [file.xls] in your formula update dynamically for each iteration of the loop. Since you're using the formula property, which takes a string, you can concatenate your stored file value right into it.
This should do the trick.
ActiveCell.Formula = "=IFERROR(VLOOKUP($I$6,'[" & file & ".xls]Sheet1'!$B$4:$V$27,MATCH($F$26,'[" & file & ".xls]Sheet1'!$B$3:$V$27,0),FALSE),0)"
Hope I understood properly, if not, we'll go another round!

Related

Using VBA macro to filter by variable

I have set up a template to pull data from another spreadsheet by filtering said spreadsheet on a variable established in my template.
I can not seem to get the macro to properly filter on my variable (it keeps filtering so that no data remains visible). The spreadsheet I am pulling from goes from column A to AM and has 20830 rows.
I need to utilize my variable to filter column B. Column B is currently set up as a VLOOKUP and I have tried paste valuing the column but to no avail. Any insight as to how I can accomplish this would be greatly appreciated! Please let me know if I need to specify anything more.
Below I have provided my current VBA script (any advice on improving it would also be appreciated). Again, thank you so much for your help!! (Variable to filter on is z, established in an earlier portion of the script)
'Below will grab the Holdings data
Workbooks.Open Filename:= _
"S:\Cashinvt\Audits\D&T" & v & "\PAM.allhold" & y & ".w.stat.xlsm", UpdateLinks:=0
Sheets("allhold").Select
'Here is where I start questioning my code, filtering on variable z
Columns("B:B").Select
ActiveSheet.Range("$B$1:$B$22001").AutoFilter Field:=2, Criteria1:="z"
'Here I am trying to copy the newly filtered data and paste it into another spreadsheet
Range("B1:AM20831").Select
Selection.SpecialCells(xlCellTypeVisible).Select
Selection.Copy
Windows("Audit.Support.Template.xlsm").Activate
Sheets("Holdings").Select
Range("A1").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Windows("S:\Cashinvt\Audits\D&T" & v & "\PAM.allhold" & y & ".w.stat.xlsm").Activate
Sheets("allhold").Select
Windows("S:\Cashinvt\Audits\D&T" & v & "\PAM.allhold" & y & ".w.stat.xlsm").Activate
ActiveWorkbook.Close SaveChanges:=False
Application.DisplayAlerts = True
Sheets("Cover Page").Select
Range("K1").Select
End Sub
ActiveSheet.Range("$B$1:$B$22001").AutoFilter Field:=2, Criteria1:=" & z & "
You are giving a range of only one column but stating Field:=2 try giving the range as the full table, ie
ActiveSheet.Range("$A$1:$AM$22001").AutoFilter Field:=2, Criteria1:=CInt(z)
Notes: i have also included Dougs comment regarding the criteria

Excel Macro is only copying "false" value of a logic statement

I used the Macro Recorder to create a macro to copy files from one worksheet to a second worksheet and reset a form to default values.
When the macro is run the "false" value ("NO") of a logic statement is copied regardless of the value in the cell at the time the macro is run. If I change the value in the false statement from "NO" to any other value (i.e. "Blue") it copies over the new value ("Blue").
Here is the formula for the logic statement:
=IF(AND(D15>VLOOKUP(C13,CCT,3,FALSE),D15<VLOOKUP(C13,CCT,4,TRUE)),"YES","NO")
where CCT is a list.
Here is the code for the macro:
Range("D17").Select
Selection.Copy
Sheets("Worksheet2").Select
Range("G3").Select
ActiveSheet.Paste
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Worksheet1").Select
You might try Worksheet.Activate after you select different sheets. Worksheet.Calculate might be in order as well.
I think that using Range... implies Activesheet.Range. Hope it helps

VBA - Copy Values Only

Following on from my previous question, which was answered perfectly I have now written code for the remaining part of my problem, however I have now developed problems.
Part of the worksheet uses =RAND() to generate a random number. As this is a volatile function I needed to copy the output of the formula to a new location. If I was doing this manually I would do a copy -> paste special values so that I just go the numbers, and not the formula.
When trying this in VBA I get an Error 1004 during part of the code when I try to select the destination range for the paste special.
Here is the code:
' Copy Random Questions to Static Page for VLOOKUPS
With Worksheets("Quiz Generator")
Range("NEWQUEST").Copy
'Selection.Copy
End With
With Worksheets("Static Question List")
Range("TOPSTAT").Select
Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End With
I've tried various ways of doing this, using
RANGE().Select
Selection.Copy
RANGE().Select
Selection.Pastespecial Paste:=xlValues
Also using:
RANGE().Copy
Range().PasteSpecial Paste:=xlValues
In the original code I can get through to the
Range("TOPSTAT).Select
Before it throws the
Run-Time error '1004':
Application-defined or object-defined error
Pop-up
Any help would be gratefully received.
All of the defined ranges are correct and in Name Manager, and I've tried with cell ref's to see if it was the range name that was the issue.
Annoyingly this worked previously using the long-hand Select / Selection.Paste etc method, but since trying to tidy the code it stopped.
Thanks in advance.
I would do something like this, to
dump the range from NEWCREST as values into an array
dump the array to the first cell in TOPSTAT and resize as needed
(you don't need sheet names when working with range names unless you have local range names)
Code
Dim x
x = Range("NEWQUEST").Value2
Range("TOPSTAT").Cells(1).Resize(UBound(x, 1), UBound(x, 2)) = x

Macro to copy values only from one sheet to another

I've this VBA code
Sheets("log").Range("A125:f1000").Copy _
Sheets("data").Cells(Rows.Count, "A").End(xlUp).Offset(1)
and it copies perfectly from sheet log to data. The only problem I'm facing is that it copies formulas with it as well whereas I only want values. I want to use same VBA code with some modifications to paste values only.
Without using clipboard:
Sheets("data").Cells(Rows.Count, "A").End(xlUp).Offset(1).Value = Sheets("log").Range("A125:f1000").Value
Need to add PasteSpecial Paste:=xlPasteValues
Next time try Recording a macro and modifying the code
Sheets("log").Range("A125:f1000").Copy
Sheets("data").Cells(Rows.Count, "A").End(xlUp).Offset(1). _
PasteSpecial Paste:=xlPasteValues, _
Operation:=xlNone, SkipBlanks:=False, Transpose:=False

Copy and paste from one workbook to another

I have code in one workbook, this should open another workbook, copy and paste into the workbook with the code. I can select the data but can't paste it.
I have tried many different variations of code getting errors or it doesn't do anything. An example is run in template.xls, which is where I want to paste the data:
Set dlsheet = appexcel.Workbooks.Open(strPath & "downloadedData.xls")
With dlsheet.Sheets("Data")
.range("A1:H3").Select.copy
selection.copy
End With
I don't know how to use the selection since this will copy from the template, I tried using a full stop before selection.
I can copy the entire sheet from dlsheet into a new workbook, if someone could tell me how to copy it to the template and not a new workbook then this would also do the trick.
dlsheet.Sheets("Data").Copy
Set dlsheet = appexcel.Workbooks.Open(strPath & "downloadedData.xls")
dlsheet.Sheets("Data").range("A1:H3").copy
ThisWorkbook.ActiveSheet.Paste Destination:=ThisWorkbook.ActiveSheet.Range( "A1:H3")
Try this
Set dlsheet = appexcel.Workbooks.Open(strPath & "downloadedData.xls")
With dlsheet
.Sheets("Data").Range("A1:H3").Copy
.Sheets("Data").Range("A1").PasteSpecial Paste:=xlPasteValues, _
Operation:=xlNone, SkipBlanks:=False, Transpose:=False
End With