I have two different worksheets. I have to copy the first column(A) from first sheet. Based upon the string in the first column, copy the data form the first column, copy the second column(B) from first sheet, copy the second column(B) from second sheet and then find out the difference between the two copied columns. All this data will be pasted into a new sheet. Repeat the process to copy the third column(C) from first sheet, copy the third column(C) from second sheet and then find out the difference. This process is repeated until the last column.
How to make the code dynamic such that it looks up the data in the first column in bot the sheets and then copies the data from the other columns.
I was able to make this code work with WWC's assistance, but how to look up the data in the first column and then copy values.
Sub Macro4()
'
' Macro4 Macro
'
'
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Dim ws3 As Worksheet
Dim coli As Double
Dim Coli3 As Double
Dim rowy As Double
Dim numCols As Double
Dim startRow As Double
Dim lastRow As Double
startRow = 6 'assuming data starts here
Coli3 = 2 ' start the columns out on ws3
Set ws1 = ThisWorkbook.Worksheets("Sheet1")
Set ws2 = ThisWorkbook.Worksheets("Sheet2")
Set ws3 = ThisWorkbook.Worksheets("Comparison")
Application.ScreenUpdating = False
ws3.Cells.Clear
ws1.Range("A1").EntireColumn.Copy Destination:=ws3.Range("A1")
'Find how many columns there are in sheet1 based on data in row 1
numCols = ws1.Cells(7, Columns.Count).End(xlToLeft).Column
For coli = 2 To numCols
'Find last Data row in the given column in sheet1
lastRow = ws1.Cells(ws1.Rows.Count, coli).End(xlUp).Row
For rowy = 6 To lastRow
ws3.Cells(rowy, Coli3) = Format(ws1.Cells(rowy, coli).Value, "#,##0") ' copy sheet 1 to the right spot of sheet 3
ws3.Cells(rowy, Coli3 + 1) = Format(ws2.Cells(rowy, coli).Value, "#,##0") 'copy sheet 2 to the right spot of sheet 3
'perform calculation and place in the right spot on sheet 3
If rowy = "6" Then
ws3.Cells(rowy, Coli3) = ws1.Cells(rowy, coli) & "-Sheet1" ' copy sheet 1 to the right spot of sheet 3
ws3.Cells(rowy, Coli3 + 1) = ws2.Cells(rowy, coli) & "-Sheet2" 'copy sheet 2 to the right spot of sheet 3
ws3.Cells(rowy, Coli3 + 2) = "Difference"
Else
ws3.Cells(rowy, Coli3) = Format(ws1.Cells(rowy, coli).Value, "#,##0") ' copy sheet 1 to the right spot of sheet 3
'ws3.Cells(rowy, Coli3).Font.Name = "Arial"
'ws3.Cells(rowy, Coli3).Font.Size = 8
ws3.Cells(rowy, Coli3 + 1) = Format(ws2.Cells(rowy, coli).Value, "#,##0") 'copy sheet 2 to the right spot of sheet 3
'ws3.Cells(rowy, Coli3 + 1).Font.Name = "Arial"
'ws3.Cells(rowy, Coli3 + 1).Font.Size = 8
ws3.Cells(rowy, Coli3 + 2) = Format((ws1.Cells(rowy, coli).Value) - (ws2.Cells(rowy, coli).Value), "#,##0")
'ws3.Cells(rowy, Coli3 + 2).Font.Name = "Arial"
'ws3.Cells(rowy, Coli3 + 2).Font.Size = 8
End If
Next rowy ' move to the next row on ws1, ws2, ws3
'Since we are placing 3 cols at a time in sheet 3 we increment differently
Coli3 = Coli3 + 3 '1 becomes 4, 4 becomes 7, 7 becomes 10 and so on
End Sub