mardi 19 novembre 2019

VBA code returning compile error for if statement

I am very new to VBA coding (and to coding in general) and I can't seem to "debug" my code. When I try to run it, I get a compile error stating "Else without if" but I don't understand why. Can someone also explain to me how to use a with and if statement collaboratively?

I have taken out the file path (for obvious reasons) and if there is a simpler, more elegant way to write my code as well, please have it typed out because again I am VERY NEW to VBA and coding. '''

Option Explicit


Dim Cd As Workbook
Dim Md As Workbook

Dim Changes As Worksheet
Dim HE171 As Worksheet

Dim nConfirmation As Integer


'Actions for when the "Confirm Changes" button is clicked
Private Sub CommandButton1_Click()


Set Cd = Workbooks.Open("\FILE PATH\Changes_Database_IRR_20.xlsm")
Set Md = Workbooks.Open("\FILE PATH\Database_IRR 20 New.xlsm")

Set Changes = Cd.Sheets("Changes")
Set HE171 = Md.Sheets("HE 171")

On Error Resume Next

'Creating the "Yes or No" message box displayed when operators click the "Confirm Changes" button on the Operator Sheet
nConfirmation = MsgBox("Do you want to send a notification about the sheet update?", vbInformation + vbYesNo, "Sheet Updates")

'Declares the variable for the string that we will be finding, which is the key in this case (for the With statement)
Dim FindString As String

'Declares the variable for the range in which we will be locating the string (for the With statement)
Dim RNG As Range

'Sets the string we need to find as the key value which is in cell "H4" of the Operator sheet (for the With Statement)
FindString = Sheet1.Range("H4").Value

'Actions if "YES" is clicked when the "Confirm Changes" button is clicked on the Operator Sheet
If nConfirmation = vbYes Then

'Opens and activates the Main Database workbook, with "HE 171" as the active sheet 
HE171.Activate

'Temporarily unprotects the Main Database Workbook and Operator sheet (this is the sheet the code is in)
ActiveSheet.Unprotect "Swarf"
Sheet1.Unprotect "Swarf"

'Searches all of column A in the Main Database in sheet "HE 171" for the string(key)
With ActiveSheet.Range("A:A") 'searches all of column A
Set RNG = .Find(What:=FindString, _
After:=.Cells(.Cells.Count), _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)

'////////////////////////////////////////////////////////////////////////////

'Actions if the key is present in column A of the MAIN database
If Not RNG Is Nothing Then

'Since Key is present in main database, now opens and sets the Changes_Database "Changes" Sheet as active contents
Changes.Activate

'Temporarily unprotects the Changes_Database
ActiveSheet.Unprotect "Swarf"

'Searches all of column A in the Changes_Database "Changes" sheet for the string(key)
With ActiveSheet.Range("A:A") 'searches all of column A
Set RNG = .Find(What:=FindString, _
After:=.Cells(.Cells.Count), _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)

'Actions if the key is present in column A of the Changes_Database (So a change request was previously made for the key and it already has a row in the "Changes" sheet)
If Not RNG Is Nothing Then

'Calls module 13 to set the date and time of the requested change in the "Changes" sheet
Call TimeStamp

'Calls module 8 to send over the requested changes to the "Changes" sheet
Call SendChanges

On Error Resume Next

'Protects the Changes_Database
ActiveSheet.Protect "Swarf"

'////////////////////////////////////////////////////////////////////////////

'Actions if the key DOES NOT exist in column A of the Changes_Database


Else

'Module 14: Adds a new row with the key to the Changes_Database
Call NewPart2

'Calls module 13 to set the date and time of the requested change in the "Changes" sheet
Call TimeStamp

On Error Resume Next

'Calls module 8 to send over the requested changes to the "Changes" sheet
Call SendChanges

End If

Else

'Module 7:  Adds a new row with the key to the MAIN Database
Call NewPart

'Module 14: Adds a new row with the key to the Changes_Database
Call NewPart2

'Module 13: to set the date and time of the requested change in the "Changes" sheet
Call TimeStamp

'Module 10: Fills in the date and time the key was created for the "HE 171" sheet
Call TimeStamp2

On Error Resume Next

'Calls module 8 to send over the requested changes to the "Changes" sheet
Call SendChanges

End If

End With

'Actions if "No" is clicked when the "Confirm Changes" button is clicked on the Operator Sheet
Else

'''''''If nConfirmation = vbNo Then


'Protects Changes_Database (as it was activated after the Main Database and is therefore the active contents and saves/closes it
Changes.Activate
ActiveSheet.Protect "Swarf"
ActiveWorkbook.Save
ActiveWorkbook.Close SaveChanges:=True

'Sets Main Database as active contents to protect it, save it and close it
HE171.Activate
ActiveSheet.Protect "Swarf"
ActiveWorkbook.Save
ActiveWorkbook.Close SaveChanges:=True

'Protects Operator Sheet and saves it
Sheet1.Protect "Swarf"
'Workbook.Close SaveChanges:=True

End If

End Sub

''''

Aucun commentaire:

Enregistrer un commentaire