I have code that runs through column E (Column C in example below) and searches for any change in data. It then inserts a blank row under that change and loops through the set of data (200-500 rows long).
I want a "copy and paste" feature of the last row of data, before the change, into the newly inserted row.
Before:
Column A
Column B
Column C
Column E
1
2
Sally
5
1
2
Sally
6
1
2
Sally
2
1
2
Chase
1
1
2
Chase
4
1
2
Ben
9
After:
Column A
Column B
Column C
Column E
1
2
Sally
5
1
2
Sally
6
1
2
Sally
2
2
Sally
1
2
Chase
1
1
2
Chase
4
2
Chase
1
2
Ben
9
2
Ben
The code I have has a loop:
Sub CleanUpPart2()
'Insert Rows by column F
'
Dim iRow As Integer, iCol As Integer
Dim oRng As Range
Set oRng = Range("f1")
iRow = oRng.Row
iCol = oRng.Column
Do
'
If Cells(iRow + 1, iCol) <> Cells(iRow, iCol) Then
Cells(iRow + 1, iCol).EntireRow.Insert Shift:=xlDown
iRow = iRow + 2
Else
iRow = iRow + 1
End If
'
Loop While Not Cells(iRow, iCol).Text = ""
'
Edited::
Try the updated code, please:
Sub testInsertRowCopyBefore()
Dim sh As Worksheet, lastRow As Long, i As Long
Set sh = ActiveSheet
lastRow = sh.Range("A" & Rows.count).End(xlUp).row
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
For i = lastRow + 1 To 3 Step -1
If sh.Range("C" & i).Value <> sh.Range("C" & i - 1).Value Then
sh.Range("C" & i).EntireRow.Insert xlUp
sh.Range("B" & i & ":C" & i).Value = sh.Range("B" & i - 1 & ":C" & i - 1).Value
End If
Next i
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
MsgBox "Ready..."
End Sub
The above code assumes that "Column A", "Column B" and "Column C" are the headers, which stay on the sheet first row.
Please, test it and send some feedback
Related
I have the following data:
Product GBP USD EUR CAD
----------------------------------------------------------
Adidas Shoe 8 30 25 25
Puma Shoe 7 40 30 25
How do I split the data into the following, in Excel 2020? Using a macro?
Product VALUE CURRENCYISO
----------------------------------------------------------
Adidas Shoe 8 GBP
Adidas Shoe 30 USD
Adidas Shoe 25 EUR
Adidas Shoe 25 CAD
Puma Shoe 7 GBP
Puma Shoe 40 USD
Puma Shoe 30 EUR
Puma Shoe 25 CAD
Not the cleanest one, but can help:
Dim MiMatriz As Variant
Dim i As Long, ZZ As Long
Dim MyRow As Long
MiMatriz = Range("A1").CurrentRegion.Value
'type row number where data is going to be pasted. HEaders will be one row over
MyRow = 10
Range("A" & MyRow - 1).Value = "Product"
Range("B" & MyRow - 1).Value = "Value"
Range("C" & MyRow - 1).Value = "Currency ISO"
'we start array at 2 because first index got headers
For i = 2 To UBound(MiMatriz) Step 1
For ZZ = 2 To 5 Step 1 'your range got 5 columns of data
Range("A" & MyRow).Value = MiMatriz(i, 1) 'product
Range("B" & MyRow).Value = MiMatriz(i, ZZ) 'value
Range("C" & MyRow).Value = MiMatriz(1, ZZ) 'header
MyRow = MyRow + 1
Next ZZ
Next i
Erase MiMatriz
I'm sure they're probably a better way to do it but I have one solution here:
Sub TransposeData()
Dim lastrow As Long, lastcolumn As Long, ws As Worksheet, irow As Long, icolumn As Long, lrowout As Long
Application.ScreenUpdating = False
Set ws = Sheets("Sheet1") 'your sheet name
lastrow = ws.Range("F" & Rows.Count).End(xlUp).Row 'Change "F" to product list
lastcolumn = ws.Cells(4, Columns.Count).End(xlToLeft).Column 'Change "4" to the row with the headers
For irow = 5 To lastrow 'what row number does the data start from (exluding headers)
For icolumn = 7 To lastcolumn 'what column number does the data start from (exluding product names)
lrowout = ws.Range("R" & Rows.Count).End(xlUp).Row 'Change "R" to column output will be in
ws.Range("R" & lrowout + 1).Value = ws.Range("F" & irow).Value 'Change "R" to where your product name will be
ws.Range("S" & lrowout + 1).Value = ws.Cells(irow, icolumn).Value 'Change "S" to where value will be
ws.Range("T" & lrowout + 1).Value = ws.Cells(4, icolumn).Value 'Change "T" to where currency will be
Next icolumn
Next irow
Application.ScreenUpdating = True
End Sub
I have to delete a row if the of the column values of Column C, Column D , Column E of the same row is zero.
for example.
ColumnA Column B ColumnC ColumnD ColumnE
row1- abc xyz 0 abs abx
row2- wqe tuy 0 0 0
row3 uhiu khj kjh khk 0
here I have to delete the row 2 only because values of all column c , D , E are zero
Please help
A reverse loop should do the job. Try the below:
Option Explicit
Public Sub DeleteRows()
Dim i As Long, count As Long, lastRow As Long
' Replace Sheet1 with your sheetname
With ThisWorkbook.Worksheets("Sheet2")
' Change C with your most consistent column letter
' (a column that has data always to make sure there's no possibility to miss the last row due to empty cells)
lastRow = .Cells(.Rows.count, "C").End(xlUp).Row
' We do a reverse loop to not screw up the index
For i = lastRow To 2 Step -1
If .Range("C" & i).Value = "0" And .Range("D" & i).Value = "0" And .Range("E" & i).Value = "0" Then
.Range("C" & i).EntireRow.Delete
count = count + 1
End If
Next i
End With
' Display some message
If count > 0 Then
MsgBox "Done!" & vbCrLf & "Deleted " & count & " row(s).", vbInformation + vbOKOnly, "Success"
Else
MsgBox "No matches found for deletion", vbInformation + vbOKOnly, "Success"
End If
End Sub
Try,
Sub test()
Dim vDB, vR()
Dim Ws As Worksheet, toWs As Worksheet
Dim i As Long, n As Long
Dim j As Integer
Set Ws = ActiveSheet
vDB = Ws.UsedRange
For i = 1 To UBound(vDB, 1)
If vDB(i, 3) = 0 And vDB(i, 4) = 0 And vDB(i, 5) = 0 Then
Else
n = n + 1
ReDim Preserve vR(1 To 5, 1 To n)
For j = 1 To 5
vR(j, n) = vDB(i, j)
Next j
End If
Next i
Set toWs = Sheets.Add '<~~ set your sheet
With toWs
.Cells.Clear
.Range("a1").Resize(n, 5) = WorksheetFunction.Transpose(vR)
End With
End Sub
Try this code, fast and easy code.
Sub deleterow()
Dim i As Integer
i = 2
LastR = Cells(Rows.Count, 1).End(xlUp).row
For i = LastR To 2 Step -1
If Cells(i, 3).value = "0" And Cells(i, 4).value = "0" And Cells(i, 5).value = "0" Then
Cells(i, 1).EntireRow.delete
End If
Next i
End Sub
I have a data set:
ID Name Amount BID BID 2
1 Jack 100 1
1 John 200 *Blank*
2 Tony 300 2
How can I lookup the ID and where the ID is matching look at the BID and find the relating ID value where the ID value has been populated for the same ID. I want the value to appear in BID 2 which is in column 5.
IE John should have a BID of 1 as his ID is 1 like Jacks ID.
I have tried
=vlookup(A2,A2:C5,3,FALSE)
I honestly don't know a way to do this through formula, but a VBA solution would look like this:
Sub FindTheValue()
Dim sht As Worksheet, lastrow As Long, i As Long, j As Long
Set sht = ThisWorkbook.Worksheets("Sheet1")
lastrow = sht.Cells(sht.Rows.Count, "C").End(xlUp).Row
For i = 1 To lastrow
If Range("Z" & i).Value = "" Then
For j = 1 To lastrow
If Range("C" & i).Value = Range("C" & j).Value And Range("Z" & j).Value <> "" Then
Range("AA" & i).Value = Range("Z" & j).Value
Exit For
End If
Next j
End If
Next i
End Sub
enter image description hereThere are 2 sheets, Sheet1 and Sheet2.
Sheet1 contain 10 columns and 5 rows with data including blank.
The requirement is to copy the data from Sheet 1 and to put in another sheet Sheet 2, wherein only populate the cell which is not blank.
I get the run time error 1004 - Application or object defined error.
The code snippet is:-
Set wsht1 = ThisWorkbook.Worksheets("Sheet1")
Set wsht2 = Sheets("Sheet2")
finalrow = wsht1.Cells(wsht1.Rows.Count, 1).End(xlUp).Row
For i = 1 To finalrow
If wsht1.Cells(i, 1).Value <> " " Then
Range(Cells(i, 2), Cells(i, 2)).Copy
Worksheets("Sheet2").Select
wsht2.Range(Cells(1, i)).PasteSpecial Paste:=xlPasteFormats
End If
Next i
Can u help me in sorting this out?
You cannot define a range like that:
wsht2.Range(Cells(1, i))
you might use:
wsht2.Cells(1, i).PasteSpecial Paste:=xlPasteFormats
BTW: with this code you won't find empty cells:
If wsht1.Cells(i, 1).Value <> " " Then
you should use:
If wsht1.Cells(i, 1).Value <> "" Then
(the difference is a missing space between the quotes)
if you want to copy the values only and to make it with a loop I'd do the following:
Sub copying()
Set wsht1 = ThisWorkbook.Worksheets("Sheet1")
Set wsht2 = Sheets("Sheet2")
finalrow = wsht1.Cells(wsht1.Rows.Count, 1).End(xlUp).Row
For i = 1 To finalrow
If wsht1.Cells(i, 1).Value <> "" Then
For j = 1 To 5
wsht2.Cells(i, j).Value = wsht1.Cells(i, j).Value
Next j
End If
Next i
End Sub
If you only have 5 cells with data in Sheet 1 and only want those 5 rows copying to Sheet 2 use the following, similar to Shai's answer above with an extra counter for the rows in Sheet 2.
Sub copying()
Set wsht1 = ThisWorkbook.Worksheets("Sheet1")
Set wsht2 = Sheets("Sheet2")
finalrow = wsht1.Cells(wsht1.Rows.Count, 1).End(xlUp).Row
k = 1
For i = 1 To finalrow
If wsht1.Cells(i, 1).Value <> "" Then
For j = 1 To 5
wsht2.Cells(k, j).Value = wsht1.Cells(i, j).Value
Next j
k = k + 1
End If
Next i
End Sub
EDIT
As per your comment if you want to dynamically change j replace For j = 1 To 5 with
For j = 1 To wsht1.Cells(i, Columns.Count).End(xlToLeft).Column
The code below will copy only values in Column A (non-empty cells) from Sheet 1 to Sheet2:
Dim j As Long
Set wsht1 = ThisWorkbook.Worksheets("Sheet1")
Set wsht2 = Sheets("Sheet2")
finalrow = wsht1.Cells(wsht1.Rows.Count, 1).End(xlUp).Row
j = 1
For i = 1 To finalrow
With wsht1
' if you compare to empty string, you need to remove the space inside the quotes
If .Cells(i, 1).Value <> "" And .Cells(i, 1).Value <> " " Then
.Cells(i, 1).Copy ' since you are copying a single cell, there's no need to use a Range
wsht2.Range("A" & j).PasteSpecial Paste:=xlPasteValues, Paste:=xlPasteFormats
j = j + 1
End If
End With
Next i
Hi I want to make the data in data base below
ID Name Card No
1 A 3
2
2 B 1
3 C 1
2
to be like
ID Name Card No
1 A 3
1 A 2
2 B 1
3 C 1
3 C 2
Using excel macros.. please help thanks :D
Update, my data is like this and the debugger only copy A & B column
Col->A B C D
Row
7 ID Name Address Card No
8 1 A 3
9 2
10 2 B X road 1
11 3 C Y road 1
12 2
How to fix this? please help :( and it started in row 7
Simple but below code will do the work:
Note: Below code assumes your "Column C" do not have any empty cells. If you do please add comment so I can update.
Sub copyPaste()
Dim lastRow As Long
lastRow = Range("C" & Rows.Count).End(xlUp).Row '<- Column that there is no empty cells
For i = 1 To lastRow
If Range("A" & i).Value = "" Then
Range("A" & i).Value = Range("A" & i).Offset(-1, 0).Value
End If
If Range("B" & i).Value = "" Then
Range("B" & i).Value = Range("B" & i).Offset(-1, 0).Value
End If
Next
End Sub
UPDATE: Below code will do the same job for all columns in the workbook. Code assumes that in the first row of worksheet you have headers and it is using this information to calculate how many iterations to do for columns.
Sub copyPaste()
Dim lastRow As Long
Dim lastCol As Long
lastRow = Range("C" & Rows.Count).End(xlUp).Row '<- Column that there is no empty cells
lastCol = Cells(1, Columns.Count).End(xlToLeft).Column
For j = 1 To lastCol
For i = 1 To lastRow
If Cells(i, j).Value = "" Then
Cells(i, j).Value = Cells(i, j).Offset(-1, 0).Value
End If
Next i
Next j
End Sub