all.
I found in a previous post, a solution that solved most of my problem. I just need some help expanding on it a little. The following macro was posted (and I have amended it for my application) to add a row after the text "CC Total" appeared. I need this macro to add a row after "CC Total" and the text "Sum Total". how can I add this second criteria to the command?
Dim Col As Variant
Dim BlankRows As Long
Dim LastRow As Long
Dim R As Long
Dim StartRow As Long
Col = "A"
StartRow = 1
BlankRows = 1
LastRow = Cells(Rows.Count, Col).End(xlUp).Row
Application.ScreenUpdating = False
With ActiveSheet
For R = LastRow To StartRow + 1 Step -1
If .Cells(R, Col) = "CC Total" Then
.Cells(R + 1, Col).EntireRow.Insert Shift:=xlUp
End If
Next R
End With
Application.ScreenUpdating = True
The following line is key here:
If .Cells(R, Col) = "CC Total" Then
To add extra criteria, you can either do
If .Cells(R, Col) = "CC Total" Or .Cells(R, Col) = "SUM Total" Then
or, to make it more accessible for more criteria:
If .Cells(R, Col) = "CC Total" Then
....
ElseIf .Cells(R, Col) = "SUM Total" Then
....
etc
End if
Related
I have a worksheet (first row headers) where the 3 last columns are as follows: Component 1(column AW), Component 2 (AX) and Number of Components (AY). Number of components value is either 1 or 2. If "Number of components" is 2 then I would like to copy the entire row and paste it twice to the last row of the same worksheet.
If "Number of components"is 1 then I would only like to copy it once.
Earlier there is also a column M that contains the article number and for each copied row I would like the value in column M to be replaced by the value from the corresponding row of column Component 1 / Component 2.
For example in this row the article number in column M is x. As "Number of components" is 2, I would like this whole row to be pasted twice to the last row of the worksheet. By the first time it is pasted, I need column M value to be replaced by 205334 (Component 1 value) and when it is pasted second time I need column M value to be replaced by 96423 (Component 2 value). Is it possible to loop this way? The worksheet has more than a 1000 rows.
I have written the following code to do the copy/paste part, however I'm having trouble to replace the value from Column M with the value from Columns Component 1 and 2.
Dim lastcol As Range
Dim lColumn As Long
Dim lRow As Long
Dim i As Long
lColumn = Cells(1, Columns.Count).End(xlToLeft).Column
Set lastcol = Cells(2, lColumn)
For i = 2 To Rows.Count
If lastcol.Value = 2 Then
Cells(i, lColumn).EntireRow.Select
Selection.Copy Sheets("Filtersets Database (2)").Range("A" & Rows.Count).End(xlUp).Offset(1)
Cells(i, lColumn).EntireRow.Select
Selection.Copy Sheets("Filtersets Database (2)").Range("A" & Rows.Count).End(xlUp).Offset(1)
Cells(i, lColumn).EntireRow.Select
ElseIf lastcol.Value = 1 Then
Cells(i, lColumn).EntireRow.Select
Selection.Copy Sheets("Filtersets Database (2)").Range("A" & Rows.Count).End(xlUp).Offset(1)
End If
Next i
Thank you in advance!
This should do what you want.
Option Explicit
Sub CopySomeThings()
Dim ws As Worksheet
Dim rngDst As Range
Dim lastcol As Long
Dim lastrow As Long
Dim i As Long
Set ws = ActiveSheet ' Sheets("Filtersets Database (2)")
lastrow = ws.Range("A" & Rows.Count).End(xlUp).Row
lastcol = ws.Cells(1, Columns.Count).End(xlToLeft).Column
Set rngDst = ws.Range("A" & lastrow + 1)
For i = 2 To lastrow
With ws
If .Cells(i, lastcol).Value = 2 Then
.Rows(i).Copy rngDst
.Cells(rngDst.Row, "M").Value = .Cells(i, lastcol - 2)
Set rngDst = rngDst.Offset(1)
.Rows(i).Copy rngDst
.Cells(rngDst.Row, "M").Value = .Cells(i, lastcol - 1)
Set rngDst = rngDst.Offset(1)
ElseIf .Cells(i, lastcol) = 1 Then
.Rows(i).Copy rngDst
.Cells(rngDst.Row, "M").Value = .Cells(i, lastcol - 2)
Set rngDst = rngDst.Offset(1)
End If
End With
Next i
End Sub
I'm trying to merge cells in excel using VBA based on the column value. For instance, on row one, wherever the month is the same, merge those cells. I've tried the following code :
Sub Main()
Dim j As Long
For j = 1 To 13
If StrComp(Cells(1, j), Cells(1, j + 1), vbTextCompare) Then
Range(Cells(1, j), Cells(1, j + 1)).Merge
End If
Next j
End Sub
Here, I'm keeping the row fixed as the first row and iterating over the columns and checking if the next cell value is same as the current value. However, in the output it's merging incorrect cells. What am I missing here?
This is easier to understand.
Application.DisplayAlerts = False
With ThisWorkbook.Sheets("Sheet1")
For i = 13 To 2 Step -1 'Loop from the last cell, and stop at the second column
If .Cells(1, i).Value = .Cells(1, i).Offset(, -1).Value Then
.Range(.Cells(1, i), .Cells(1, i).Offset(, -1)).Merge
End If
Next i
End With
Application.DisplayAlerts = True
Should work like this …
Option Explicit
Public Sub MergeSameValuesInRow()
Const iRow As Long = 1 'the row number
Const FirstColumn As Long = 1 'first column with data in iRow
Dim ws As Worksheet
Set ws = ThisWorkbook.Worksheets("Sheet1") 'define your worksheet
Dim LastColumn As Long 'find last used column in iRow
LastColumn = ws.Cells(iRow, ws.Columns.Count).End(xlToLeft).Column
Dim StartCell As Range 'remember the start cell (first occurence of a new value)
Set StartCell = ws.Cells(iRow, FirstColumn)
Dim iCol As Long
For iCol = FirstColumn + 1 To LastColumn + 1 'loop through columns in iRow
If ws.Cells(iRow, iCol).Value <> StartCell.Value Then 'if value changed …
Application.DisplayAlerts = False 'hide merging messages
ws.Range(StartCell, ws.Cells(iRow, iCol - 1)).Merge 'merge from start cell until one before value change
Application.DisplayAlerts = True
Set StartCell = ws.Cells(iRow, iCol) 'set start cell to the next value
End If
Next iCol
End Sub
It will change this …
into this …
I have a table consisting of strings and numbers. Row one contains the heading and row two contains the unit type (percent and dollars). I would like to round the numbers in the column based on the heading in row two.
At the moment I am selecting the columns individually. Is there a way to round the column based on the heading in row two?
Sub Round()
Dim Lastrow As Long
Lastrow = ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row 'Determine
last row
For Each cell In ActiveSheet.Range("R3:R" & Lastrow)
cell.Value = WorksheetFunction.Round(cell.Value, 2) 'Round dollars to 2 places
Next cell
For Each cell In ActiveSheet.Range("AB3:AB" & Lastrow)
cell.Value = WorksheetFunction.Round(cell.Value, 2)
Next cell
For Each cell In ActiveSheet.Range("Q3:Q" & Lastrow)
cell.Value = WorksheetFunction.Round(cell.Value, 1) 'Round percentages to 1 places
Next cell
....
End Sub
You were close enough, just needed a bit from both of those tries together. Please see if the below helps, I've added an alternative using arrays as well (if you have lots of data, it will be much faster):
Sub RoundRanges()
Dim ws As Worksheet: Set ws = ActiveSheet 'better use something like: ActiveWorkbook.Sheets("Sheet name here")
Dim lRow As Long: lRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row 'get last row
Dim lCol As Long: lCol = ws.Cells(2, ws.Columns.Count).End(xlToLeft).Column 'get last column
Dim R As Long, C As Long
For C = 1 To lCol 'iterate through each column
Select Case ws.Cells(2, C) 'get the text of the cell 2...
Case "Percent"
For R = 3 To lRow 'iterate through each row
ws.Cells(R, C) = WorksheetFunction.Round(ws.Cells(R, C).Value, 1) 'apply the desired calculation
Next R
Case "Dollars"
For R = 3 To lRow 'iterate through each row
ws.Cells(R, C) = WorksheetFunction.Round(ws.Cells(R, C).Value, 2) 'apply the desired calculation
Next R
End Select
Next C
'ALTERNATIVE:
'Dim arrData As Variant: arrData = ws.Range(ws.Cells(1, 1), ws.Cells(lRow, lCol))
'For R = LBound(arrData) + 2 To UBound(arrData) 'skip first 2 rows
' For C = LBound(arrData, 2) To UBound(arrData, 2)
' If arrData(2, C) = "Percent" Then
' arrData(R, C) = Round(arrData(R, C), 1)
' ElseIf arrData(2, C) = "Dollars" Then
' arrData(R, C) = Round(arrData(R, C), 2)
' End If
' Next C
'Next R
'ws.Range(ws.Cells(1, 1), ws.Cells(lRow, lCol)) = arrData
End Sub
I found a macro at Insert copied row based on cell value.
The macro inserts a blank row above any row that has a value of "0" in column E.
Instead of the empty row appearing above, I need it to appear below.
Sub BlankLine()
Dim Col As Variant
Dim BlankRows As Long
Dim LastRow As Long
Dim R As Long
Dim StartRow As Long
Col = "E"
StartRow = 1
BlankRows = 1
LastRow = Cells(Rows.Count, Col).End(xlUp).Row
Application.ScreenUpdating = False
With ActiveSheet
For R = LastRow To StartRow + 1 Step -1
If .Cells(R, Col) = "0" Then
.Cells(R, Col).EntireRow.Insert Shift:=xlDown
End If
Next R
End With
Application.ScreenUpdating = True
End Sub
Edit the following line from:
.Cells(R, Col).EntireRow.Insert Shift:=xlDown
to:
.Cells(R+1, Col).EntireRow.Insert Shift:=xlDown
Pertinent to you question, modify a single line in original code: instead of:
.Cells(R, Col).EntireRow.Insert Shift:=xlDown
use this one:
.Cells(R, Col).EntireRow.Insert Shift:=xlUp
Rgds,
Below is my initial excel sheet
Below is what my output should be
My approach:
Get the first value from column A
Get the Offset(0,1) of column A (results in B1)
Get the number of records from column B till the first empty cell (results in 3)
Copy value of A1 to the cells (number of records - 1) below A1
Loop for all the 3 values in column A
I have implemented till I get the addresses of the cells with values in column A. Below is my code.
Dim currentRow As Integer
Dim element As Variant
Dim totalRows As String
Dim offsetrow As String
Dim offsetcell As Variant
totalRows = Range("A" & ActiveSheet.Rows.Count).End(xlUp).row
MsgBox (totalRows)
For currentRow = 1 To totalRows
If (IsEmpty(Cells(currentRow, 1).Value)) Then
Else
Cells(currentRow, 1).Select
offsetcell = ActiveCell.Offset(0, 1).Address
'Do the rest
End If
Next
End Sub
Any help is appreciated
You could optimize this by using arrays but this is the basic idea:
Dim i As Long
For i = 1 To ActiveSheet.UsedRange.Rows.Count
If Cells(i + 1, 1).Value = "" And Cells(i, 1).Value <> "" And Cells(i + 1, 2).Value <> "" Then
Cells(i + 1, 1).Value = Cells(i, 1).Value
End If
Next
This is my work around to achieve the desired output. This might not be the most efficient way of doing this. Anyways it works :).
Sub Button1_Click()
Dim currentRow As Integer
Dim totalrows As String
Dim offsetrow As Integer
Dim i As Integer
Dim row As Integer
totalrows = Range("A" & ActiveSheet.Rows.Count).End(xlUp).row
For currentRow = 1 To totalrows
If (IsEmpty(Cells(currentRow, 1).Value)) Then
Else
Cells(currentRow, 1).Select
offsetrow = ActiveCell.Offset(0, 1).row
row = offsetrow
i = 1
Do While (Cells(row, 2).Value <> "")
i = i + 1
row = row + 1
Cells((row - 1), 1).Value = Cells(currentRow, 1).Value
Loop
End If
Next
End Sub
Hope this helps to anyone come across the same issue.
Just an alternate solution:
To accurately determine the number of rows for your range:
Lastrow = Cells.Find("*", [A1], , , xlByRows, xlPrevious).Row
An alternate method for your problem:
Manual
select range in column a,
go to blanks (ctrl+g),
press =, up arrow. Now hold ctrl+Enter
Copy/paste as values...
Here's a code snippet to do this:
Lastrow = Cells.Find("*", [A1], , , xlByRows, xlPrevious).Row
Range("A1:A" & Lastrow).Select
Selection.SpecialCells(xlCellTypeBlanks).Select
Selection.FormulaR1C1 = "=R[-1]C"
Range("A1:A" & Lastrow).Value = Range("A1:A" & Lastrow).Value
Finish off by looping through column B and deleting A values where B is blank.
Hope this helps.