tljenkin
Board Regular
- Joined
- Jun 14, 2007
- Messages
- 147
Hi All,
I have the following code below. It is supposed to do the following:
1) Ask user if file is open, if yes, check that the file is indeed open, activate file and run the code
2) If file is not open, it should ask user to select file to open, then run the code.
I am getting all sorts of End If or Do Loop error messages including at one time nothing happening after you answer "NO" to file is not open.
Can someone please look at code and tell me whats errors are in there?
Many Thanks
Sub UpdateDatabase()
Dim FileToBeOpened As Variant, File As Variant, Ard As Workbook, Wrr As Workbook, wb As Workbook
Dim strLastWeek As String, strThisWeek As String
Dim lLastRowColA As Long, lLastRowColB As Long
Set Ard = ThisWorkbook ' or Actuals repository current version presently open
Application.ScreenUpdating = False
Response = MsgBox(prompt:="Is the most recent weekly Redress report open?", Buttons:=vbYesNo)
If Response = vbYes Then
For Each Wrr In Application.Workbooks
If Wrr.Name Like "Week*" Then
Set wb = Wrr
wb.Activate
Exit For
End If
Next Wrr
Else
If wb Is Nothing Then
MsgBox "The file is not open."
End If
Exit Sub
Do
Sheets("Tracking Report").Select
Application.Goto Reference:="R5C2:R500C38"
Selection.Copy
'Define the final row
Ard.Activate
Sheet13.Select
FinalRow = Cells(Rows.Count, 1).End(xlUp).Row
NextRow = FinalRow + 1
Cells(NextRow, 2).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
'---get last row of Columns A and B
lLastRowColA = Range("A" & Rows.Count).End(xlUp).Row
lLastRowColB = Range("B" & Rows.Count).End(xlUp).Row
If lLastRowColA = lLastRowColB Then Exit Sub
'---increment last week by 1 to make string for this week
strLastWeek = Range("A" & lLastRowColA).Value
If Left(strLastWeek, 4) = "Week" Then
strThisWeek = "Week" & Mid(strLastWeek, 5) + 1
Else
MsgBox "Error: Last Cell in Col A is: " & strLastWeek
End If
'---fill in Col A for this week's records
Range(Range("A" & lLastRowColA + 1), _
Range("A" & lLastRowColB)).Value = strThisWeek
Calculate
ActiveWorkbook.RefreshAll
Sheet22.Activate
Wrr.Close (False)
Application.ScreenUpdating = True
If Response = vbNo Then
On Error GoTo 0
FileToBeOpened = Application.GetOpenFilename(FileFilter:="All Excel Files (*.xls*), *.xls*", Title:="Where is the most recent weekly Redress file?", MultiSelect:=False)
Application.ScreenUpdating = False
' Exit if user exits dialog
If FileToBeOpened = False Then Exit Sub
' Open file (Wrr), copy most recent weekly redress data to the Actuals Repository
Set Wrr = Workbooks.Open(FileToBeOpened)
Wrr.Activate
Loop
End Sub
I have the following code below. It is supposed to do the following:
1) Ask user if file is open, if yes, check that the file is indeed open, activate file and run the code
2) If file is not open, it should ask user to select file to open, then run the code.
I am getting all sorts of End If or Do Loop error messages including at one time nothing happening after you answer "NO" to file is not open.
Can someone please look at code and tell me whats errors are in there?
Many Thanks
Sub UpdateDatabase()
Dim FileToBeOpened As Variant, File As Variant, Ard As Workbook, Wrr As Workbook, wb As Workbook
Dim strLastWeek As String, strThisWeek As String
Dim lLastRowColA As Long, lLastRowColB As Long
Set Ard = ThisWorkbook ' or Actuals repository current version presently open
Application.ScreenUpdating = False
Response = MsgBox(prompt:="Is the most recent weekly Redress report open?", Buttons:=vbYesNo)
If Response = vbYes Then
For Each Wrr In Application.Workbooks
If Wrr.Name Like "Week*" Then
Set wb = Wrr
wb.Activate
Exit For
End If
Next Wrr
Else
If wb Is Nothing Then
MsgBox "The file is not open."
End If
Exit Sub
Do
Sheets("Tracking Report").Select
Application.Goto Reference:="R5C2:R500C38"
Selection.Copy
'Define the final row
Ard.Activate
Sheet13.Select
FinalRow = Cells(Rows.Count, 1).End(xlUp).Row
NextRow = FinalRow + 1
Cells(NextRow, 2).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
'---get last row of Columns A and B
lLastRowColA = Range("A" & Rows.Count).End(xlUp).Row
lLastRowColB = Range("B" & Rows.Count).End(xlUp).Row
If lLastRowColA = lLastRowColB Then Exit Sub
'---increment last week by 1 to make string for this week
strLastWeek = Range("A" & lLastRowColA).Value
If Left(strLastWeek, 4) = "Week" Then
strThisWeek = "Week" & Mid(strLastWeek, 5) + 1
Else
MsgBox "Error: Last Cell in Col A is: " & strLastWeek
End If
'---fill in Col A for this week's records
Range(Range("A" & lLastRowColA + 1), _
Range("A" & lLastRowColB)).Value = strThisWeek
Calculate
ActiveWorkbook.RefreshAll
Sheet22.Activate
Wrr.Close (False)
Application.ScreenUpdating = True
If Response = vbNo Then
On Error GoTo 0
FileToBeOpened = Application.GetOpenFilename(FileFilter:="All Excel Files (*.xls*), *.xls*", Title:="Where is the most recent weekly Redress file?", MultiSelect:=False)
Application.ScreenUpdating = False
' Exit if user exits dialog
If FileToBeOpened = False Then Exit Sub
' Open file (Wrr), copy most recent weekly redress data to the Actuals Repository
Set Wrr = Workbooks.Open(FileToBeOpened)
Wrr.Activate
Loop
End Sub