Help with shortening my routine

Skip Bisconer

Active Member
Joined
Jun 14, 2002
Messages
263
I have created a backup log form to post backup history and I have this tremendous amount of typing involve in my VBA. Can some one take a look for me and tell me how it can be reduced and still do the job?

Thanks for looking

Skip

Private Sub cmdPostData_Click()
Dim NextRow

Worksheets("Skip").Activate


NextRow = Application.WorksheetFunction.CountA(Range("A:A")) + 1
'Find the next available Row to post data

Worksheets("Skip").Activate

Cells(NextRow, 1) = txtDate.Value

If sxb1 = True Then Cells(NextRow, 2).Value = "Yes"

If sxb1 = False Then
Cells(NextRow, 2).Interior.ColorIndex = 6
Cells(NextRow, 2).Value = "No"
End If

If sxb2 = True Then Cells(NextRow, 3).Value = "Yes "

If sxb2 = False Then
Cells(NextRow, 3).Interior.ColorIndex = 6
Cells(NextRow, 3).Value = "No"
End If

If sxb3 = True Then Cells(NextRow, 4).Value = "Yes"

If sxb3 = False Then
Cells(NextRow, 4).Interior.ColorIndex = 6
Cells(NextRow, 4).Value = "No"
End If

If sxb4 = True Then Cells(NextRow, 5).Value = "Yes"

If sxb4 = False Then
Cells(NextRow, 5).Interior.ColorIndex = 6
Cells(NextRow, 5).Value = "No"
End If

If mxb1 = True Then
Cells(NextRow, 6).Value = "CD"
Cells(NextRow, 6).HorizontalAlignment = xlCenter
End If

If mxb2 = True Then
Cells(NextRow, 6).Value = "DVD"
Cells(NextRow, 6).HorizontalAlignment = xlCenter
End If

If mxb3 = True Then
Cells(NextRow, 6).Value = "Skips Computer"
Cells(NextRow, 6).HorizontalAlignment = xlCenter
End If

Cells(NextRow, 1).Select

Worksheets("Rosalie").Activate

NextRow = Application.WorksheetFunction.CountA(Range("A:A")) + 1

Cells(NextRow, 1).Value = txtDate
If rxb1 = True Then Cells(NextRow, 2).Value = "Yes"

If rxb1 = False Then
Cells(NextRow, 2).Interior.ColorIndex = 6
Cells(NextRow, 2).Value = "No"
End If

If rxb2 = True Then Cells(NextRow, 3).Value = "Yes "

If rxb2 = False Then
Cells(NextRow, 3).Interior.ColorIndex = 6
Cells(NextRow, 3).Value = "No"
End If

If rxb3 = True Then Cells(NextRow, 4).Value = "Yes"

If sxb3 = False Then
Cells(NextRow, 4).Interior.ColorIndex = 6
Cells(NextRow, 4).Value = "No"
End If

If mxb1 = True Then
Cells(NextRow, 5).Value = "CD"
Cells(NextRow, 5).HorizontalAlignment = xlCenter
End If

If mxb2 = True Then
Cells(NextRow, 5).Value = "DVD"
Cells(NextRow, 5).HorizontalAlignment = xlCenter
End If


If mxb3 = True Then
Cells(NextRow, 5).Value = "Skips Computer"
Cells(NextRow, 5).HorizontalAlignment = xlCenter
End If

Cells(NextRow, 1).Select


sxb1 = False
sxb2 = False
sxb3 = False
sxb4 = False
rxb1 = False
rxb2 = False
rxb3 = False
mbx1 = False
mbx2 = False
mbx3 = False

Unload BackupForm

End Sub
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Skip

Why not use option buttons for mxb1-3?

Also why not use If Else End If?
Code:
Private Sub cmdPostData_Click()
Dim NextRow

Worksheets("Skip").Activate

NextRow = Application.WorksheetFunction.CountA(Range("A:A")) + 1

ws.Cells(NextRow, 1) = txtDate.Value

If sxb1 = True Then
    Cells(NextRow, 2).Value = "Yes"
Else
    Cells(NextRow, 2).Interior.ColorIndex = 6
    Cells(NextRow, 2).Value = "No"
End If

If sxb2 = True Then
    Cells(NextRow, 3).Value = "Yes "
Else
    Cells(NextRow, 3).Interior.ColorIndex = 6
    Cells(NextRow, 3).Value = "No"
End If

If sxb3 = True Then
    Cells(NextRow, 4).Value = "Yes"
Else
    Cells(NextRow, 4).Interior.ColorIndex = 6
    Cells(NextRow, 4).Value = "No"
End If

If sxb4 = True Then
    Cells(NextRow, 5).Value = "Yes"
Else
    Cells(NextRow, 5).Interior.ColorIndex = 6
    Cells(NextRow, 5).Value = "No"
End If

If mxb1 = True Then
    Cells(NextRow, 6).Value = "CD"
    Cells(NextRow, 6).HorizontalAlignment = xlCenter
End If

If mxb2 = True Then
    Cells(NextRow, 6).Value = "DVD"
    Cells(NextRow, 6).HorizontalAlignment = xlCenter
End If

If mxb3 = True Then
    Cells(NextRow, 6).Value = "Skips Computer"
    Cells(NextRow, 6).HorizontalAlignment = xlCenter
End If

Worksheets("Rosalie").Activate

NextRow = Application.WorksheetFunction.CountA(Range("A:A")) + 1

Cells(NextRow, 1).Value = txtDate

If rxb1 = True Then
    Cells(NextRow, 2).Value = "Yes"
Else
    Cells(NextRow, 2).Interior.ColorIndex = 6
    Cells(NextRow, 2).Value = "No"
End If

If rxb2 = True Then
    Cells(NextRow, 3).Value = "Yes "
Else
    Cells(NextRow, 3).Interior.ColorIndex = 6
    Cells(NextRow, 3).Value = "No"
End If

If rxb3 = True Then
    Cells(NextRow, 4).Value = "Yes"
Else
    Cells(NextRow, 4).Interior.ColorIndex = 6
    Cells(NextRow, 4).Value = "No"
End If

If mxb1 = True Then
    Cells(NextRow, 5).Value = "CD"
    Cells(NextRow, 5).HorizontalAlignment = xlCenter
End If

If mxb2 = True Then
    Cells(NextRow, 5).Value = "DVD"
    Cells(NextRow, 5).HorizontalAlignment = xlCenter
End If

If mxb3 = True Then
    Cells(NextRow, 5).Value = "Skips Computer"
    Cells(NextRow, 5).HorizontalAlignment = xlCenter
End If

Cells(NextRow, 1).Select

sxb1 = False
sxb2 = False
sxb3 = False
sxb4 = False
rxb1 = False
rxb2 = False
rxb3 = False
mbx1 = False
mbx2 = False
mbx3 = False

Unload BackupForm

End Sub
 
Upvote 0

Forum statistics

Threads
1,224,803
Messages
6,181,055
Members
453,014
Latest member
Chris258

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top