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
Related
I am new to VBA and need some help - if anyone could help me.
I need to copy a range, say A1:F1, and paste it in the row directly under, so A2:F2, on an automatic loop.
The top-most row contains data from a different sheet, therefore I will be copying the formulas of that row which links to the secondary sheet. I would like the loop to end once the other sheet has been exhausted and the macro hits an empty row from the sheet.
Any ideas?
You can use a do until loop to find the length of your list. Then u can use this index to use range, push down the data and push your new data on the now empty top row.
I understand, that u want to push data always in the first row and everything else down, so you add always one new row.
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.
I need to move the results of formula placed in H8-H22 to colomn C8-C22. How would a piece of VBA code look like to achieve this?
If possible can I do the same thing for multiple sheets at once?
Thanks for any help!
/Rasmus
range("C8:C22").value = range("H8:H22").value
To do the multiple sheets you can either write a simple loop, or else try recording a macro where you select multiple sheets and then copy your "H" range and paste special as values into your "C" range (but all while you have multiple sheets selected)
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.
I am new to Excel VBA and need some help writing a macro.
From Worksheet1 of Workbook1 I need to copy each column of range D1:Z100, one at a time, and pasteValue it to cells B1:B100.
This triggers a calculation in Worksheet2 of Workbook1. Here I need to copy cells A1:B200 into a new workbook.
This new workbook is to be renamed with the text string in Worksheet1, cell B1. The new workbook is to be saved into the same folder as Workbook1.
The loop is then to continue with the next column from Worksheet1, and continue until all columns in the range have been treated this way.
I have used two days searching the net to find an answer without any luck....
Start the way most of us did:
Record new macro.
Manually perform all the steps you want to automate.
Stop recording.
Go to the Developer tab and inspect the auto-generated code.
Write your own VBA based on what you learn in Step 4.
You will have to figure out a few things on your own, or come back here and ask specific questions.