Block If without End If

TimmyTime

New Member
Joined
Sep 18, 2023
Messages
6
Office Version
  1. 365
  2. 2021
Platform
  1. Windows
Hey all,

I'm new to vba and I've successfully done a few macros to simplify some processes at work. However currently I'm struggling to create one for this scenario as it gives a "Block If without End If" error;

VBA Code:
Sub SAFESections()
Dim FileToOpen As Variant
Dim OpenBook As Workbook

Application.ScreenUpdating = False
FileToOpen = Application.GetOpenFilename(Title:="Browse for your FIle & Import Range", FileFilter:="Excel Files (*.xls*), *xls*")
If FileToOpen <> False Then

Set OpenBook = Application.Workbooks.Open(FileToOpen)
    Range("EU2").Select
    ActiveCell.FormulaR1C1 = _
        "=CONCAT(RIGHT(RC[-146],LEN(RC[-146])-FIND("" "",RC[-146])),"", "",LEFT(RC[-146],FIND("" "",RC[-146])-1))"
    Range("EU2").Select
    Selection.AutoFill Destination:=Range("EU2:EU1244")
    Range("EU2:EU1244").Select
   
OpenbookLastRow = OpenBook.Sheets(1).Range("EU2" & Rows.Count).End(xlUp).Row
MasterBookLastRow = ThisWorkbook.Worksheets("NSW Data").Range("C4" & Rows.Count).End(xlUp).Row
    For r = 2 To OpenbookLastRow
    For m = 4 To MasterBookLastRow
    If OpenBook.Sheets(1).Range("EU2" & r).value = ThisWorkbook.Worksheets("NSW Data").Range("C4" & r).value Then
   
        OpenBook.Sheets(1).Range("AQ", "BC", "BN", "BX", "CJ", "CT", "DI", "DT", "EJ").Copy
        ThisWorkbook.Worksheets("NSW Data").Activate
        lastRowRpt = ThisWorkbook.Worksheets("NSW Data").Range("O4" & Rows.Count).End(xlUp).Row
        ThisWorkbook.Worksheets("NSW Data").Range("O4" & lastRowRpt + 1).Select
       
        ActiveSheet.Paste
   
    Else
End If
Next m
Next r


End Sub

The aim of this code is to open a file which will sort the colomn of names into "lastname, firstname" and then compare that to the source document. If the names match, 9 cells will be copied then pasted into a targeted area of the source document. It will then repeat till the end of the target document. Any missing names will be not inputted.

I'm not sure what I did wrong, or what else I need to add. Could I get some guidance?
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
Omg thanks! The code executes now however I get a runtime error of 1004 on this line;

OpenbookLastRow = OpenBook.Sheets(1).Range("EU2" & Rows.Count).End(xlUp).Row
MasterBookLastRow = ThisWorkbook.Worksheets("NSW Data").Range("C4" & Rows.Count).End(xlUp).Row

I'm assuming the line after is wrong too, I'm trying to make the purpose of those lines to define the ranges that will be used to compare names
 
Upvote 0
Lose the "2"
change this:
Rich (BB code):
OpenbookLastRow = OpenBook.Sheets(1).Range("EU2" & Rows.Count).End(xlUp).Row

to this:
Rich (BB code):
OpenbookLastRow = OpenBook.Sheets(1).Range("EU" & Rows.Count).End(xlUp).Row
 
Upvote 0
I've changed it to remove the 2 and part of that code works now, however the next part where it copies 9 cells shows up a 450 runtime error:

OpenBook.Sheets(1).Range("AQ", "BC", "BN", "BX", "CJ", "CT", "DI", "DT", "EJ").Copy

I tried fixing it myself using the code below to refer the name referenced in the Target book but it still doesn't work.

OpenBook.Sheets(1).Range("AQ" & r, "BC" & r, "BN" & r, "BX" & r, "CJ" & r, "CT" & r, "DI" & r, "DT" & r, "EJ" & r).Copy

What on earth am i doing wrong?
 
Upvote 0
You're missing a few ampersands, try this (untested)

OpenBook.Sheets(1).Range("AQ" & r & ",BC" & r & ",BN" & r & ",BX" & r & ",CJ" & r & ",CT" & r & ",DI" & r & ",DT" & r & ",EJ" & r).Copy
 
Upvote 0
The code works now! But when I excecute it looks like it only copies the first row of 9 sections and pastes it on the entire table on the master..

I want it to go through name by name and then get it to copy 9 sections to the master according to matching names.

Do i have to add another line that gets the module to look at the next name etc.?
 
Upvote 0
It's difficult to visualise what your data looks like. Could you provide a copy of the sheets involved using the XL2BB - Excel Range to BBCode, or alternatively share your file via Google Drive, Dropbox or similar file sharing platform - as well as a simple explanation of what iit is exactly that you're trying to do. A before and after samplle would be most useful.
 
Upvote 0
Master File. Note: List of names are in column C4 onwards, not shown in this picture
1695103165096.png


Source File. Where the data will be drawn from. Sections that I want data drawn out from are the following columns on row 2: AQ, BC, BN, BX, CJ, CT, DI, DT, EJ (9 sections)
List of names and one of the rows are provided as an example to compare with the master file.
1695103393284.png



I've found a simpler way by using formulas, however I'm still stuck on a couple of things;

How can i change the formula so it uses the file that I have opened up instead of having to use a specific file name? The current formula I'm using is:

=IF(SUMIF('[Document.xlsm]SAFE Report (9)'!$EU$2:$EU$10000,C4,'[Document.xlsm]SAFE Report (9)'!$AQ$2:$AQ$10000)>1,1,SUMIF('[Document.xlsm]SAFE Report (9)'!$EU$2:$EU$10000,C4,'[Document.xlsm]SAFE Report (9)'!$AQ$2:$AQ$10000))

How can I make the autofill dynamic when entries get added or deleted?
I tried to recreate it after being successful with reversing names in the source file, but for some reason it doesn't work in the master.

VBA Code:
Sub SAFERecord()
Dim FileToOpen As Variant
Dim OpenBook As Workbook
Dim lastRow As Long

Application.ScreenUpdating = False
FileToOpen = Application.GetOpenFilename(Title:="Browse for your File & Import Range", FileFilter:="Excel Files (*.xls*), *xls*")
If FileToOpen <> False Then

Set OpenBook = Application.Workbooks.Open(FileToOpen)
    Range("EU2").Select
    ActiveCell.FormulaR1C1 = _
        "=CONCAT(RIGHT(RC[-146],LEN(RC[-146])-FIND("" "",RC[-146])),"", "",LEFT(RC[-146],FIND("" "",RC[-146])-1))"
    Range("EU2").Select
    Selection.autofill Destination:=Range("EU2:EU" & Range("E" & Rows.Count).End(xlUp).Row)
    Range(Selection, Selection.End(xlDown)).Select
    
    'NSW
        ThisWorkbook.Worksheets("NSW Data").Range("O4").Formula = "=IF(SUMIF('[NGFE Report 11092023.xlsm]SAFE Report (9)'!$EU$2:$EU$10000,C4,'[NGFE Report 11092023.xlsm]SAFE Report (9)'!$AQ$2:$AQ$10000)>1,1,SUMIF('[NGFE Report 11092023.xlsm]SAFE Report (9)'!$EU$2:$EU$10000,C4,'[NGFE Report 11092023.xlsm]SAFE Report (9)'!$AQ$2:$AQ$10000))"
        ThisWorkbook.Worksheets("NSW Data").Range("P4").Formula = "=IF(SUMIF('[NGFE Report 11092023.xlsm]SAFE Report (9)'!$EU$2:$EU$10000,C4,'[NGFE Report 11092023.xlsm]SAFE Report (9)'!$BC$2:$BC$10000)>1,1,SUMIF('[NGFE Report 11092023.xlsm]SAFE Report (9)'!$EU$2:$EU$10000,C4,'[NGFE Report 11092023.xlsm]SAFE Report (9)'!$BC$2:$BC$10000))"
        ThisWorkbook.Worksheets("NSW Data").Range("Q4").Formula = "=IF(SUMIF('[NGFE Report 11092023.xlsm]SAFE Report (9)'!$EU$2:$EU$10000,C4,'[NGFE Report 11092023.xlsm]SAFE Report (9)'!$BN$2:$BN$10000)>1,1,SUMIF('[NGFE Report 11092023.xlsm]SAFE Report (9)'!$EU$2:$EU$10000,C4,'[NGFE Report 11092023.xlsm]SAFE Report (9)'!$BN$2:$BN$10000))"
        ThisWorkbook.Worksheets("NSW Data").Range("R4").Formula = "=IF(SUMIF('[NGFE Report 11092023.xlsm]SAFE Report (9)'!$EU$2:$EU$10000,C4,'[NGFE Report 11092023.xlsm]SAFE Report (9)'!$BX$2:$BX$10000)>1,1,SUMIF('[NGFE Report 11092023.xlsm]SAFE Report (9)'!$EU$2:$EU$10000,C4,'[NGFE Report 11092023.xlsm]SAFE Report (9)'!$BX$2:$BX$10000))"
        ThisWorkbook.Worksheets("NSW Data").Range("S4").Formula = "=IF(SUMIF('[NGFE Report 11092023.xlsm]SAFE Report (9)'!$EU$2:$EU$10000,C4,'[NGFE Report 11092023.xlsm]SAFE Report (9)'!$CJ$2:$CJ$10000)>1,1,SUMIF('[NGFE Report 11092023.xlsm]SAFE Report (9)'!$EU$2:$EU$10000,C4,'[NGFE Report 11092023.xlsm]SAFE Report (9)'!$CJ$2:$CJ$10000))"
        ThisWorkbook.Worksheets("NSW Data").Range("T4").Formula = "=IF(SUMIF('[NGFE Report 11092023.xlsm]SAFE Report (9)'!$EU$2:$EU$10000,C4,'[NGFE Report 11092023.xlsm]SAFE Report (9)'!$CT$2:$CT$10000)>1,1,SUMIF('[NGFE Report 11092023.xlsm]SAFE Report (9)'!$EU$2:$EU$10000,C4,'[NGFE Report 11092023.xlsm]SAFE Report (9)'!$CT$2:$CT$10000))"
        ThisWorkbook.Worksheets("NSW Data").Range("U4").Formula = "=IF(SUMIF('[NGFE Report 11092023.xlsm]SAFE Report (9)'!$EU$2:$EU$10000,C4,'[NGFE Report 11092023.xlsm]SAFE Report (9)'!$DI$2:$DI$10000)>1,1,SUMIF('[NGFE Report 11092023.xlsm]SAFE Report (9)'!$EU$2:$EU$10000,C4,'[NGFE Report 11092023.xlsm]SAFE Report (9)'!$DI$2:$DI$10000))"
        ThisWorkbook.Worksheets("NSW Data").Range("V4").Formula = "=IF(SUMIF('[NGFE Report 11092023.xlsm]SAFE Report (9)'!$EU$2:$EU$10000,C4,'[NGFE Report 11092023.xlsm]SAFE Report (9)'!$DT$2:$DT$10000)>1,1,SUMIF('[NGFE Report 11092023.xlsm]SAFE Report (9)'!$EU$2:$EU$10000,C4,'[NGFE Report 11092023.xlsm]SAFE Report (9)'!$DT$2:$DT$10000))"
        ThisWorkbook.Worksheets("NSW Data").Range("W4").Formula = "=IF(SUMIF('[NGFE Report 11092023.xlsm]SAFE Report (9)'!$EU$2:$EU$10000,C4,'[NGFE Report 11092023.xlsm]SAFE Report (9)'!$EJ$2:$EJ$10000)>1,1,SUMIF('[NGFE Report 11092023.xlsm]SAFE Report (9)'!$EU$2:$EU$10000,C4,'[NGFE Report 11092023.xlsm]SAFE Report (9)'!$EJ$2:$EJ$10000))"
    
Range("O4:W4").Select
    Selection.autofill Destination:=Range("O4:W" & Range("C" & Rows.Count).End(xlUp).Row)
    Range(Selection, Selection.End(xlDown)).Select

    End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,173
Members
453,021
Latest member
Justyna P

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