VBA Excel - Transform Column value To another Column along with inserting blank row - vba

I want to tranform the values.
Suppose in A1:A20 there are 20 values and in B1:B20 also 20 values.
I want to copy values of column B in such manner that outcome should be:
Cell A1 value is A1, cell A2 value is B1, cell A3 value is A2, cell A4 value is B1.
Likewise in cell A40 value is B20.
(for more clarity i have attached image)
So here first step is inserting blanck cells alternatively in column A
and than copy value of column B in that blank cells.
For more clarity Please find the image
.

Always post what you have tried first !
Sub test()
Application.ScreenUpdating = False
Dim i As Long
For i = 2 To 20 Step 2
Sheets("Sheet1").Range("A" & i).EntireRow.Insert
Next i
Sheets("Sheet1").Range("B1:B40").copy
Sheets("Sheet1").Range("A2").PasteSpecial Paste:=xlPasteAll, SkipBlanks:=True
Application.ScreenUpdating = True
End Sub

Maybe this is not exactly what you are looking for but an alternative solution is to use a regular formula like below.
=IFERROR(INDEX($A$2:$B$9,ROUNDDOWN(ROW()/2,0),ISODD(ROW())+1),"")
But this won't give the color you are looking for. Just throw out an alternative method for your reference.

Related

finding sum of squares of column using for each

I am a newbie. I want to sum the squares of the numbers in the active column. I am getting an error 'object doesn't support this method'. Here is my code
Sub sum_squares()
Dim total As Integer
Dim c As Range
Dim d As Range
'set d equal to column from active cell down to last non-empty'
d = Range(ActiveCell, ActiveCell.endxldown)
total = 0
For Each c In d
total = total + c.Value ^ 2
Next c
End Sub
Appreciate the help.
Thanks
As has been pointed out you've got the syntax of xlDown incorrect.
You should also start at the bottom and move up - xlDown may not find the last cell.
E.g.
With a value in cell A1:A3 and A1 as the ActiveCell it will correctly return A3.
Same scenario but with A4 left blank and a value in A5 still returns A3.
Same scenario with A1 left blank it returns A2.
This will return a reference from the ActiveCell to the last cell containing a value in that column.
Note that if the ActiveCell is lower down than the last cell containing data you'll get a reference reflecting that.
Set d = Range(ActiveCell, Cells(Rows.Count, ActiveCell.Column).End(xlUp))
A Range can be made up of one or more cell references:
Range("A1") and Range("A1,A3,C5") reference individual cells.
Range("A1:C5") and Range("A1", "C5") reference all cells between the first and last address.
A Cell is a single cell reference that uses row and columns identifiers.
Cells("A1") will return an error as it's a full address.
Cells(1,"A") will return A1 (row 1, column A)
Cells(1,1) will also return A1 (row 1, column 1)
The code above is using two cell addresses to reference all cells between the two.
Range(ActiveCell,....) is the reference to the first cell.
Cells(Rows.Count, ActiveCell.Column) is the reference to the second cell using row numbers and column numbers.
If the ActiveCell is in column B then this is the same as writing Cells(1048573,2).
The End(xlUp) then goes from that cell back up to the first one containing data.
There is a syntax error in your code - .endxldown and add Set before assigning range.
Correct it to -
Set d = Range(ActiveCell, ActiveCell.End(xlDown)

Copy rows based on cell value and paste on a new sheet

Check This
I need a help. I want to copy whole cell from A sheet name "Components" only if value in Column C is > 0 to a new Sheet name "Load list"
Can someone please give me the macro code for this?
on your new sheet you can add this condition the cell or range of cells:
=IF(Components!C5>0,Components!A5)
where C5 has thevalue to compare, and A5 has the value copy if the condition happens.
Right in my swing!
The formula given by #sweetkaos will work fine, in case you want to replicate the data as it is with blanks where data is not found.
I will imagine a slightly more complicated situation. I am assuming you want just one line in the next format as is shown in your image.
Also conveniently assuming the following:
a. both sheets have fixed start points for the lists
b. 2 column lists - to be copied and pasted, with second column having value
c. Continuous, without break source list
d. basic knowledge of vba, so you can restructure the code
Here is the code. Do try to understand it line by line. Happy Excelling!
Sub populateLoadList()
'declaring range type variables
Dim rngStartFirstList As Range, rngStartLoadList As Range
'setting values to the range variables
'you must change the names of the sheets and A1 to the correct starts of your two lists
Set rngStartFirstList = Worksheets("Name_of_your_fist_sheet").Range("A1")
Set rngStartLoadList = Worksheets("Name_of_your_second_sheet").Range("A1")
Do While rngStartFirstList.Value <> ""
If rngStartFirstList.Offset(1, 0).Value < 0 Then
Range(rngStartFirstList, rngStartFirstList.Offset(0, 1)).Copy
rngStartLoadList.PasteSpecial xlPasteValues
Application.CutCopyMode = False
Set rngStartLoadList = rngStartLoadList.Offset(1, 0)
End If
Set rngStartFirstList = rngStartFirstList.Offset(1, 0)
Loop
End Sub
Basically what i want is ... if Value on C is >0 i want whole column 10 copied to that new sheet .... not only that cell

Clone columns, but formulas need to be shifted too - with VBA

I have a cell (A1) which conatins a number, ie. A1 = 4. This means, that I have to clone 4 times a column from a sheet.
It would be very easy, but the trick is, that the cloned columns cells may contain formulas too, which should be shifted by the number of the clone. For the better understanding here is an example:
If the columns contains =A1, the first clone should contain =B1, the second one =C1 and so on.
The copy script, without shifting:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$A$1" Then
For i = 0 To Target
ActiveWorkbook.Sheets("Sheet1").Columns(3).Copy Destination:=Sheets("Sheet1").Columns(i) 'Columns(3) is the column that I am cloning
Next i
End If
End Sub
How should I solve this problem?
If you have a formula (=A1 for example) then the column is automatically shifted, if you want a different behaviour, use $ in front of the column or row:
If you write =$A1, then the column will always stay the same but the row can change
If you write =A$1, the column will change but the row will stay the same
If you write = $A$1 both the column and row will stay the same
Here's my example: In A1 i use the formula =A2. I entered 5 into A2 so A1 shows 5 as well. Now i ran this code:
Dim i As Integer
For i = 1 To 4 Step 1
Cells(1, 1).EntireColumn.Copy Destination:=Cells(1, i).EntireColumn
Next i
Now B1 has the formula =B2, C1 has =C2 and D1 has =D2
If you wouldn't want the formula to change, enter =$A$2 into A1
Hope this helps somehow

Weird behaivour getting value from merged cell

I've come across this behaivour in Excel. Given these cells:
A B C D
|---------|---------|---------|---------|
1 | merged cell text | foo | bar |
|---------|---------|---------|---------|
2 | | | | |
|---------|---------|---------|---------|
When Cell C1 is selected (foo), the following VBA statement returns an empty string:
Debug.Print Selection.Offset(0, -1).Range("A1").Value
This next VBA however returns the expected value (merged cell text):
Selection.Offset(0, -1).Select
Debug.Print Selection.Range("A1").Value
I can't for the life of my figure out why - surely as Offset returns a Range and Selection is also a Range, it's just breaking down the same behaivour over multiple lines, and getting different results. The behaivour does not occur when there are no merged cells.
Can anyone explain what's going on here?
This should be the reason:
Debug.Print Selection.Offset(0, -1).Range("A1").Value
Your current selection is C1, offset -1 column gives you B1. You are trying to print A1's value while selecting B1, thus gives nothing. Merged cell does not automatically cause both A1 and B1 being selected.
(you can test by printing B1's value instead). However:
Selection.Offset(0, -1).Select
Debug.Print Selection.Range("A1").Value
selects the range B1 first, which when merged, cause it to select both A1 and B1 on a application level, thus able to print A1's value
Hope that's clear to you
if you want to read the value in a1 from anywhere of the mergedcell,
Debug.Print Range("anyCellFormMergedRange").cells(1,1).Value 'wich gets the value from the first cell of a bigger range (here A1:B1)
or
Debug.Print Range("anyCellFormMergedRange").MergeArea(1, 1).value 'wich gets the value from the first cell of a MERGED range (here A1:B1)
'for a small mergedcell like yours, a simple .mergearea(1) works too
you can change the range to anything you need (selection, .offset(x,y), activecell ...) wich are also ranges.
if you want to use the whole mergedCells (A1:B1), without using .select :
(from C1)
Debug.Print selection.offset(0,-1).MergeArea.address
Also remember that comment, validation data, are also contained in the first cell of the range (A1 here).

Clear Contents of a Column

How would I clear the contents of a column from cell A3 to cell __ where __ represents the last entry in the column (assuming there are no empty spaces between entries).
Thanks for the help.
range("A3", Range("A" & Columns("A").SpecialCells(xlCellTypeLastCell).Row)).Delete
That will delete A3 through the last cell in column A, regardless of any blanks in the column.
range("A3", range("A3").End(xlDown)).Delete
That will delete from A3 down to the first blank cell after A3 in column A.
EDIT: Fixed the first code snippet so it only deletes cells in column A.
Range("A3", Range("A3").End(xlDown)).Clear
Using .Delete will actually delete the cells, shifting up any cells that might appear after this list (separated by a blank cell). If you just want to clear the contents, .Clear is a good way to go.
I would use a vbNullString, because it's slightly faster and works efficently on huge amount of data worksheets.
Paste 'nothing' from A3 to the first blank cell in column A:
Range(Cells(1,3), Cells(Range("A3").End(xlDown).Row,1)).Value = vbNullString
Paste 'nothing' from A3 to the last cell in column A:
Range(Cells(1,3), Cells(Range("A3").SpecialCells(xlTypeLastCell),1)).Value = vbNullString
I have had good results with this:
Set tbl = ActiveSheet.ListObjects("Table_Name")
Count = tbl.DataBodyRange.Rows.Count
Range("AC2:AC" + CStr(Count)).Select
Selection.ClearContents