Excel VBA Vlookup where user chooses what cells - vba

I am creating a macro to do a vlookup between 2 different sheets within the same workbook, but sometime the layout of the cells changes so I wish to be able to add a dialog box that will allow the user to select what they are looking for and what list they would like to compare against but am not sure how to do this.
This is my code thus far:
ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-59],ONCE!C[-49],1,0)"
Range("BI2").Select
Selection.AutoFill Destination:=Range("BI2:BI208032")
Range("BI2:BI208032").Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _ :=False, Transpose:=False
Thanks in advance

You can use InputBox like this:
myValue = InputBox("Give me some input")

Related

How to edit Formula Bar in macro with variable output?

I am currently working on a macro that needs to, at one point, click into the formula bar of the current cell, and then press enter. However, when I record this, despite making no edits to the cell, it inputs it based on the text in the cell at the end. The current code is this:
Sub Macro36()
Application.CutCopyMode = False
Selection.Copy
ActiveCell.Offset(1, 0).Range("A1").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
ActiveCell.Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = _
"='C:\Users\User\Documents\[TimeSheet.xlsx]Sheet1'!R[-3]C[-1]"
ActiveCell.Offset(1, 0).Range("A1").Select
End Sub
The portion from "ActiveCell.FormulaR1C1" onward is where the issue occurs. It automatically does this while recording, rather than having the macro select the formula bar in the cell and then press enter. This is necessary to be able to do because it would allow a workaround for the INDIRECT function not working on closed documents.
Has anyone come across this issue/would know how to fix it? Thank you for any responses.

vba code to copy worksheets and paste as values to a new workbook

I'm not very good at vba so excuse my amateur question.
I have an active workbook open containing 3 tabs. I want to build a macro that opens up another workbook and pastespecial values the data from my three tabs into the three tabs on the second workbook.
This is my coding which keeps breaking on the paste special line.
Sub NewVersion_Click()
Dim y As Workbook
ThisWorkbook.Sheets("Fact Find").Range("A5:I283").Copy
Set y = Workbooks.Open("location")
y.Worksheets("Fact Find").Range("A1").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
'ThisWorkbook.Sheets("Entity Fact Find").Range("A4:F237").Copy
'y.Worksheets("Entity Fact Find").Range("A4").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
'ThisWorkbook.Sheets("Suitability Assessment Form").Range("A4:E108").Copy
'y.Worksheets("Suitability Assessment Form").Range("A4").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
ActiveWorkbook.Close
End Sub
Any help will be really appreciated.
Thanks
Arvin
Here is your paste special for worksheet fact find.
Modify the code to copy the other sheets, but no need to open the file again.
Which file are you trying to close?
Set wb = ThisWorkbook
wb.Sheets("Fact Find").Range("A5:I283").Copy
Workbooks.Open ("location")
Workbooks("location").Worksheets("Fact Find").Range("A1").PasteSpecial xlPasteValues
wb.Sheets("Entity Fact Find").Range("A4:F237").Copy
Workbooks("location").Worksheets("Entity Fact Find").Range("A1").PasteSpecial xlPasteValues

VBA Copy Paste Special

I am trying to copy a range of cells from Sheet "Stream" to the Sheet "General".
Somehow my paste function is not working. Any hint?
Stream.Range("F103:J103").Copy General.Cells(General.Range("F2:J2").PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:=_False, Transpose:=False)
Sheets("Stream").Range("F103:J103").Copy
Sheets("General").Range("F2").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
For value pasting, you need to first copy the range and then paste to destination.
Also you have to use Sheets() to identify different sheets.

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

Excel delete formula but keep values witha lot of rows

I'm working with a macro excel. I'm trying to delete the formula but keep the values of the cells as it makes filtering/sorting so slow. I already tried the following:
Range("A2:E70000").Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, _
SkipBlanks :=False, Transpose:=False
and
Sheet1.Range("A2:E70000").Values = Sheet1.Range("A2:E70000").Values
but both of them take too long to finish(about 30mins. to complete). Are there any faster ways to do this? Thanks in advance.
Bracket your code with
Application.ScreenUpdating = False
and
Application.ScreenUpdating = True
and you should see performance improve dramatically.
Visuals like selecting the cells are going to slow this down considerably. Also selecting to row 70,0000? If this is actually the case then fair enough, but if not then why?
Have edited your first code slightly. This will stop the screen updating until finished, won't 'select' any cells (visually) and will find the last actual row that is in use instead of just blanket selecting a range in hope it gets everything.
Try:
Application.ScreenUpdating = False
with Sheet1.Range("A2:E" & Sheet1.Range("E" & .Rows.Count).End(xlUp).Row)
.copy
.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, _
SkipBlanks :=False, Transpose:=False
end with
Application.ScreenUpdating = True