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

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

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.

Excel VBA Vlookup where user chooses what cells

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")

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.

Copy/PasteSpecial Offset macro is slow when switching between worksheets

Intro: I've been working on this macro for quite awhile and have had success getting it to do what I need it to do, however I'm not doing it the most efficient way as my coding background is minimal. Because of this the macro is extremely slow due to switching back and forth from one worksheet "FB MATE DATA" to another "Sheet 2 (2)" copying and pasting data.
Background: A CMM machine spits out measurement data into a spreadsheet that I need to copy and paste from one sheet to another in neat X and Y columns to be overlayed in a scatter plot. The problem is that the data is in an unconventional pattern that requires the use of an offset, and different sheets have data that starts in different rows so the macro must account for that. The reason it is so slow is because the way i have achieved this is by constantly activating each worksheet back and forth, over and over to maintain control over the active cell instead of just referencing an offset from a fixed cell due to the offset changing occasionally in a row. The code i have now:
cycles down through column A until it finds a blank cell
shifts down to the cell immediately below it and starts copying data over. (This is very important because the first few rows are data irrelevant to me, and they are separated from the data i seek by a space.)
copies data from the range (G:FZ) in an offset (e.g. G21, K21, O21, S21, W21, AA21, AE21, AI21, AM21, AQ21, AU21, AY21, BG21) into a neat X|Y table from D73:E:93. Notice the irregular pattern (due to info extraneous to my plot)
Every two represent an X|Y pair (e.g. X: G21 pasted into D73 in Sheet 2, Y: K21 pasted into E73 in Sheet 2, etc.)
Problem: For each row of data, I need to copy certain columns in an interval that changes (first column, fourth column, ninth column, twelvth column, etc.) and paste it into two X|Y columns on another page.
The data grab will not always start in G21. Sometimes the first data point may be G24, or G10, or G15, hence why the macro searches for a blank row first instead of pulling from a fixed position. Below is a sample of cycling through the first four columns.
Code:
Sub LocatorTest()
'Select first row of eligible spare pallet data
Range("B73").Select
Application.Goto (ActiveWorkbook.Sheets("FB MATE DATA").Range("A1"))
Dim c
For Each c In Range("A1:A100").Cells
If c = "" Then
c.Select
Exit For
End If
Next
ActiveCell.Offset(1, 6).Range("A1").Select
Selection.Copy
Sheets("Sheet2 (2)").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("C73").Select
Worksheets("FB MATE DATA").Activate
ActiveCell.Offset(rowOffset:=0, columnOffset:=4).Activate
Selection.Copy
Sheets("Sheet2 (2)").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("B74").Select
Worksheets("FB MATE DATA").Activate
ActiveCell.Offset(rowOffset:=0, columnOffset:=8).Activate
Selection.Copy
Sheets("Sheet2 (2)").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("C74").Select
Worksheets("FB MATE DATA").Activate
ActiveCell.Offset(rowOffset:=0, columnOffset:=12).Activate
Selection.Copy
Sheets("Sheet2 (2)").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End Sub
Any help you can provide into maybe a different way of achieving this or making it run quicker would be greatly appreciated.
o wow, this can use some MAJOR improvements.
but to make it easy, you might want to put this at the start:
Application.Calculation = xlCalculationManual
Application.ScreenUpdating = False
and this at the end of your macro:
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
This will give it huge speed boost already. Next; you should try to reference to the cells without activating...

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