How to duplicate a row based on column or cell value - vba

I am trying to duplicate rows in excel using VBA and merge columns into one.
The VBA code below hide some columns. i need help edit my code to show all columns(copy col A through col Q).
This is how the original data looks like
This is how i would like to end up to
This is how i get using the code listed below (problem: doesnt show or copy col. B to Col P)
I would like to show all columns between A and Q. the code below hides all columns except the first and merged one(Col A and merged col on col. B).
Sub SortMacro()
Dim SourceSheet As Worksheet
Dim OutSheet As Worksheet
Set SourceSheet = ActiveSheet
Set OutSheet = Sheets.Add
With SourceSheet
Out_i = 1
For r = 1 To .Cells(Rows.Count, 1).End(xlUp).Row
For i = 17 To 20 'or For each i in Array(17,18,20)
OutSheet.Cells(Out_i, 1) = .Cells(r, 1)
OutSheet.Cells(Out_i, 2) = .Cells(r, i)
Out_i = Out_i + 1
Next
Next
End With
End Sub
Thanks!

This is my interpretation of what you need. I've added a loop to copy columns A:P into each new row
Sub SortMacro()
Dim SourceSheet As Worksheet
Dim OutSheet As Worksheet
Set SourceSheet = ActiveSheet
Set OutSheet = Sheets.Add
With SourceSheet
Out_i = 1
For r = 1 To .Cells(Rows.Count, 1).End(xlUp).Row
' Create a new row for each column entry in Q:T
For i = 17 To 20
' Check the cell isn't empty before creating a new row
If (.Cells(r, i).Value <> "") Then
' Copy columns A:P
For j = 1 To 16
OutSheet.Cells(Out_i, j) = .Cells(r, j)
Next j
' Copy the current column from Q:T
OutSheet.Cells(Out_i, 17) = .Cells(r, i)
Out_i = Out_i + 1
End If
Next i
Next r
End With
End Sub

Related

VBA: How can i select the cell in a row which matches a variable's value?

I have 2 sheets. Sheet1 has 2 rows: column names and values.
Sheet 2 is a master sheet with all the possible column names in. I need to copy the values from sheet 1 into their appropriate column.
I think i can do this via a match function, and so far i have this:
Sub dynamic_paste()
Dim Columnname As String
Dim inputvalue As String
Dim starter As Integer
Dim i As Integer
starter = 0
For i = 1 To 4
'replace 4 with rangeused.rows.count?
Sheets("sheet1").Select
Range("a1").Select
ActiveCell.Offset(0, starter).Select
Columnname = ActiveCell
'sets columnname variable
ActiveCell.Offset(1, 0).Select
inputvalue = ActiveCell
'sets inputname variable
Sheets("sheet2").Select
'**Cells(0, WorksheetFunction.Match(Columnname, Rows(1), 0)).Select**
Range("a1").Offset(1, starter).Value = inputvalue
'inputs variable in the next cell along
starter = starter + 1
Next
End Sub
I need to find out how to use my columnname variable as the matching value, and then offset down to the first row that is empty - then change the value of that cell to the variable called inputvalue.
For extra points: I need to make sure the code doesnt break if they dont find a matching value, and if possible put any values that dont match into the end of the row?
What about this:
Dim LR As Long, X As Long, LC As Long, COL As Long
Dim RNG As Range, CL As Range
Option Explicit
Sub Test()
LR = Sheets(2).Cells.SpecialCells(xlCellTypeLastCell).Row 'Get last used row in your sheet
LC = Sheets(2).Cells(1, Sheets(2).Columns.Count).End(xlToLeft).Column 'Get last used column in your sheet
Set RNG = Sheets(2).Range(Sheets(2).Cells(1, 1), Sheets(2).Cells(1, LC))
'Loop through all the columns on your sheet with values
For X = 1 To Sheets(1).Cells(1, Sheets(1).Columns.Count).End(xlToLeft).Column
Set CL = RNG.Find(Sheets(1).Cells(1, X).Value, lookat:=xlWhole)
If Not CL Is Nothing Then
COL = CL.Column
Sheets(2).Cells(LR + 1, COL).Value = Sheets(1).Cells(2, X).Value 'Get the value on LR offset by 1
Else
Sheets(2).Cells(1, Sheets(2).Cells(1, Sheets(2).Columns.Count).End(xlToLeft).Column).Value = Sheets(1).Cells(1, X).Value
Sheets(2).Cells(LR + 1, Sheets(2).Cells(1, Sheets(2).Columns.Count).End(xlToLeft).Column).Value = Sheets(1).Cells(2, X).Value
End If
Next X
End Sub
This way you will avoid using select. Which is very recommandable!
This is Sheet1:
This is Sheet2:
This is the code:
Option Explicit
Sub DynamicPaste()
Dim col As Long
Dim wks1 As Worksheet: Set wks1 = Worksheets(1)
Dim wks2 As Worksheet: Set wks2 = Worksheets(2)
For col = 1 To 3
Dim currentRow As Long
currentRow = WorksheetFunction.Match(wks2.Cells(1, col), wks1.Columns(1))
wks2.Cells(2, col) = wks1.Cells(currentRow, 2)
Next col
End Sub
This is Sheet2 after the code:
This is a must-read - How to avoid using Select in Excel VBA

Moving data from one sheet to multiple sheets - vba

I have some code that creates worksheets based on a cell value in a column and then I have the below code which will scan the same column and move the entire row of that sheet to the matching sheet name.
Sub CopyRowData()
'Declare variables
Dim x As Integer
Dim y As Integer
Dim i As Integer
Dim shSource As Worksheet
Dim shTarget1 As Worksheet
Dim shTarget2 As Worksheet
Dim shTarget3 As Worksheet
Dim shTarget4 As Worksheet
Dim shTarget5 As Worksheet
Dim shTarget6 As Worksheet
'Assign string values to variables
Set shSource = ThisWorkbook.Sheets("1")
Set shTarget1 = ThisWorkbook.Sheets("2")
Set shTarget2 = ThisWorkbook.Sheets("3")
Set shTarget3 = ThisWorkbook.Sheets("4")
Set shTarget4 = ThisWorkbook.Sheets("5")
Set shTarget5 = ThisWorkbook.Sheets("6")
Set shTarget6 = ThisWorkbook.Sheets("7")
'Locate the rows to be checked
'2
If shTarget1.Cells(3, 6).Value = "" Then
a = 3
Else
a = shTarget1.Cells(3, 6).CurrentRegion.Rows.Count + 3
End If
'3
If shTarget2.Cells(3, 6).Value = "" Then
b = 3
Else
b = shTarget2.Cells(3, 6).CurrentRegion.Rows.Count + 3
End If
'4
If shTarget3.Cells(3, 6).Value = "" Then
c = 3
Else
c = shTarget3.Cells(3, 6).CurrentRegion.Rows.Count + 3
End If
'5
If shTarget4.Cells(3, 6).Value = "" Then
d = 3
Else
d = shTarget4.Cells(3, 6).CurrentRegion.Rows.Count + 3
End If
'6
If shTarget5.Cells(3, 6).Value = "" Then
e = 3
Else
e = shTarget5.Cells(3, 6).CurrentRegion.Rows.Count + 3
End If
'7
If shTarget6.Cells(3, 6).Value = "" Then
f = 3
Else
f = shTarget6.Cells(3, 6).CurrentRegion.Rows.Count + 3
End If
i = 3
'Do while that will read the data of the cells in the 5th column and if it is match for the string variables, it will move the entire row to the worksheet of the same name
Do While i <= 200
'2
If Cells(i, 6).Value = "2" Then
shSource.Rows(i).Copy
shTarget1.Cells(a, 1).PasteSpecial Paste:=xlPasteValues
shSource.Rows(i).Delete
a = a + 1
GoTo Line1
'3
ElseIf Cells(i, 6).Value = "3" Then
shSource.Rows(i).Copy
shTarget2.Cells(b, 1).PasteSpecial Paste:=xlPasteValues
shSource.Rows(i).Delete
b = b + 1
GoTo Line1
End If
'4
If Cells(i, 6).Value = "4" Then
shSource.Rows(i).Copy
shTarget3.Cells(c, 1).PasteSpecial Paste:=xlPasteValues
shSource.Rows(i).Delete
c = c + 1
GoTo Line1
'5
ElseIf Cells(i, 6).Value = "5" Then
shSource.Rows(i).Copy
shTarget4.Cells(d, 1).PasteSpecial Paste:=xlPasteValues
shSource.Rows(i).Delete
d = d + 1
GoTo Line1
End If
'6
If Cells(i, 6).Value = "6" Then
shSource.Rows(i).Copy
shTarget5.Cells(e, 1).PasteSpecial Paste:=xlPasteValues
shSource.Rows(i).Delete
e = e + 1
GoTo Line1
'7
ElseIf Cells(i, 6).Value = "7" Then
shSource.Rows(i).Copy
shTarget6.Cells(f, 1).PasteSpecial Paste:=xlPasteValues
shSource.Rows(i).Delete
f = f + 1
GoTo Line1
End If
i = i + 1
Line1: Loop
Set mysheet = ActiveSheet
Dim wrksht As Worksheet
For Each wrksht In Worksheets
wrksht.Select
Cells.EntireColumn.AutoFit
Next wrksht
mysheet.Select
End Sub
I get the "Run Time Error 9, Subscript out of range".
The reason I get this error is because the sheet does not exist.
So for example, when the sheets are being created based on their cell values and in the cell there's no actual number 4, then a sheet with the name "4" will obviously not be created.
Ideally I wanted to code it in a way that didn't require hard coded string variables to do the check, but I simply don't know how to create that dynamic piece of code. So this is what I have at the moment and I am hoping someone can either help clean up the code to not have hard coded variables (1,2,3,4...) and perhaps just do a check first if the sheet exists then look for the sheet name in the column OR do the same thing but just input some kind of if statement to determine if the sheet exists before it bombs out.
I'm thinking of something like:
If (sheet.name("4") exists) Then
Set shTarget4 = ThisWorkbook.Sheets("4")
Else
Resume
There's no need for me to keep the original sheet's data as this is not the source sheet.
The data from the first sheet comes from its source via means of a macro, so if I ever need to refer to the source data then it wont be an issue.
Also, the other reason is that each sheet will be saved as individual workbooks in a folder when my macro's are run so that I can send off each individual sheet to their respective departments.
Here's how I'd do it. Should be OK provided the values in Col F are valid sheet names.
Sub CopyData()
Dim shtSrc As Worksheet
Dim c As Range, ws, r As Long, v
Set shtSrc = ThisWorkbook.Sheets("Sheet1")
For Each c In shtSrc.Range(shtSrc.Cells(2, 6), shtSrc.Cells(Rows.Count, 6).End(xlUp)).Cells
v = c.Value
If Len(v) > 0 Then
With GetSheet(ThisWorkbook, v)
'first row with no value in ColF
r = .Cells(Rows.Count, 6).End(xlUp).Offset(1, 0).Row
If r < 3 Then r = 3 'start at 3rd row
.Rows(r).Value = c.EntireRow.Value 'copy row content (value only)
End With
End If
Next c
End Sub
'Return a worksheet from a workbook: if not there, create a new sheet
' with the supplied name and return that
Function GetSheet(wb As Workbook, theName) As Worksheet
Dim ws As Worksheet
On Error Resume Next
Set ws = wb.Worksheets(theName)
On Error GoTo 0
If ws Is Nothing Then
Set ws = wb.Worksheets.Add(after:=wb.Worksheets(wb.Worksheets.Count))
ws.Name = theName
End If
Set GetSheet = ws
End Function
as for your explicit question (looking for some If (sheet.name("4") exists) Then way) you could take advantage of this helper function:
Function IsSheetThere(shtName As String, sht As Worksheet) As Boolean
On Error Resume Next
Set sht = Worksheets(shtName)
IsSheetThere = Not sht Is Nothing
End Function
to be used like:
Dim targetSht As Worksheet
If IsSheetThere("4", targetSht) Then
... (code to handle existing sheet)
End If
While as for the more general request ("dynamic piece of code"), you could use AutoFilter() method of Range object to formerly filter your source sheet column F and then copy/paste values to proper target sheet in one shot
I'm assuming that:
"1" is the worksheet whose column 6 cells you want to loop through from row 3 to the last one and copy/paste entire rows to a target sheet whose name matches current cell value
source sheet column 6 has a header in row 2
Sub CopyRowData()
Dim sourceSht As Worksheet
Set sourceSht = ThisWorkbook.Sheets("1")
Dim iSht As Long
Dim targetSht As Worksheet
With sourceSht
With .Range("F2", .Cells(.Rows.Count, "F").End(xlUp))
For iSht = 2 To 7
If IsSheetThere(CStr(iSht), targetSht) Then
.AutoFilter Field:=1, Criteria1:=iSht
If Application.WorksheetFunction.Subtotal(103, .Cells) > 1 Then
Intersect(.Offset(1).Resize(.Rows.Count - 1).SpecialCells(xlCellTypeVisible).EntireRow, .Parent.UsedRange).Copy
With targetSht
.Cells(WorksheetFunction.Max(3, .Cells(.Rows.Count, 1).End(xlUp).Row), 1).PasteSpecial Paste:=xlPasteValues
.Cells.EntireColumn.AutoFit
End With
Application.CutCopyMode = False
End If
End If
Next
End With
.AutoFilterMode = False
End With
End Sub

Compare 2 Sheets, Export Unique Rows to Another Sheet

I am being providing two contact lists as CSVs. List 2 is a new export. List 1 is from a week prior. However, List 2 includes the contacts from List 1.
This is not a matter of "Remove Duplicates" because I want to extract only the unique rows.
I have List 1 in Sheet1. I have List 2 in Sheet2. Sheet3 is empty. I need to compare Column 3 (email address) in List 1 to Column 3 (email address) in List 2 and EntireRow.Copy where Column 3 is unique, i.e. it appears ONLY in List 2, NOT in List 1.
I am no stranger to conditional logic, but I've never used Excel Macros / VBA like this. I was able to find a solution (see "2nd code") to export duplicates to a new sheet, and tried to modify it to export uniques, but I wasn't able to make it work.
EDIT 1
This is the code I modified from the aforementioned answer.
Option Explicit
Sub FilterAndCopy2()
Dim wstSource As Worksheet, _
wstOutput As Worksheet
Dim rngMyData As Range, _
helperRng As Range, _
unionRng As Range
Dim i As Long, iOld As Long
Set wstSource = Worksheets("DUPLICATE LIST FILTER")
Set wstOutput = Worksheets("UNIQUE LIST RESULTS")
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
With wstSource
Set rngMyData = .Range("A1:K" & .Range("A" & .Rows.Count).End(xlUp).Row)
End With
With rngMyData
Set helperRng = .Offset(, rngMyData.Columns.Count - 1).Resize(, 1)
Set unionRng = .Cells(1000, 1000) 'set a "helper" cell to be used with Union method, to prevent it from failing the first time
End With
With helperRng
.FormulaR1C1 = "=row()" 'mark rows with ad ascending number (its own row number)
.Value = .Value
End With
With rngMyData.Resize(, rngMyData.Columns.Count + 1) 'enclose "helper" column
.Sort key1:=.Columns(10), Order1:=xlAscending, Orientation:=xlTopToBottom, Header:=xlNo ' sort data to have all same columnA values grouped one after another
i = .Rows(1).Row 'start loop from data first row
Do While i < .Rows(.Rows.Count).Row
iOld = i 'set current row as starting row
Do While .Cells(iOld + 1, 1) = .Cells(iOld, 1) 'loop till first cell with different value
iOld = iOld + 1
Loop
If iOld - i = 0 Then Set unionRng = Union(unionRng, .Cells(i, 1).Resize(iOld - i + 1)) 'if more than one cell found with "current" value, then add them to "UnionRng" range
i = iOld + 1
Loop
Intersect(unionRng, rngMyData).EntireRow.Copy Destination:=wstOutput.Cells(1, 1) 'get rid of the "helper" cell via Intersect method
wstOutput.Columns(helperRng.Column).Clear 'delete "Helper" column pasted in wstOutput sheet
.Sort key1:=.Columns(10), Order1:=xlAscending, Orientation:=xlTopToBottom, Header:=xlNo ' sort data in wstSource back
End With
helperRng.Clear 'delete "helper" column, not needed anymore
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
End Sub
The result is not comparing the Email column. I found a known duplicate in my data, and modified the email address. The row was not exported to the target Sheet.
NOTE: This in-progress solution does not use 3 separate sheets as I described above, only two.
Code below is assuming you don't need to actually copy/paste the row but rather transfer the value in the result sheet.
Sub find_unique()
Application.ScreenUpdating = False
Dim Wb As Workbook, Sh1 As Worksheet, Sh2 As Worksheet, Sh3 As Worksheet
Dim P1 As Range, P2 As Range, a As Integer
Set Wb = Workbooks("unique.xlsm")
Set Sh1 = Wb.Sheets(1) ' Adapt to original list
Set Sh2 = Wb.Sheets(2) ' Adapt to updated list
Set Sh3 = Wb.Sheets(3) ' Adapt to destination sheet
Set P1 = Sh1.UsedRange
Set P2 = Sh2.UsedRange
Set D1 = CreateObject("scripting.dictionary")
T1 = P1
For i = 2 To UBound(T1)
D1(UCase(T1(i, 1))) = UCase(T1(i, 1)) 'Change 1 for the column number of your unique identifier
Next i
T2 = P2
a = 1
Dim T3()
For i = 1 To UBound(T2)
If i = 1 Then 'Considering you have headers
ReDim Preserve T3(1 To UBound(T2, 2), 1 To a)
For j = 1 To UBound(T2, 2)
T3(j, a) = T2(i, j)
Next j
a = a + 1
Else
If Not D1.exists(UCase(T2(i, 1))) Then 'Change 1 for the column number of you unique identifier
ReDim Preserve T3(1 To UBound(T2, 2), 1 To a)
For j = 1 To UBound(T2, 2)
T3(j, a) = T2(i, j)
Next j
a = a + 1
End If
End If
Next i
Sh3.Cells.Clear 'Assuming you want to replace the result in sheet(3) each time
Sh3.Range("A1").Resize(UBound(T3, 2), UBound(T3, 1)) = Application.Transpose(T3)
Application.ScreenUpdating = True
End Sub
Other option if you really want to copy/paste the unique row :
Sub find_unique2()
Application.ScreenUpdating = False
Dim Wb As Workbook, Sh1 As Worksheet, Sh2 As Worksheet, Sh3 As Worksheet
Dim P1 As Range, P2 As Range, a As Integer
Set Wb = Workbooks("unique.xlsm")
Set Sh1 = Wb.Sheets(1) ' Adapt to original list
Set Sh2 = Wb.Sheets(2) ' Adapt to updated list
Set Sh3 = Wb.Sheets(3) ' Adapt to destination sheet
Set P1 = Sh1.UsedRange
Set P2 = Sh2.UsedRange
Set D1 = CreateObject("scripting.dictionary")
T1 = P1
For i = 2 To UBound(T1)
D1(UCase(T1(i, 1))) = UCase(T1(i, 1)) 'Change 1 for the column number of your unique identifier
Next i
T2 = P2
a = 2
Sh3.Cells.Clear
For i = 1 To UBound(T2)
If i = 1 Then 'Considering you have headers
Sh2.Rows(1).Copy Sh3.Rows(1)
Else
If Not D1.exists(UCase(T2(i, 1))) Then 'Change 1 for the column number of you unique identifier
Sh2.Rows(i).Copy Sh3.Rows(a)
a = a + 1
End If
End If
Next i
Application.ScreenUpdating = True
End Sub

Copying Multiple columns in Excel-Vba

Hi I am trying to copy multiple columns from one workbook to another, and below is the code how I copied one and need help in making the code more optimized as I don't want to write same code for all the columns. below is the code.
Sub Copymc()
Dim x As Workbook
Dim y As Workbook
Set x = Workbooks.Open("H:\testing\demo\test2.xlsx")
Set y = Workbooks.Open("H:\testing\demo\test1.xlsx")
Dim LastRow As Long
Dim NextRow As Long
' determine where the data ends on Column B Sheet1
x.Worksheets("Sheet1").Activate
Range("A65536").Select
ActiveCell.End(xlUp).Select
LastRow = ActiveCell.Row
' copy the data from Column B in Sheet 1
Range("A2:A" & LastRow).Copy
' Determine where to add the new data in Column C Sheet 2
y.Worksheets("Sheet1").Activate
Range("A65536").Select
ActiveCell.End(xlUp).Offset(1, 0).Select
NextRow = ActiveCell.Row
' paste the data to Column C Sheet 2
y.Worksheets("Sheet1").Range("A" & NextRow).Select
ActiveSheet.Paste
Application.CutCopyMode = False
Range("A1").Select
End Sub
I tried to put all columns in range statement but problem I found was how to paste? How can I do it for multiple columns without repeating the code? Thanks in advance.
Let's say you want to copy columns A-D:
Sub Copymc()
Dim x As Workbook
Dim y As Workbook
Set x = Workbooks.Open("H:\testing\demo\test2.xlsx")
Set y = Workbooks.Open("H:\testing\demo\test1.xlsx")
Dim LastRow As Long
Dim NextRow As Long
' determine where the data ends on Column B Sheet1
x.Worksheets("Sheet1").Activate
Range("A65536").Select
ActiveCell.End(xlUp).Select
LastRow = ActiveCell.Row
' copy the data from Column B in Sheet 1
Range("A2:D" & LastRow).Copy y.worksheets("Sheet1").range("a65536").end(xlup).offset(1,0)
' Determine where to add the new data in Column C Sheet 2
'y.Worksheets("Sheet1").Activate
'Range("A65536").Select
'ActiveCell.End(xlUp).Offset(1, 0).Select
'NextRow = ActiveCell.Row
' paste the data to Column C Sheet 2
'y.Worksheets("Sheet1").Range("A" & NextRow).Select
'ActiveSheet.Paste
Application.CutCopyMode = False
Range("A1").Select
End Sub
I try to avoid the copy and paste functions as much as possible. To get around this I would loop through all of the values in the column and move them to your other workbook as such:
Sub test()
Dim x As Workbook
Dim y As Workbook
Set x = Workbooks.Open("H:\testing\demo\test2.xlsx")
Set y = Workbooks.Open("H:\testing\demo\test1.xlsx")
Dim LastRow As Long
LastRow = x.Sheets("Sheet1").Range("A65536").End(xlUp).Row
For i = 1 To LastRow
CopyVal = x.Sheets("Sheet1").Range("A1").Offset(i, 0).Value
CopyVal2 = x.Sheets("Sheet1").Range("A1").Offset(i, 1).Value
CopyVal3 = x.Sheets("Sheet1").Range("A1").Offset(i, 2).Value
CopyVal4 = x.Sheets("Sheet1").Range("A1").Offset(i, 3).Value
y.Sheets("Sheet1").Range("A65536").End(xlUp).Offset(1, 3).Value = CopyVal4
y.Sheets("Sheet1").Range("A65536").End(xlUp).Offset(1, 2).Value = CopyVal3
y.Sheets("Sheet1").Range("A65536").End(xlUp).Offset(1, 1).Value = CopyVal2
y.Sheets("Sheet1").Range("A65536").End(xlUp).Offset(1, 0).Value = CopyVal
Next
End Sub

VBA how to loop from the first cell/column (Force it)

Below are my codes, I am trying to force the checking to start from the first cell, but it doesn't work. Can anyone advise me on that. Thanks
I am trying to do checking on the names which is on the 3rd column of Workbook A and compare it with the other column in another workbook. Upon match of the string, it will copy certain cells to the desalinated column
Sub copyandpaste()
Set From_WS = Workbooks("copy_data2").Worksheets("Data")
Set To_WS = Workbooks("Book1").Worksheets("Sheet1")
Dim v1 As String
Dim v2 As String
Dim diffRow As Long
Dim dataWs As Worksheet
Dim copyWs As Worksheet
Dim rowData As Long
Dim totRows As Long
Dim lastRow As Long
Dim result As String
Dim row_no As Integer
Dim Name As Range
Dim Namelist As Range
diffRow = 1 'compare
Set dataWs = Worksheets("Data")
Set copyWs = Worksheets("Diff")
For Each c In Worksheets("Data").Range("C2:C10")
If c.Value <> "" Then
v1 = c
End If
For Each d In Workbooks("Book1").Worksheets("Sheet1").Range("B2:B10")
If d.Value <> "" Then
v2 = d
End If
With From_WS.Cells(1, 2).CurrentRegion
Total_Rows = .Rows.Count
Total_Columns = .Columns.Count
End With
Set mycellA = From_WS.Range("C:C")
Set mycellB = To_WS.Range("B:B")
Copy = False
' With Sheets("copy_data2")
' lastRow = .Range("A" & .Rows.Count).End(xlUp).Row
'find first row
'column1 = Range("A2").End(xlToRight).Column
'For row_no = 1 To 10
'=========================================================================
Set Namelist = dataWs.Range("A1:A" & dataWs.Cells(Rows.Count, "A").End(xlUp).Row)
'Now loop through all the cells in the range
'For Each Name In Namelist.Cells
mynumber = 1
For Each Name In Namelist
'=======================================================================
If v1 = v2 Then
'select sheet
Sheets("Data").Select
'ActiveCell.Select 'select active cell
ActiveCell.Interior.ColorIndex = 36 'color the cell
'copy active cell same row
ActiveCell.Range("A1:F1").Copy
ActiveCell.Interior.ColorIndex = 50 'color the cell
'Paste file destination
Sheets("Diff").Select
Sheets("Diff").Range("A2").Select
'Paste Active
ActiveSheet.Paste
ActiveCell.Interior.ColorIndex = 37 '<< Colored Blue
'==================================================================
'select sheet
Sheets("Data").Select
'ActiveCell.Select 'select active cell
ActiveCell.Interior.ColorIndex = 36 'color cell Yellow
'result = ActiveCell.EntireRow.copy
'copy active cell same row
ActiveCell.Range("H1:J1").Copy
'Paste file destination
Sheets("Diff").Select
'Paste cell destination
Sheets("Diff").Range("G2").Select
'Paste Active
ActiveSheet.Paste
mynumber = mynumber + 1
End If
Next Name
Next d
Next c
End Sub
This is the second function, to count and go through the rows.
Sub RoundToZero1()
For Counter = 1 To 20
Set curCell = Worksheets("Data").Cells(Counter, 3)
If Abs(curCell.Value) < 0.01 Then curCell.Value = 0
Next Counter
End Sub
Update Question:
I have the code below, I need to make the column A to be incremental. Anyone have suggestion how to achieve that?
Sheets("Diff").Range("A").Select
The line Set selectedCell = selectedCell + 1 throws an error when I run it and doesn't appear to do anything in the code, if that is the case you should comment it out or delete it.
Also I think you need to change
Else
If IsEmpty(Cells(i, 1)) = True Then 'if cells in column "A" is empty then stop
to
ElseIf IsEmpty(Cells(i, 1)) = True Then 'if cells in column "A" is empty then stop
As it stands you have an extra open If statement.