D
davegb
With a lot of help here, I've written some code to compare 2
spreadsheets. But when I couple the code to run a userform to get the
filenames to compare with the code to align the data in corresponding
rows, the second macro doesn't recognize the workbook names that have
been saved in the first macro.
Here's the code with the userform:
Option Explicit
Public wsNew As Worksheet
Public wsOrig As Worksheet
Public wbNew As Workbook
Public wbOrig As Workbook
Private Sub CancelButton_Click()
Unload UserForm2
End Sub
Private Sub OkButton_Click()
'Workbooks(ComboBox1.Text).Select
Set wbNew = Workbooks(ComboBox1.Text)
Set wbOrig = Workbooks(ComboBox2.Text)
'wbNew.Activate
UserForm2.Hide
Call EvenOutRows
End Sub
Private Sub UserForm_Initialize()
Dim wbk As Workbook
For Each wbk In Workbooks
ComboBox1.AddItem wbk.Name
ComboBox2.AddItem wbk.Name
Next wbk
End Sub
And the other code:
Sub EvenOutRows()
Dim wsNew As Worksheet
Dim wsOrig As Worksheet
Dim lCurRow As Long
Dim lrowNew As Long
Dim lrowOrig As Long
UserForm2.Show
Set wsOrig = wbOrig.Worksheets("Sheet1") <-------OBJECT VARIABLE OR
WITHBLOCK VARIABLE NOT SET
Set wsNew = wbNew.Worksheets("sheet1")
lCurRow = 2
Do While Not IsEmpty(wsNew.Cells(lCurRow, 1)) And _
Not IsEmpty(wsOrig.Cells(lCurRow, 1))
If wsNew.Cells(lCurRow, 1) > wsOrig.Cells(lCurRow, 1) Then
wsNew.Rows(lCurRow).Insert: lCurRow = lCurRow + 1
ElseIf wsOrig.Cells(lCurRow, 1) > wsNew.Cells(lCurRow, 1) Then
wsOrig.Rows(lCurRow).Insert: lCurRow = lCurRow + 1
ElseIf wsNew.Cells(lCurRow, 1) = wsOrig.Cells(lCurRow, 1) Then
lCurRow = lCurRow + 1
End If
Loop
Application.Run "Compare.xla!Compare"
End Sub
The variables have been declared publicly in both modules. Any
suggestions?
spreadsheets. But when I couple the code to run a userform to get the
filenames to compare with the code to align the data in corresponding
rows, the second macro doesn't recognize the workbook names that have
been saved in the first macro.
Here's the code with the userform:
Option Explicit
Public wsNew As Worksheet
Public wsOrig As Worksheet
Public wbNew As Workbook
Public wbOrig As Workbook
Private Sub CancelButton_Click()
Unload UserForm2
End Sub
Private Sub OkButton_Click()
'Workbooks(ComboBox1.Text).Select
Set wbNew = Workbooks(ComboBox1.Text)
Set wbOrig = Workbooks(ComboBox2.Text)
'wbNew.Activate
UserForm2.Hide
Call EvenOutRows
End Sub
Private Sub UserForm_Initialize()
Dim wbk As Workbook
For Each wbk In Workbooks
ComboBox1.AddItem wbk.Name
ComboBox2.AddItem wbk.Name
Next wbk
End Sub
And the other code:
Sub EvenOutRows()
Dim wsNew As Worksheet
Dim wsOrig As Worksheet
Dim lCurRow As Long
Dim lrowNew As Long
Dim lrowOrig As Long
UserForm2.Show
Set wsOrig = wbOrig.Worksheets("Sheet1") <-------OBJECT VARIABLE OR
WITHBLOCK VARIABLE NOT SET
Set wsNew = wbNew.Worksheets("sheet1")
lCurRow = 2
Do While Not IsEmpty(wsNew.Cells(lCurRow, 1)) And _
Not IsEmpty(wsOrig.Cells(lCurRow, 1))
If wsNew.Cells(lCurRow, 1) > wsOrig.Cells(lCurRow, 1) Then
wsNew.Rows(lCurRow).Insert: lCurRow = lCurRow + 1
ElseIf wsOrig.Cells(lCurRow, 1) > wsNew.Cells(lCurRow, 1) Then
wsOrig.Rows(lCurRow).Insert: lCurRow = lCurRow + 1
ElseIf wsNew.Cells(lCurRow, 1) = wsOrig.Cells(lCurRow, 1) Then
lCurRow = lCurRow + 1
End If
Loop
Application.Run "Compare.xla!Compare"
End Sub
The variables have been declared publicly in both modules. Any
suggestions?