Paste data Excel VBA - 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.

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)

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

Problems with paste as values in VBA

I have a problem in paste as values a multiple selection, this is what I have to do:
I have a spreadsheet, every cell has a formula in it and I want to create a macro which copy each cell and paste on itself as value (in order to remove every formula). The problem is that there are some cells which are locked, so if I try to copy and paste them excel returns an error and it stops the procedure.
Now I have two possibilities:
case 1: copy and past as values each cell individually, but I have a lot of cells and I have to do this procedure very often.
case 2: create a big selection which contains only the unlocked cells and then copy and paste them all togheter.
case 2 seems to be the better choice, but with excel I can't copy and paste multiple selection...does anyone have a tip for me?
I though to take the big fragmentary selection and copy and paste every block of the selection one by one, but I don't know if is it possible :(
Loop throught every cell in selection and check if they are locked like this:
For Each Z In Selection
if Not Z.Locked Then
'do copy paste here
End If
Next Z

Clear contents and formatting of an Excel cell with a single command

If you want to clear the contents of a cell or range in Microsoft Excel, you can use .ClearContents. If you also want to clear the formatting, you can use .ClearFormats.
Sheets("Test").Range("A1:C3").ClearContents
Sheets("Test").Range("A1:C3").ClearFormats
If you want to do both, you can use .Delete, but then the other cells in the spreadsheet shift to replace the deleted cells.
Sheets("Test").Range("A1:C3").Delete
How can you remove the contents and the formatting of a cell or range in VBA with a single command, without affecting the rest of the worksheet?
Use the .Clear method.
Sheets("Test").Range("A1:C3").Clear
MSDN documentation here.

Blank Fields Copied as #N/A

I am working with an Excel 2003 VBA script that copies the content from worksheets in an external Excel file to worksheets in our own:
ThisWorkbook.Sheets(strTarget).UsedRange.Value = Workbooks(strFile).Sheets(strSource).UsedRange.Value
In a large number of blank cells, this function is posting #N/A instead of the blank values, which causes errors further down in the VBA code. This only occurs on some sheets, while others copy over just fine, sometimes by adding these values in extra rows and other times in extra columns. I have attempted to clear the error with IsNA to no avail:
If (IsNA(Workbooks(strFile).Sheets(strSource).UsedRange.Value)) Then
ThisWorkbook.Sheets(strTarget).UsedRange.Value = ""
Else: ThisWorkbook.Sheets(strTarget).UsedRange.Value = Workbooks(strFile).Sheets(strSource).UsedRange.Value
Is there a simple way to remove these #N/A values during the copy or even clear them after the fact?
Any other advise on how to handle this issue would also be greatly appreicated, as I'm not a VBA dev myself, but simply the lucky soul who got to pick this up when the only person with Excel and VBA experience left our team. Thanks in advance!
The problem comes when the ranges aren't the same size. You should resize appropriately:
With Workbooks(strFile).Sheets(strSource).UsedRange
ThisWorkbook.Sheets(strTarget).UsedRange.Resize(.Rows.count, .Columns.count).Value = .Value
End With
It's probably safer to clear the target sheet first using:
ThisWorkbook.Sheets(strTarget).UsedRange.ClearContents