IF Statement in Macro

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a macro that opens workbooks. The paths for the workbooks are
contained in various cells, say Cell A1 contains the path for Workbook1, Cell
A2 contains the path to Workbook2, and Cell A3 contains the path to
Workbook3, and so on, and the macro opens the files that are specified in
each of the cells. However, some of the cells on which the macro operates
are blank, which creates an error.

I’m trying to use an IF statement to have the macro skip over the blank
cells, and thus avoid an error, but have had no luck so far. My co-worker
suggested using the following code:

If Range("A1").Value = "" then Goto 2
[Code to open file contained in Cell A2]
End IF
1:
[Macto to open file contained in A1]
End Sub

However, this code generates the following error :"End if without block IF"

Any advice you could give me on overcoming the problem would be greatly
appreciated?

Magnivy
 
try this instead where the list is

c:\yourdirectory\yourfile
blank
c:\yourdirectory\yourfile1
blank

Sub openworkbooksinlist()
On Error Resume Next
For Each c In Range("a1:a4")
Workbooks.Open Filename:=c & ".xls"
Next c
End Sub
 
Don,

Got It! Thanks a lot for your help!

Magnivy

Don Guillett said:
try this instead where the list is

c:\yourdirectory\yourfile
blank
c:\yourdirectory\yourfile1
blank

Sub openworkbooksinlist()
On Error Resume Next
For Each c In Range("a1:a4")
Workbooks.Open Filename:=c & ".xls"
Next c
End Sub

--
Don Guillett
SalesAid Software
(e-mail address removed)
Magnivy said:
I have a macro that opens workbooks. The paths for the workbooks are
contained in various cells, say Cell A1 contains the path for Workbook1,
Cell
A2 contains the path to Workbook2, and Cell A3 contains the path to
Workbook3, and so on, and the macro opens the files that are specified in
each of the cells. However, some of the cells on which the macro operates
are blank, which creates an error.

I'm trying to use an IF statement to have the macro skip over the blank
cells, and thus avoid an error, but have had no luck so far. My co-worker
suggested using the following code:

If Range("A1").Value = "" then Goto 2
[Code to open file contained in Cell A2]
End IF
1:
[Macto to open file contained in A1]
End Sub

However, this code generates the following error :"End if without block
IF"

Any advice you could give me on overcoming the problem would be greatly
appreciated?

Magnivy
 
glad to help

--
Don Guillett
SalesAid Software
(e-mail address removed)
Magnivy said:
Don,

Got It! Thanks a lot for your help!

Magnivy

Don Guillett said:
try this instead where the list is

c:\yourdirectory\yourfile
blank
c:\yourdirectory\yourfile1
blank

Sub openworkbooksinlist()
On Error Resume Next
For Each c In Range("a1:a4")
Workbooks.Open Filename:=c & ".xls"
Next c
End Sub

--
Don Guillett
SalesAid Software
(e-mail address removed)
Magnivy said:
I have a macro that opens workbooks. The paths for the workbooks are
contained in various cells, say Cell A1 contains the path for
Workbook1,
Cell
A2 contains the path to Workbook2, and Cell A3 contains the path to
Workbook3, and so on, and the macro opens the files that are specified
in
each of the cells. However, some of the cells on which the macro
operates
are blank, which creates an error.

I'm trying to use an IF statement to have the macro skip over the blank
cells, and thus avoid an error, but have had no luck so far. My
co-worker
suggested using the following code:

If Range("A1").Value = "" then Goto 2
[Code to open file contained in Cell A2]
End IF
1:
[Macto to open file contained in A1]
End Sub

However, this code generates the following error :"End if without block
IF"

Any advice you could give me on overcoming the problem would be greatly
appreciated?

Magnivy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top