How to make a simple conditional macro ?¿

Joke

Board Regular
Joined
Jan 21, 2004
Messages
171
Yes thanks to this amazing Forum I{m slowly getting into writing Macros but apparently not fast enough. Do need help with this one.

I have a macro that copies some cells in to protected excel database. But now I want to make the macro conditional that is

If there range named CONTROL contains data the saving macro can be executed but if these cells are blank I need an error message and go back to the named cells.

This is how far I that is despite using many example form this Forum i cant get the conditional ...............

I{m sure it is not something difficult, thanks in advance to the expert that can spare some minutes to help me out on this!

Joke

Sub Savedata()
'IF the cells in range D113:F113 (named "control") have a value
other data will be saved in separate file if not a message box will be displayed and empty cells will be selected


Dim ActiveSh As Worksheet
Dim OpenedWb As Workbook
Dim strFname As String
strFname = Range("ubicacion").value
Set ActiveSh = ActiveSheet
Set OpenedWb = Workbooks.Open(Filename:=strFname)
With OpenedWb
.ActiveSheet.Unprotect Password:="x"
.ActiveSheet.Range("A2").EntireRow.Insert
.ActiveSheet.Range("a2:M2").value = ActiveSh.Range("guardar1").value
.ActiveSheet.Protect Password:="x"
.Close True
End With
Application.Goto REference:="comienzo2"

Else
MsgBox "You have to fill the data before saving"
Style = vbOkYes
Application.Goto REference:="control"
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.

Chitosunday

Well-known Member
Joined
Jul 14, 2003
Messages
1,017
Use counta function in your condition like this:

If WorksheetFunction.CountA(Range("d113:f113")) Then
ActiveWorkbook.SaveAs "c:\my documents\file1.xls"
Else
MsgBox "No Data to Save"
End If
 

Joke

Board Regular
Joined
Jan 21, 2004
Messages
171
excellent works perfect thanks a million for the advice
joke
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,152,332
Messages
5,769,470
Members
425,552
Latest member
learnerrr

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
Top