excel move result of formula to another colomn - 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)

Related

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.

Find column that meet a criteria and copy other cells in that row to a new sheet

I'm trying to create a Macro that will find text in a column (D) and if a criteria is met, then copies the row into to another sheet. The Criteria I am looking for will be not be the only text located in the cell. For Example the criteria could be "Prodigy" but the Cell could contain "Prodigy, Prodigy Tubing Set".
I have a Sheet called "Sheet1" which has all the raw data with headers. The column that has the values to meet the criteria is "D".
I have other sheets where the data will be copied to. For example one of the Sheets will be titled "Prodigy", another will be "Prodigy Tubing Set". There will up to 20 other sheets.
Macro: Given row X copy specific cells from that row to a new sheet
I tried using the code the person in the link above built but it would only work if my criteria was the only thing in the cell.
A picture of what my spreadsheet will look like is below.
I am pretty new at this and would appreciate any possible assistance. Hope this makes sense.
I think the function you are looking for is InStr.
The example you posted has this: If cell.Value = "Card" Then
Instead, you will want something like this: If InStr(cell.Value, "Prodigy") > 0 Then
Of course, you will probably replace the literal "Prodigy" with some cell value or variable.

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

Lookup function in multiple sheets data

I have multiple sheets of data and I want to make it in one sheet (All of them are in the same workbook). Link to the excel file.
I tried to use Hlookup function in excel file, something like below:
=HLOOKUP("University",Sheet1!$A$1:$G$2, 2, FALSE).
But, since I have more than 100 sheets of data, I want to find a way to drag the function and auto generate the function below the 2nd row. I have tried to use indirect function by setting a reference column in front as below but cannot deal with it.
=HLOOKUP("University", 'INDIRECT(A3)'!$A$1:$G$2, 2, FALSE)
My next option is VB code. But, I am new to VB. Anybody can help on it?
Place your individual sheet names in column H of the Summary sheet and the row number in column I (as helper columns) and write this formula in cell A2 of the summary sheet.
=IFERROR(HLOOKUP(A$1,INDIRECT($H2&"!A1:G"&$I2),$I2,0),)
and drag to column F and down for as many sheet rows combos you have. I used 10 rows but you can obviously make it longer or shorter as neeed.
When you are done you can filter on 0 in column A and remove any lines with no data.
If your sheet names have spaces in them, you'll need to adjust the INDIRECT formula to this:
INDIRECT("'"&$H2&"'!A1:G"&$I2)
best way would be "defined names" + INDIRECT + HLOOKUP (or LOOKUP) like:
defined names
name: SList
formula: =MID(TRANSPOSE(GET.WORKBOOK(1))&T(NOW()),FIND("]",TRANSPOSE(GET.WORKBOOK(1))&T(NOW()))+1,255)
formula in cells: (this in A2 then simply autofill to G2 and thenn everything down) (you'll get a row with 0's between the sheets, which can be filtered out or deleted later (copy/paste values))
=IFERROR(HLOOKUP(A$1,INDIRECT("'"&INDEX(SList,COUNTIF($A$1:$A1,0)+2)&"'!$A:$G"),$H2,0),"")
Set H2 to 2 and for H3: (autofill down from H3)
=MAX(($H2+1)*($A2>0),2)
works perfectly for me LINK
No manual typing of sheetnames or something like that (only Column H:H as helper). Youll get rows's with 0's every time a new sheet is selected which can be filtered out. (or if you copy/paste values also can be deleted)
the +2 at ...st,COUNTIF($A$1:$A1,0)+2)&... simply tells to start with sheet 2 (if summary is the first). You may change it to +1 if you want to lookup starting with the first sheet.
Assuming you already have all 100+ sheet names typed out in column A, this will work whether or not you have spaces in the sheet names:
=HLOOKUP("University", OFFSET(INDIRECT(ADDRESS(1,1,1,1,A2)),0,0,2,7),2,FALSE)

Excel VBA loop through columns in range and copy columns

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.