VBA Apply Formula to All Cells not Working - vba

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

Related

How to make dynamic cell in formula based on pointed value in excel spreadsheet

I have an issue with the cell range in formula and I don't know how to change it based on a predefined value in the spreadsheet. For example, from figure I have cells B8:B12=0 (5 cells), however, if I want to change range to range B10:B12=0 (3 cells) I should delete them from formula. How can I do the reference to a specific cell in a spreadsheet where I can simply change value 5 to 3 and it will change automatically, without interfering formula each time? I'm new to VBA, any help is appreciated.
As it was mentioned before, you should try offset function and do something like:
AND(SUM(OFFSET(D13,-1,-2,-(G6),1))=0). Then the cells in the range B8:B12 would be possible to change inserting range in cell G6.
Use the =INDIRECT function to define your target cells, e.g.
=TEXT(INDIRECT(A1), "")
If I entered the text B3 into cell A1, then this formula would return the text value in cell B3.
Let me know if it works for you.

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.

Paste data Excel VBA

I am pasting a lot of data during a VBA macro in excel and for some reason when it pastes, it puts the data at a random point in the sheet and I am not sure why. I have pasted the paste command below. Any help would be greatly appreciated!
Sheet5.Range("A1", "AH5000").SpecialCells(xlCellTypeVisible).Copy
Sheet9.Paste
Where do you want this pasted? I am going to assume you want it pasted in cell A1 on Sheet9. Change "Sheet9.Range("A1") to whatever you need (ie Sheet9.Range("B1") to paste into cell B1).
Sheet5.Range("A1", "AH5000").SpecialCells(xlCellTypeVisible).Copy Sheet9.Range("A1")
I guess it pasts the data into active(marked) cell on the Sheet9, am I right? You should specify a cell in the second line of your code.

Copy range until empty row and paste into new sheet

I'm trying to write a VBA macro for Excel 2013. It's purpose is to merge two worksheets into a combined worksheet. (I tried to find a built in feature to do this but was unable to find what I needed).
What the macro needs to do is this:
Activate "Sheet3" and clear all rows starting with row 3 and down
Go into "Sheet1" and copy all rows starting with (A3:P3) and copy down until, and not including, the first row with null in the A column.
Go into "Sheet3" and paste those rows starting at A3.
Go into "Sheet2" and copy all rows starting with (A3:P3) and copy down until, and not including, the first row with null in the A column.
Go into "Sheet3" and past those rows starting at the first empty cell in column A.
I'm a novice at VBA but I've managed to find the following code and I'm trying to make it work to accomplish the above requirements.
Sub CreateCombinedSheet()
lastRow = ActiveSheet.Cells(65536, lastCol).End(xlUp).Row
ActiveSheet.Range("A3", ActiveSheet.Cells(lastRow, 12)).Copy
End Sub
I'm trying to write parts of it and test it as I go but I'm already getting a 1004 error with this:
Application-defined or object-defined error
Any thoughts on how I should work this?
Thanks
Activate "Sheet3" and clear all rows starting with row 3 and down
You do not need to activate a sheet to clear the rows. You may want to see THIS You can directly say
Sheets("BlahBlah").Rows("3:200").ClearContents
I have hardcoded 200 as an example. To find the end of rows, see the below point.
Go into "Sheet1" and copy all rows starting with (A3:P3) and copy down until, and not including, the first row with null in the A column.
Same for this. You do not need to go to that sheet. You need to first find the last row. I see that you are hardcoing the rows in your code. You don't need to do that. xl2007+ now has 1048576 rows. Use .Rows.Count Please see THIS
Go into "Sheet3" and paste those rows starting at A3.
To paste, you again don't need to go to that sheet. You can directly say
rng.Copy Sheet("BlahBlah").Rows(3)
Your point 4 and 5 are just variations of the above. I am sure you can now take it from here :). In case you still face any difficulty, simply post back.

VBA autofilter and visible cells

here's something weird i cant figure out:
after applying filter, and LR=2, with the following code:
Row 1 and Row 2 have values in each cell all thru A1:AH2
For Each a In Range("O2:O" & LR).SpecialCells(xlCellTypeVisible)
debug.print a
Next a
instead of picking only "O2" cell the loop goes through all cells in row 1.
this is a special case as LR=2 happens very rarely in my macro.
can anyone help me figure out what's happening?
Thanks