Dear All,
I am new to Excel VBA. I have two sheets in the same workbook, Sheet1 & Production. In "Sheet1" I have two columns A & B with date and well names. I need to loop through these rows and compare to production sheet. If well name and date are matched then get production figure. I managed to get it working but my real sheet has over 23,000 rows and over 200 columns on both sheets so I need a faster and more efficient way of doing this. my current code looks like this:
Private Sub CommandButton1_Click()
Dim i As Integer
Dim j As Integer
Dim k As Integer
Dim iRow As Long
Dim jRow As Long
Dim kCol As Long
'Find the last non-blank row in Sheet1
iRow = Worksheets("Sheet1").Range("B" & Rows.Count).End(xlUp).Offset(1).Row
'Find the last non-blank row in Production sheet
jRow = Worksheets("Production").Range("A" & Rows.Count).End(xlUp).Offset(1).Row
'Find the last non-blank column in Production sheet
kCol = Worksheets("Production").Cells(1, Columns.Count).End(xlToLeft).Column
For i = 2 To iRow
For j = 2 To jRow
For k = 2 To kCol
If Worksheets("Sheet1").Cells(i, 2) = Worksheets("Production").Cells(j, 1) Then
If Worksheets("Sheet1").Cells(i, 1) = Worksheets("Production").Cells(1, k) Then
Worksheets("Sheet1").Cells(i, 3) = Worksheets("Production").Cells(j, k)
End If
End If
Next k
Next j
Next i
End Sub
my "Sheet1" looks like this"
[TABLE="width: 241"]
<tbody>[TR]
[TD]Name[/TD]
[TD]Date[/TD]
[TD]Production[/TD]
[/TR]
[TR]
[TD="align: left"]Well A[/TD]
[TD="align: right"]01/01/2017[/TD]
[TD][/TD]
[/TR]
[TR]
[TD="align: left"]Well B[/TD]
[TD="align: right"]01/01/2017[/TD]
[TD][/TD]
[/TR]
[TR]
[TD="align: left"]Well C[/TD]
[TD="align: right"]01/01/2017[/TD]
[TD][/TD]
[/TR]
[TR]
[TD="align: left"]Well A[/TD]
[TD="align: right"]02/01/2017[/TD]
[TD][/TD]
[/TR]
[TR]
[TD="align: left"]Well B[/TD]
[TD="align: right"]02/01/2017[/TD]
[TD][/TD]
[/TR]
[TR]
[TD="align: left"]Well C[/TD]
[TD="align: right"]02/01/2017[/TD]
[TD][/TD]
[/TR]
[TR]
[TD="align: left"]Well A[/TD]
[TD="align: right"]03/01/2017[/TD]
[TD][/TD]
[/TR]
[TR]
[TD="align: left"]Well B[/TD]
[TD="align: right"]03/01/2017[/TD]
[TD][/TD]
[/TR]
[TR]
[TD="align: left"]Well C[/TD]
[TD="align: right"]03/01/2017[/TD]
[TD][/TD]
[/TR]
</tbody><colgroup><col><col><col></colgroup>[/TABLE]
my "Production sheet looks like this:
[TABLE="width: 267"]
<tbody>[TR]
[TD="align: left"] Date [/TD]
[TD="align: left"]Well A[/TD]
[TD="align: left"]Well B[/TD]
[TD="align: left"]Well C[/TD]
[/TR]
[TR]
[TD="align: right"]05/02/2015[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]10[/TD]
[TD="align: right"]100[/TD]
[/TR]
[TR]
[TD="align: right"]06/02/2015[/TD]
[TD="align: right"]2[/TD]
[TD="align: right"]20[/TD]
[TD="align: right"]200[/TD]
[/TR]
[TR]
[TD="align: right"]07/02/2015[/TD]
[TD="align: right"]3[/TD]
[TD="align: right"]30[/TD]
[TD="align: right"]300[/TD]
[/TR]
[TR]
[TD="align: right"]08/02/2015[/TD]
[TD="align: right"]4[/TD]
[TD="align: right"]40[/TD]
[TD="align: right"]400[/TD]
[/TR]
[TR]
[TD="align: right"]09/02/2015[/TD]
[TD="align: right"]5[/TD]
[TD="align: right"]50[/TD]
[TD="align: right"]500[/TD]
[/TR]
[TR]
[TD="align: right"]10/02/2015[/TD]
[TD="align: right"]6[/TD]
[TD="align: right"]60[/TD]
[TD="align: right"]600[/TD]
[/TR]
[TR]
[TD="align: right"]11/02/2015[/TD]
[TD="align: right"]7[/TD]
[TD="align: right"]70[/TD]
[TD="align: right"]700[/TD]
[/TR]
[TR]
[TD="align: right"]12/02/2015[/TD]
[TD="align: right"]8[/TD]
[TD="align: right"]80[/TD]
[TD="align: right"]800[/TD]
[/TR]
[TR]
[TD="align: right"]13/02/2015[/TD]
[TD="align: right"]9[/TD]
[TD="align: right"]90[/TD]
[TD="align: right"]900[/TD]
[/TR]
[TR]
[TD="align: right"]14/02/2015[/TD]
[TD="align: right"]10[/TD]
[TD="align: right"]100[/TD]
[TD="align: right"]1000[/TD]
[/TR]
[TR]
[TD="align: right"]15/02/2015[/TD]
[TD="align: right"]11[/TD]
[TD="align: right"]110[/TD]
[TD="align: right"]1100[/TD]
[/TR]
[TR]
[TD="align: right"]01/01/2017[/TD]
[TD="align: right"]12[/TD]
[TD="align: right"]120[/TD]
[TD="align: right"]1200[/TD]
[/TR]
[TR]
[TD="align: right"]02/01/2017[/TD]
[TD="align: right"]13[/TD]
[TD="align: right"]130[/TD]
[TD="align: right"]1300[/TD]
[/TR]
[TR]
[TD="align: right"]03/01/2017[/TD]
[TD="align: right"]14[/TD]
[TD="align: right"]140[/TD]
[TD="align: right"]1400[/TD]
[/TR]
</tbody><colgroup><col><col span="3"></colgroup>[/TABLE]
For short number of rows and columns the code works well. However it takes forever when I ran it with real data with over 20,000 rows. Please help me to optimize the loop so it can runs faster.
Regards,
I am new to Excel VBA. I have two sheets in the same workbook, Sheet1 & Production. In "Sheet1" I have two columns A & B with date and well names. I need to loop through these rows and compare to production sheet. If well name and date are matched then get production figure. I managed to get it working but my real sheet has over 23,000 rows and over 200 columns on both sheets so I need a faster and more efficient way of doing this. my current code looks like this:
Private Sub CommandButton1_Click()
Dim i As Integer
Dim j As Integer
Dim k As Integer
Dim iRow As Long
Dim jRow As Long
Dim kCol As Long
'Find the last non-blank row in Sheet1
iRow = Worksheets("Sheet1").Range("B" & Rows.Count).End(xlUp).Offset(1).Row
'Find the last non-blank row in Production sheet
jRow = Worksheets("Production").Range("A" & Rows.Count).End(xlUp).Offset(1).Row
'Find the last non-blank column in Production sheet
kCol = Worksheets("Production").Cells(1, Columns.Count).End(xlToLeft).Column
For i = 2 To iRow
For j = 2 To jRow
For k = 2 To kCol
If Worksheets("Sheet1").Cells(i, 2) = Worksheets("Production").Cells(j, 1) Then
If Worksheets("Sheet1").Cells(i, 1) = Worksheets("Production").Cells(1, k) Then
Worksheets("Sheet1").Cells(i, 3) = Worksheets("Production").Cells(j, k)
End If
End If
Next k
Next j
Next i
End Sub
my "Sheet1" looks like this"
[TABLE="width: 241"]
<tbody>[TR]
[TD]Name[/TD]
[TD]Date[/TD]
[TD]Production[/TD]
[/TR]
[TR]
[TD="align: left"]Well A[/TD]
[TD="align: right"]01/01/2017[/TD]
[TD][/TD]
[/TR]
[TR]
[TD="align: left"]Well B[/TD]
[TD="align: right"]01/01/2017[/TD]
[TD][/TD]
[/TR]
[TR]
[TD="align: left"]Well C[/TD]
[TD="align: right"]01/01/2017[/TD]
[TD][/TD]
[/TR]
[TR]
[TD="align: left"]Well A[/TD]
[TD="align: right"]02/01/2017[/TD]
[TD][/TD]
[/TR]
[TR]
[TD="align: left"]Well B[/TD]
[TD="align: right"]02/01/2017[/TD]
[TD][/TD]
[/TR]
[TR]
[TD="align: left"]Well C[/TD]
[TD="align: right"]02/01/2017[/TD]
[TD][/TD]
[/TR]
[TR]
[TD="align: left"]Well A[/TD]
[TD="align: right"]03/01/2017[/TD]
[TD][/TD]
[/TR]
[TR]
[TD="align: left"]Well B[/TD]
[TD="align: right"]03/01/2017[/TD]
[TD][/TD]
[/TR]
[TR]
[TD="align: left"]Well C[/TD]
[TD="align: right"]03/01/2017[/TD]
[TD][/TD]
[/TR]
</tbody><colgroup><col><col><col></colgroup>[/TABLE]
my "Production sheet looks like this:
[TABLE="width: 267"]
<tbody>[TR]
[TD="align: left"] Date [/TD]
[TD="align: left"]Well A[/TD]
[TD="align: left"]Well B[/TD]
[TD="align: left"]Well C[/TD]
[/TR]
[TR]
[TD="align: right"]05/02/2015[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]10[/TD]
[TD="align: right"]100[/TD]
[/TR]
[TR]
[TD="align: right"]06/02/2015[/TD]
[TD="align: right"]2[/TD]
[TD="align: right"]20[/TD]
[TD="align: right"]200[/TD]
[/TR]
[TR]
[TD="align: right"]07/02/2015[/TD]
[TD="align: right"]3[/TD]
[TD="align: right"]30[/TD]
[TD="align: right"]300[/TD]
[/TR]
[TR]
[TD="align: right"]08/02/2015[/TD]
[TD="align: right"]4[/TD]
[TD="align: right"]40[/TD]
[TD="align: right"]400[/TD]
[/TR]
[TR]
[TD="align: right"]09/02/2015[/TD]
[TD="align: right"]5[/TD]
[TD="align: right"]50[/TD]
[TD="align: right"]500[/TD]
[/TR]
[TR]
[TD="align: right"]10/02/2015[/TD]
[TD="align: right"]6[/TD]
[TD="align: right"]60[/TD]
[TD="align: right"]600[/TD]
[/TR]
[TR]
[TD="align: right"]11/02/2015[/TD]
[TD="align: right"]7[/TD]
[TD="align: right"]70[/TD]
[TD="align: right"]700[/TD]
[/TR]
[TR]
[TD="align: right"]12/02/2015[/TD]
[TD="align: right"]8[/TD]
[TD="align: right"]80[/TD]
[TD="align: right"]800[/TD]
[/TR]
[TR]
[TD="align: right"]13/02/2015[/TD]
[TD="align: right"]9[/TD]
[TD="align: right"]90[/TD]
[TD="align: right"]900[/TD]
[/TR]
[TR]
[TD="align: right"]14/02/2015[/TD]
[TD="align: right"]10[/TD]
[TD="align: right"]100[/TD]
[TD="align: right"]1000[/TD]
[/TR]
[TR]
[TD="align: right"]15/02/2015[/TD]
[TD="align: right"]11[/TD]
[TD="align: right"]110[/TD]
[TD="align: right"]1100[/TD]
[/TR]
[TR]
[TD="align: right"]01/01/2017[/TD]
[TD="align: right"]12[/TD]
[TD="align: right"]120[/TD]
[TD="align: right"]1200[/TD]
[/TR]
[TR]
[TD="align: right"]02/01/2017[/TD]
[TD="align: right"]13[/TD]
[TD="align: right"]130[/TD]
[TD="align: right"]1300[/TD]
[/TR]
[TR]
[TD="align: right"]03/01/2017[/TD]
[TD="align: right"]14[/TD]
[TD="align: right"]140[/TD]
[TD="align: right"]1400[/TD]
[/TR]
</tbody><colgroup><col><col span="3"></colgroup>[/TABLE]
For short number of rows and columns the code works well. However it takes forever when I ran it with real data with over 20,000 rows. Please help me to optimize the loop so it can runs faster.
Regards,