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

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

Related

Adding a new cell reference to an existing formula

New to these forums (posting anyway - been using them as a reference for a while!).
I have a formula that calculates average unit size by adding up cells from other sheets in the same book.
I have a macro that creates a new template sheet, and am now working on the macro to update the summary tab with the data from the newly created sheet.
For Example my formula reads:
=SUM(Sheet2!X7,Sheet3!X7,Sheet4!X7)/F8
F8 is the total number of units, each X7 is the total unit size.
What I can't work out is if a macro can edit the above formula so that when I have added sheet 5, I run the macro and it edits the formuala to read:
=SUM(Sheet2!X7,Sheet3!X7,Sheet4!X7,Sheet5!X7)/F8
You can change the formula to always include any new sheets.
You will just have to add the new sheet between the first and last sheet you are summing.
=SUM('Sheet2:SheetLast'!X7)/F8
You will not need to edit this formula, so long as any new sheets are between Sheet2 and SheetLast in your file.
As mentioned by Mooseman, you will not need to edit this formula, as long as any new sheets are between Sheet2 and SheetLast in your file.
But to address your specific question regarding the ability of a macro to modify the formula: yes, a macro can edit that formula. Assuming your formula is in a specific cell such as "A2", you could have a macro like this:
Sub MyMacro()
Range("A2").Select
ActiveCell.FormulaR1C1 ="=SUM(Sheet2!X7,Sheet3!X7,Sheet4!X7,Sheet5!X7)/F8"
End Sub

Applying conditional formatting from cells being called into another sheet in Excel VBA

Is there a way in VBA that I can format each cell in one sheet of my workbook based off of the cell formatting of a cell in another sheet?
In one of my sheets, I have a conditional format that changes the color of the cell based off a formula. I want to use VBA to copy the cell's color into a separate sheet's cell. I tried to use Format Painter but it was copying the conditional format's equation when all I need is the color of the cell.
Any ideas?
PasteSpecial Formats
Copy a whole worksheet's formats
Sheets("Sheet1").Cells.Copy
Sheets("Sheet2").Cells.PasteSpecial Paste:=xlPasteFormats
Copy a range's formats
Sheets("Sheet1").Range("A1").Copy
Sheets("Sheet2").Range("A1").PasteSpecial Paste:=xlPasteFormats

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

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.

Copy and paste to new sheet

I am trying to copy a row of data from one sheet to another having run some other macros, however I keep getting the error message that the paste area and copy area are not the same size and shape.
I have tried using special cells paste method but it seems be missing the data from the last 9 columns.
Code:
id.EntireRow.SpecialCells(xlCellTypeConstants).Copy
missingdata.Range("A" & Rows.Count).End(xlUp).Offset(1,1).PasteSpecial xlPasteValuesAndNumberFormats
Application.CutCopyMode = False
I need help changing the code so it copies the full row of data and pastes it under existing entries in missingdata.
It seems when it works when I choose to paste in Column A, however it does not allow me to paste from column B
I normally use:
Rows(index).Copy
Cells(1,Rows.Count+1).Select
ActiveSheet.Paste

How do I increment cell in Excel VBA script?

I have a data in excel which I want to make a VBA script to copy it into a new worksheet but in a different way.
For example, I have this in sheet1 in A1~A3 cells.
Adam(A1)
Sam(A2)
Smith(A3)
I want to use these cells and create the following in another worksheet using refedit control.
Adam(A1)
Adam(A2)
Adam(A3)
Adam(A4)
Sam(A5)
Sam(A6)
Sam(A7)
Sam(A8)
Smith(A9)
Smith(A10)
Smith(A11)
Smith(A12)
I have refedit control in place in VBA script, but I'm not sure how to increment cell numbers to make it copy and paste into a new worksheet. I would like to use refedit control so that I can assign any cells and make it copy and repeat itself. How do I do this in VBA script?
Check out the Range Rows, Cells, and Address properties. This should help. Your question is too vague for a direct answer.
(This will get you started.)
Range.Row Property
http://msdn.microsoft.com/en-us/library/bb221550(office.12).aspx
Returns the number of the first row of the first area in the range. Read-only Long.
Example
For Each rw In Worksheets("Sheet1").Rows
If rw.Row Mod 2 = 0 Then
rw.RowHeight = 4
End If
Next rw
To increment cells in Excel VBA, you can use the Offset-property of the Range-object, e.g.
ActiveCell.Offset(1, 1).Select
will select the cell one row down and one column to the right of the active cell.
To add to Geoffrey's answer about active cell - it would also require that you activate the sheet you are looking to input your values if it is a different sheet from the one that is currently active. Additionally you would have to activate a cell to use activecell and the activecell offset property.
For example
'Activates the name of the sheet you would like to activate
Sheets("Sheet2").Activate
'Activates cell A1
Range("A1").Activate
'Activates cell one row down, one column right
ActiveCell.Offset(1,1).Select
'if current sheet is not activate you just do Sheets("Sheet2").Range("A1").Activate
The offset property of ActiveCell refers to other cells based off of the current active cell.
For example-
Offset(row,column) -
First Argument -Positive values as the first argument refer you to rows below the current active cell and Negative values refer you to rows above the current active cell
Second Argument-Positive values as the second argument refer you to columns right of the current active cell and Negative values refer you to columns left the current active cell