Let me start off by saying I absolutely suck at VBA. What you are looking at is months of work and luck. I have a command button that when pressed asked for password and when entered it will move data from one sheet to another based on criteria. It works! That's the good part, however, when you press button and DON'T enter password and instead cancel....it STILL moves the data?!?!
VBA Code:
Dim pwdstate As Integer
Dim fle As String
Sub copy_Sun_Mon()
Call pwdprotect
If pwdstate = 0 Then
Call copy_sheet1("Sunday", "Monday")
End If
End Sub
Sub copy_Mon_Tue()
Call pwdprotect
If pwdstate = 0 Then
Call copy_sheet1("Monday", "Tuesday")
End If
End Sub
Sub copy_Tue_Wed()
Call pwdprotect
If pwdstate = 0 Then
Call copy_sheet1("Tuesday", "Wednesday")
End If
End Sub
Sub copy_Wed_Thu()
Call pwdprotect
If pwdstate = 0 Then
Call copy_sheet1("Wednesday", "Thursday")
End If
End Sub
Sub copy_Thu_Fri()
Call pwdprotect
If pwdstate = 0 Then
Call copy_sheet1("Thursday", "Friday")
End If
End Sub
Sub copy_Fri_Sat()
Call pwdprotect
If pwdstate = 0 Then
Call copy_sheet1("Friday", "Saturday")
End If
End Sub
Sub copy_Sat_Sun()
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Call pwdprotect
If pwdstate = 0 Then
Set wb = ActiveWorkbook
wb.Save
Call copy_sheet2("Saturday", "Sunday")
End If
Application.ScreenUpdating = True
Application.DisplayAlerts = True
End Sub
Sub Choose_File()
Dim diaFolder As FileDialog
Set diaFolder = Application.FileDialog(msoFileDialogFilePicker)
diaFolder.AllowMultiSelect = False
diaFolder.Show
On Error GoTo ErrorHandler
fle = diaFolder.SelectedItems(1)
Set diaFolder = Nothing
Exit Sub
ErrorHandler:
fle = ""
Exit Sub
End Sub
Sub pwdprotect()
Dim password As Variant
password = Application.InputBox("Enter Password", "Password Protected")
pwdstate = 0
Select Case password
Case Is = False
Exit Sub
Case Is = "CPC"
'continue
pwdstate = 0
Case Else
MsgBox "Incorrect Password"
pwdstate = 1
End Select
End Sub
Sub copy_sheet1(S1 As String, S2 As String)
Application.ScreenUpdating = False
Set wb = ActiveWorkbook
Set wp = wb.Sheets("Parameters")
For i = 1 To wb.Sheets.Count
If wb.Sheets(i).Name = S1 Then
Set w1 = wb.Sheets(i)
ElseIf wb.Sheets(i).Name = S2 Then
Set w2 = wb.Sheets(i)
End If
Next
w1.Unprotect "CPC"
w2.Unprotect "CPC"
w1.Range("ZZ99") = 1
w2.Range("ZZ99") = 1
For i = 15 To 185
If LCase(w1.Cells(i, 9)) = "open" Or LCase(w1.Cells(i, 9)) = "down" Or LCase(w1.Cells(i, 9)) = "rescheduled" Or LCase(w1.Cells(i, 9)) = "pending" Then
fromln = i
fromln = Application.WorksheetFunction.VLookup(fromln, wp.Range("A:D"), 1)
toln = Application.WorksheetFunction.VLookup(fromln, wp.Range("A:D"), 2)
If i <= toln Then
fromcol = Application.WorksheetFunction.VLookup(fromln, wp.Range("A:D"), 3)
tocol = Application.WorksheetFunction.VLookup(fromln, wp.Range("A:D"), 4)
For j = fromln To toln
If w2.Cells(j, 9) = "" Then
w2.Range(fromcol & j & ":" & tocol & j).Value = w1.Range(fromcol & i & ":" & tocol & i).Value
If fromcol = "B" Then
w1.Range(fromcol & i).Copy w2.Range(fromcol & j)
End If
w2.Rows(j).RowHeight = w1.Rows(i).RowHeight
Exit For
End If
Next
End If
End If
Next
ActiveSheet.Range("ZZ99") = ""
w1.Unprotect "CPC"
w2.Unprotect "CPC"
w1.Range("ZZ99") = 0
w2.Range("ZZ99") = 0
w1.Protect "CPC"
w2.Protect "CPC"
w2.Activate
Application.ScreenUpdating = True
End Sub
Sub copy_sheet2(S1 As String, S2 As String)
Application.ScreenUpdating = False
Set wb = ActiveWorkbook
Set wp = wb.Sheets("Parameters")
Call Choose_File
If fle <> "" Then
Set wb2 = Workbooks.Open(fle)
Else
Exit Sub
End If
For i = 1 To wb.Sheets.Count
If wb.Sheets(i).Name = S1 Then
Set w1 = wb.Sheets(i)
End If
Next
For i = 1 To wb2.Sheets.Count
If wb2.Sheets(i).Name = S2 Then
Set w2 = wb2.Sheets(i)
End If
Next
w1.Unprotect "CPC"
w1.Range("ZZ99") = 1
w2.Unprotect "CPC"
w2.Range("ZZ99") = 1
For i = 15 To 185
If LCase(w1.Cells(i, 9)) = "open" Or LCase(w1.Cells(i, 9)) = "down" Or LCase(w1.Cells(i, 9)) = "rescheduled" Or LCase(w1.Cells(i, 9)) = "pending" Then
fromln = i
fromln = Application.WorksheetFunction.VLookup(fromln, wp.Range("A:D"), 1)
toln = Application.WorksheetFunction.VLookup(fromln, wp.Range("A:D"), 2)
If i <= toln Then
fromcol = Application.WorksheetFunction.VLookup(fromln, wp.Range("A:D"), 3)
tocol = Application.WorksheetFunction.VLookup(fromln, wp.Range("A:D"), 4)
For j = fromln To toln
If w2.Cells(j, 9) = "" Then
w2.Range(fromcol & j & ":" & tocol & j).Value = w1.Range(fromcol & i & ":" & tocol & i).Value
If fromcol = "B" Then
w1.Range(fromcol & i).Copy w2.Range(fromcol & j)
End If
w2.Rows(j).RowHeight = w1.Rows(i).RowHeight
Exit For
End If
Next
End If
End If
Next
w1.Unprotect "CPC"
w1.Range("ZZ99") = 0
w1.Protect "CPC"
w2.Unprotect "CPC"
w2.Range("ZZ99") = 0
w2.Protect "CPC"
wb.Close
wb2.Save
wb2.Activate
w2.Activate
Application.ScreenUpdating = True
End Sub
Sub autofit()
Set wb = ActiveWorkbook
For i = 1 To wb.Sheets.Count
If InStr(LCase(wb.Sheets(i).Name), "day") <> 0 And wb.Sheets(i).Visible = True Then
'15-26 F,G,H
wb.Sheets(i).Range("F15:H26").WrapText = True
'27-31 B,C,D,E,F,G,H
wb.Sheets(i).Range("B27:H31").WrapText = True
'33-36 F,G,H
wb.Sheets(i).Range("F33:H36").WrapText = True
'38-45 F,G,H
wb.Sheets(i).Range("F38:H45").WrapText = True
'55-59 E,F
wb.Sheets(i).Range("D47:F53").WrapText = True
wb.Sheets(i).Range("E55:F59").WrapText = True
'61-66 F,G,H
wb.Sheets(i).Range("F61:H66").WrapText = True
'67-71 E,F,G,H
wb.Sheets(i).Range("E67:H71").WrapText = True
'73-78 F,G,H
wb.Sheets(i).Range("F73:H78").WrapText = True
'79-81 E,F,G,H
wb.Sheets(i).Range("E79:H81").WrapText = True
'83-88 F,G,H
wb.Sheets(i).Range("F83:H88").WrapText = True
'89-91 E,F,G,H
wb.Sheets(i).Range("E89:H91").WrapText = True
'137-143 F,G,H
wb.Sheets(i).Range("F137:H143").WrapText = True
'145-149 F,G,H
wb.Sheets(i).Range("F145:H149").WrapText = True
'151-155 F,G,H
wb.Sheets(i).Range("F151:H155").WrapText = True
End If
Next
End Sub
Last edited by a moderator: