How to paste/equalize specific range value in one sheets into another? - excel

I have named "FormEntry" in Range("E7,F11,H12,I8,E16") in Sheet1.
and picture above is Sheet2.
Is that possible to copy Range "FormEntry" from Sheet1 and paste/equalize into the same Range in Sheets2 without other cells range in Sheet2 being replaced?
I've tried this syntax but the values on Sheets2 are A's
Sub test()
Sheets("Sheet2").Range("E7,F11,H12,I8,E16").Value = Sheets("Sheet1").Range("E7,F11,H12,I8,E16").Value
End Sub

You can use:
For Each cell In Range("FormEntry")
Sheets("Sheet2").Cells(cell.Row, cell.Column).Value = cell.Value
Next

Related

How can I extract data from database sheet and paste matched range in different sheets

I have multiple sheets in a workbook. I wan't to copy cells containing SPT values for each Boreholes from the "combine" sheet based on sheetname matching within range listed within "A5:A116" or may be more after final data entry to the sheet matching with the borehole ID /cell value within range "A5:A116" and paste it below the SPT column. This has to continue through a loop process for all the sheets. Also a relative referencing from the matched cell to the range to be copied will be required. In the code the range D5:D16 will have to be a floating ranged based on relative referencing from cell value. Also the paste range will start from 1 cell below the SPT value in the matched.name sheet The code I am working on is as follows the relative referencing for range does not seem to work:
Sub example()
Dim wkSht As Worksheet
For Each wkSht In Sheets
For Each Cell In Sheets("Combine").Range("A5:A116")
If Cell.Value = wkSht.Name Then
On Error Resume Next
Sheets("Combine").Range("D5:D16").Copy Destination:=wkSht.Range("D29:D52")
End If
Next Cell
Next wkSht
End Sub
Data Extraction in different sheets
You sequence for the looping is incorrect, and you did not declare the variable for cell, please try the following code modification and it shall work properly:
Sub example()
Dim wkSht As Worksheet
Dim cell As Range
For Each cell In Sheets("Combine").Range("A5:A116").Cells
For Each wkSht In ThisWorkbook.Worksheets
If cell.Value = wkSht.Name Then
Sheets("Combine").Range(cell.Offset(0, 11), cell.Offset(30, 11)).Copy wkSht.Range("D29")
End If
Next wkSht
Next cell
End Sub

Copy Cell From Range On Active Row

I'm trying to use VBA to copy a cell on the active row of a range.
A1:A10 is a range named "Panel_Length". I want to click a button to copy the cell from column "A" of the active row. The code works if I specify column "A" and active row but there is a chance that the range "Panel_Length" may get moved to another column.
Sub Plen_Click()
Dim PLength as Range
Set PLength = Sheet1.Range("Panel_Length")
Range(PLength & ActiveCell.Row).Copy
End Sub
Don't know if I understand the question correct. but if you try to copy the cell from column of the named range and the row form the selected cell you can try the code below
Sub Plen_Click()
Sheet1.Cells(Selection.Row, Sheet1.Range("Panel_Length").Column).Copy
End Sub

Change formula to next cell down

I am trying to compare values from a vlookup to a static value in the column to the left, and then paste the value if it matches.
Column J contain static values
Column K contains a vlookup formula referencing another sheet
I need to loop through column K, and if the value from the vlookup matches the value in the same row in column J, then paste the value into column K.
So if cell J2 = 240.89, and the value of the vlookup in cell K2 = 240.89, then paste the value 240.89 into K2.
So far, the code I have loops through the range but stops at the last pasted value and ignores the vlookup formulas.
Here is the code I have:
Option Explicit
Sub CheckValue()
Dim myRange As Range
Dim cell As Range
Set myRange = Range("K2:K3194")
For Each cell In myRange
If cell.Value = cell.Offset(0, -1) Then
cell.Copy
cell.PasteSpecial xlPasteValues
End If
Next cell
End Sub
Any help is appreciated!
Just for fun another option:
Sub Test()
Dim ws as Worksheet: Set ws = Thisworkbook.Worksheets("Sheet1")
ws.Range("K2:K3194").Value = ws.Evaluate("IF(K2:K3194=J2:J3194,J2:J3194,FORMULATEXT(K2:K3194))")
End Sub
Note: This requires FORMULATEXT, a function available since Excel2013.
Try
For Each cell In myRange
If cell.Value2 = cell.Offset(0, -1).Value2 Then
cell.value = cell.value
End If
Next cell
or maybe use a WorksheetFunction.Round to match the values to a specific precision. I feel like this is more the issue than anything.

Referencing a range on another sheet in vba code to copy a formula down to the last row of data

I have a workbook with two sheets. On sheet1 I have a column (A) for which I need to copy the formula in A2 down for as many rows as there are in sheet2 column (B).Currently I use the following code
Range("B3:B" & Cells(Rows.Count,"C").End(xlUp).Row).Formula = Range ("B2")
to do something similar but where the range referred to is on the same worksheet. What I need to know is how to adapt this code to replace the "C" above with the range on sheet 2. Can someone please help?
you could use:
With Sheet1 ' reference Sheet1 worksheet
.Range("B3:B" & Sheet2.Cells(Rows.Count, "B").End(xlUp).Row).Formula = .Range("B2").Formula
End With
to copy sheet1 cell B2 formula down as many rows as Sheet2 column B last not empty cell row index
For every Range(), Cells(), Rows() and Columns() object specify in which sheet they are. Don't miss any of them or your code can randomly fail.
Example
Worksheets("Sheet1").Range("A1") 'addresses cell A1 in Sheet1
You can use variables as references:
Dim ws As Worksheet
Set ws = ThisWorkbook.Worksheets("Sheet1")
ws.Range("A1") 'now also addresses cell A1 in Sheet1
while
Range("A1")
will address the sheet that has focus (is on top) while the code runs. Don't rely on this because this can easily change by a single mouse click of the user. Therefore always specify which sheet you mean.

How to copy specific cells from one sheet to another meting if criteria

I'm brand new to VBA.
I want to copy specific cells from one sheet to another based on a criteria.
For instance if in the range of H2:H1000 value = "Yes" then copy cells from column A,B,C and D from same row where the criteria meets to another sheet.
My code is just a scratch from what I managed to do, I don't know how to select the cells from the same row where the criteria meets.
Sub FindandCopy()
Dim rngA As Range
Dim cell As Range
Set rngA = Sheets("OFCE").Range("H2:H1000")
For Each cell In rngA
If cell.Value = "Yes" Then
cell.EntireRow.Copy
Sheets("Dashboard").Range("I2").End(xlDown).Select
ActiveSheet.Paste
End If
Next cell
End Sub
You might want to try using v-lookup or a simple sumif function. You can do this outside of vba, in the sheet itself, or you can program it into your existing macro.
Plenty of material online on this so you should be able to solve it.
I used offset (to get from H to A) and resize (to expand A to A-D) and then copy to the first unused row of I on the other sheet.
You don't need to Select/Activate, in fact it's discouraged.
Sub FindandCopy()
Dim rngA As Range
Dim cell As Range
Set rngA = Sheets("OFCE").Range("H2:H1000")
For Each cell In rngA
If cell.Value = "Yes" Then
cell.Offset(, -7).Resize(, 4).Value.Copy _
Sheets("Dashboard").Range("I" & Rows.Count).End(xlUp)(2)
End If
Next cell
End Sub
However, rather than a loop, you should consider using AutoFilter or Find to do this as they are more efficient.

Resources