VBA Excel: How to assign range of cells to another range of cells? - vba

New Excel VBA user here. I feel like I made a silly syntax mistake.
I am trying to copy some vertical cells in one worksheet, to horizontal cells in another worksheet.
Worksheets("Master").Range("D14:F14").Value = Worksheets("Temp").Range("B12:B14").Value
However, the above command seems to only paste the value of B12 to cells D14:F14. Is there a way to paste the cells of B12, B13, and B14 to D14:F14?

Use Application.Transpose():
Worksheets("Master").Range("D14:F14").Value = Application.Transpose(Worksheets("Temp").Range("B12:B14").Value)
Or PasteSpecial:
Worksheets("Temp").Range("B12:B14").Copy
Worksheets("Master").Range("D14").PasteSpecial xlPasteAll, , , True
If you only want values then the first is best. When transposing with PasteSpecial one must use the xlPasteAll. It will throw an error if one tries to paste values only with transpose as true.

Related

VB.net copy and paste excel cell to elsewhere in the workbook

So i am currently using Visual Studio to create a application that takes info out of an excel sheet and then does some calculations on the data and then pushes back to excel.
This bit i have managed to do but the bit i am struggling on is using a 'Parameters' sheet. I want to be able to enter a formula into a cell in one sheet of the workbook and then paste that formula into another sheet but to have it updating,e.g. as the cells go down the formula changes like it would in excel. I used a manual work around by hard coding the formula and then having variable as the row number, however i want to be able to just change the formula in the excel sheet and then when the code runs it applies to the rest.
Currently i have tried saving the cell value/text into a variable and then making the new cells equal that variable, however this then applies the same identical formula to the whole of the column(All required rows).
What i am currently trying to do is paste the variable into the top row and then copy and paste that cell down to the last one,
I have tried making the variable a formula but it evaluates the formula before it is equal to the variable and therefore just sets all the new cells to the formula answer, so i changed the cell to be text instead which then meant the formula did appear in the new cell however it was the identical formula for all cells.
The copy code works as below
bjExcel.cells(rown, colval) = param1
objExcel.cells(rown, colval).copy
This is working fine
But when i use the below the paste won't work
Do Until rown = 10
objExcel.cells(rown, colval).copy
rown = rown + 1
objExcel.cells(rown, colval).paste
Paste is not a recognized with the error:
System.MissingMemberException: 'Public member 'Paste' on type
'ApplicationClass' not found.'
Could be you need to use PasteSpecial instead?
https://learn.microsoft.com/en-us/office/vba/api/excel.range.pastespecial
Depends on what you're using to interop with excel.
shWorkSheet.Range("C7:C7").Copy()
shWorkSheet.Range("V7:V7").PasteSpecial(Excel.XlPasteType.xlPasteAll)

Highlighted cells that use formulas won't copy the highlight to another sheet

I have a macro that highlights cells in a column that uses formulas.
I'm using this code to highlight the cells:
With Sheets("Sheets1").Range("G:G").SpecialCells(xlCellTypeFormulas)
.Interior.ColorIndex = 6
End With
However, I'm trying to use another script to copy this data from Sheet 1 to Sheet 2 and when I run the script, the highlighting is removed from the cell but the data is still copied into the new sheet (Sheet 2).`
Sheets("Sheets2").Range("G3:G100").Copy
Sheets("Sheets2").Activate
Sheets("Sheets2").Range("A1").Offset(0, l - 1).PasteSpecial xlPasteValues`
I need assistance trying to copy the cells over to the other sheet and to maintain the highlight on the cells that use a formula.
Thanks in advance!
Because you are pasting values the formats don't come along. Add a second paste as follows:
Sheets("Sheets2").Range("A1").Offset(0, l - 1).PasteSpecial Paste:=xlPasteFormats

VBA Apply Formula to All Cells not Working

I need to apply the formula to all cells on Sheet2 and am getting the "Method'Range' of object'_Global' failed" error.
Here's the VBA code I'm using:
Range("D2:AJ" & LastRow).Formula = "=OFFSET(Sheet1!$D$2,(ROW(1:1)-1)+INT((ROW(1:1)-1)/2)*8,COLUMN(A:A)-1)"
The formula copies every 2 rows from Sheet1 and skips 8 rows and it's working when I manually enter it.
Could you please let me know what I'm doing wrong?
Thanks much!
You could try setting the formula into the first cell, then copying and pasting into the rest as per this answer here: Set formula to a range of cells

SpecialCells in Excel to return value of a formula

I wanted a quick simple way to copy cell values to another sheet using SpecialCells in Excel as opposed to looping
My VBA code is as below:
Sub copyMissingData()
Worksheets("Source").Range("Z4:Z2000").SpecialCells(xlCellTypeConstants).Copy Worksheets("Destination").Range("missing_qbc")
End Sub
My source data Z4:Z20000 has formulas that returns a value (texts/numbers/fraction etc) or blank "". I want the copy to ignore the blanks, but copy any other value returned
The VBA code above using SpecialCells(xlCellTypeConstants) doesn't work because of the formula in the source range.
My question: Is there a straightforward way I can use range.specialcells to copy my data from a worksheet to another bearing in mind that source cells contain formulas and the formulas may produce empty string cells which will need to be skipped
If you have formulas, why are you trying to select the constants?
Use this:
Worksheets("Source").Range("Z4:Z2000").SpecialCells(xlCellTypeFormulas, 23).Copy
Worksheets("Destination").Range("missing_qbc").pastespecial(xlPasteValues)
The 23 means "Numbers, Texts, Logicals and Errors".
Doing the copy and paste separately ensure blanks are skipped (if that's what you mean by "ignore").
Paste values makes sure only the values get pasted, not the formulas themselves.
Please note that if you have a formula in a cell, it is not blank. Even if the formula produces an empty string value as a result, the cell itself is not empty! In htat case, you need to do a copy-paste values in place before you do anything else - and even then Excel sometimes doesn't consider blank cells blank. If this is the case, you need to iterate (loop) through the cells, and copy them one-by-one.
The easiest way I can think of is to remove the blanks after copying all:
Set rngFrom = [Source!Z4:Z2000]
Set rngTo = [Destination!missing_qbc].Resize(rngFrom.Rows.Count, 1)
rngTo.Value = rngFrom.Value
rngTo.SpecialCells(xlCellTypeBlanks).Delete Shift:=xlUp
The more complicated way is with array formula, but doesn't need VBA.

Only change values on active worksheet

I have a workbook with 4 sheets. The 4 sheets are almost identical Some of the cells values are calculated by calling a macro in the cell. For the active worksheet this Works fint. The macro gets all the values from the correct sheet and the calculation is correct.
But, when the calculation is done in the active worksheet the cells in all the worksheet that calls the macro is changed with the value calculated in the active worksheet.
How do I make sure that only the cells in the active workseet that calls the macro is changed?
[EDIT]
Example:
I have 2 sheets.
In both sheets I have a cell that calls a function: .Value = "=FunctionName()"
When I do calculations in sheet 1, the cell with the call is showing correct result. But the cell in sheet 2 is showing the same result.
Only the cell in sheet 1 should show the result from the function calculated from sheet 1.
Say if you want to change in sheet1. Then
Worksheets("Sheet1").Activate
With ActiveWorksheet
Call calculationsmacro 'your macro to calculate the cells
End With
I had kind of similar problem and it was solved by this method.
If it works for you then it would be really grateful if you can mark my answer.