Question

Macro not working

Asked by: rsernowski

Hello
I have a macro that is not working as expected.The macro is supposed to take, the original file name and add a sequence number to the existing filename. Is is supposed to sequence up to 3 versions. After the macro checks the file name , it emails it as an attachment , then reformats the spreadsheet to a different format then saves it to my desktop. The first first sequence works great, giving the file its sequence name. saves it to a shared folder, emails it and reformats it and saves it to my desktop. So when I open the first sequence file, and run the macro on it , it just sends an email with no attachment and doesn't save the file no place with the new sequenced name.

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2009-10-06 at 07:54:53ID24789116
Tags

Microsoft excel macro file name

Topics

Spreadsheet Software

,

Microsoft Excel Spreadsheet Software

Participating Experts
1
Points
500
Comments
25

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. Creating a macros that will update a spreadsheet
    I have been trying to create a macro that will save an excel spreadsheet under a different name, i can't get it to work once the new file is created. Basically i want to create new spreadsheet from an old one that gets updated periodically from the old sheet. Macros that i ...
  2. MACRO TO MOVE OPEN SPREADSHEET TO NEW LOCATION
    I would like to move an open spreadsheet to a different location on my lan. I have a print macro that sends certain pages to different printers which works fine. I would like the macro to then move the spreadsheet from its current location to a different folder called say &qu...
  3. Macro
    I need to write a macro. I have this report exported to excel format and saved in a folder. I want to write a macro to get that excel file from that folder and have to sort it by partnumber(a column in the excel file) and subtotal that column by part number and also subtotal ...
  4. Running a sequence of macros
    I'm trying to run the three scripts below in sequence. I don't even know if what I'm trying to do will work since I can't get the macros to run in order. I don't really understand the function of macros vs modules, so that might be a problem. I'm trying to create an .xls em...

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: lorendaPosted on 2009-10-06 at 08:01:48ID: 25505827

Can you also post the code behind the

ExecuteExcel4Macro "PRINT(1,,,1,,FALSE,,,,,,2,,,TRUE,,FALSE)"

   

 

by: lorendaPosted on 2009-10-06 at 08:04:43ID: 25505849

You are only changing the file name if the current file name is 15 characters in length. Can you confirm that the file name is this long?

 

by: rsernowskiPosted on 2009-10-06 at 09:21:18ID: 25506775

IHi Lorenda

there is no other code.  I noted out the reformat code so I can trouble shoot. Yes about the file name. The orginal will be:
deal (plus date) so deal20091006
the first rev will be deal20091006001
the 2nd will be 20091006002
the 3rd 20091006003

The file naming structure will always be the same

 

by: rsernowskiPosted on 2009-10-06 at 09:24:24ID: 25506807

Sorry the original file will be dealmac20091006
the first rev will be dealmac20091006001
the 2nd will be dealmac20091006002
the 3rd dealmac20091006003

 

by: rsernowskiPosted on 2009-10-06 at 09:42:28ID: 25507027

Hi Lorenda

Ok I have got it it checks for lenght 15 , but since it is adding length on the second files it should check for length 18!!

 

by: lorendaPosted on 2009-10-06 at 10:00:39ID: 25507226

I'm walking through the code now - I will post issues as I find them

.Body = "Here is the deal CANCEL AND CORRECT original file name attached" currentFileName

Syntax error - change this line to

.Body = "Here is the deal CANCEL AND CORRECT original file name attached" & currentFileName

 

by: lorendaPosted on 2009-10-06 at 10:05:08ID: 25507273

And yes - your post above is the only other issue I see. If you change

ElseIf Len(currentFileName) = 15 Then

to

ElseIf Len(currentFileName) = 18 Then

you should be good.

 

by: rsernowskiPosted on 2009-10-06 at 10:41:32ID: 25507649

Hi Lorena
Ok on other thing. As you can see the fist part of the code saves the file with a new name. I then reformat the file and give it a little bit of a change,I was able to do this before by using dealmac" & format(Date, "yyyymmdd") & ".xls", or deal " & format(Date, "yyyymmdd") & ".csv", for the seconf format. Not that I am incrementing, is it possible to reuse the file name under from the first saving. So the first file will be named dealmac20091006001. I save the file to the shared drive, send it then reformat it. How can I easily tell it to save the file deal20091006001or --02 or 003 ?

 

by: lorendaPosted on 2009-10-06 at 11:14:58ID: 25508017

Are you saying that if you are running this on a file called deal20091006002.xls, you want to be able to save it as deal20091006002.csv?

 

by: rsernowskiPosted on 2009-10-06 at 11:27:44ID: 25508158

Hi Lorenda

Sort of , the first part saves the code saves the file  as dealmac20091006 + the increment 001,002 or 003 as format xls. the 2nd part of the macro, reformats the file, adding columns etc. the I want to save it as deal20091006 plus the increment.
I am using this
ActiveWorkbook.SaveAs FileName:="C:\Documents and Settings\rsernowski.R-SERNOWSKI\Desktop\deal" & newFileName & ".csv", FileFormat:=xlCSV
that is not working right as it is duplicating the word deal. Actually I need it to remove the word MAC. Hope this makes sense

 

by: lorendaPosted on 2009-10-06 at 11:41:35ID: 25508316

If I am understanding, this should work. I've done it in steps so you can more easily follow what I'm doing

Dim a As String
 
a = Left(ActiveWorkbook.FullName, Len(ActiveWorkbook.FullName) - 4) 'removes the .xls
a = Replace(a, "mac", "") ' finds and removes the mac
a = a & ".csv" ' adds the csv

                                              
1:
2:
3:
4:
5:

Select allOpen in new window

 

by: rsernowskiPosted on 2009-10-06 at 11:52:35ID: 25508444

ActiveWorkbook.SaveAs FileName:="C:\Documents and Settings\rsernowski.R-SERNOWSKI\Desktop\deal" & newFileName & ".csv", FileFormat:=xlCSV

Should I put your code after or before this?

 

by: lorendaPosted on 2009-10-06 at 12:00:16ID: 25508520

before - and change the line to

ActiveWorkbook.SaveAs FileName:="C:\Documents and Settings\rsernowski.R-SERNOWSKI\Desktop\" & a ,FileFormat:=xlCSV

 

by: rsernowskiPosted on 2009-10-06 at 12:22:33ID: 25508791

Hi,

I am getting a runtime error 1004, on
ActiveWorkbook.SaveAs FileName:="C:\Documents and Settings\rsernowski.R-SERNOWSKI\Desktop\" & a ,FileFormat:=xlCSV
on this line
make sure the folder  exists,

 

by: lorendaPosted on 2009-10-06 at 12:35:54ID: 25508947

Oops - this was my fault. The Full Name should just be Name

Dim a As String
 
a = Left(ActiveWorkbook.Name, Len(ActiveWorkbook.Name) - 4) 'removes the .xls
a = Replace(a, "mac", "") ' finds and removes the mac
a = a & ".csv" ' adds the csv

ActiveWorkbook.SaveAs FileName:="C:\Documents and Settings\rsernowski.R-SERNOWSKI\Desktop\" & a ,FileFormat:=xlCSV

 

by: rsernowskiPosted on 2009-10-07 at 08:22:24ID: 25516515

Hi Lorenda
works almost to a tee, except the file is named deal20091006001.xls.csv. It is saved as CSV format but has the XLS extension
Thanks for your help

 

by: lorendaPosted on 2009-10-07 at 08:58:19ID: 25516877

Can you post your full final code here? Are you missing this line?

a = Left(ActiveWorkbook.Name, Len(ActiveWorkbook.Name) - 4) 'removes the .xls

 

by: rsernowskiPosted on 2009-10-07 at 09:10:59ID: 25516994

Hi,

The code is there on that line
here is is:
'
' Mac_Trades_Correct Macro
'
    Dim FileName As String
    Dim wb1 As Workbook
    Dim Row As Long
    Dim OutApp As Object
    Dim OutMail As Object
    Dim Today As Date
    Dim ie As Object
    Dim ErrorCount As String
    Dim currentFileName As String
    Dim newFileName As String
    Dim msgReply As Long
    Dim incrementNumber As Integer
    Dim a As String
   

 
currentFileName = Left(ActiveWorkbook.Name, Len(ActiveWorkbook.Name) - 4)
 
If Len(currentFileName) = 15 Then
    'Changes have not yet been saved
   
    newFileName = currentFileName & "001"
   
    Select Case MsgBox("The file will be saved as " & newFileName & ".xls", _
    vbOKCancel, "Save As Filename")
       
    Case vbOK
        'Save the file
        Application.EnableEvents = False
'        ActiveWorkbook.SaveAs Filename:=ActiveWorkbook.Path & "\" _
'        & newFileName & ".xlsm"
        ActiveWorkbook.SaveAs FileName:="\\Share_folder\Common\tickets\" _
        & newFileName & ".xls", FileFormat:=xlExcel8
        Application.EnableEvents = True
    Case vbCancel
        'Do nothing (exit the procedure).
        Exit Sub
    End Select
ElseIf Len(currentFileName) = 18 Then
    'Changes have already been saved at least once.
   
    incrementNumber = Right(currentFileName, 1)
 
    If incrementNumber = 1 Or incrementNumber = 2 Then
        newFileName = Left(currentFileName, Len(currentFileName) - 1) _
        & (incrementNumber + 1)
 
        Select Case MsgBox("The file will be saved as " & newFileName _
        & ".xls", vbOKCancel, "Save As Filename")
        '& ".xlsm", vbOKCancel, "Save As Filename")
 
        Case vbOK
            Application.EnableEvents = False
            ActiveWorkbook.SaveAs FileName:="\\Share_folder\Common\tickets\" _
            & newFileName & ".xls", FileFormat:=xlExcel8
            Application.EnableEvents = True
        Case vbCancel
            'Do nothing (exit the procedure).
            Exit Sub
        End Select
    Else
        MsgBox "You cannot save any new revisions to this file.", vbOKOnly
    End If
End If
 
'Cancel the native Save As dialog box.
If SaveAsUI = True Then Cancel = True
   
   
'Print the work Sheet
Set wb1 = ActiveWorkbook
ActiveSheet.PageSetup.PrintArea = ""
    With ActiveSheet.PageSetup
        .Orientation = xlLandscape
        .Draft = False
        .PaperSize = xlPaperLetter
        .Zoom = False
        .FitToPagesWide = 1
        .FitToPagesTall = False
  End With
      ExecuteExcel4Macro "PRINT(1,,,1,,FALSE,,,,,,2,,,TRUE,,FALSE)"
 
'send the trade ticket to Mackenzie'
If MsgBox("Click OK to EMail, Cancel to STOP.", vbOKCancel) = vbCancel Then Exit Sub
Set OutApp = CreateObject("Outlook.Application")
   OutApp.Session.Logon
   Set OutMail = OutApp.CreateItem(0)
   With wb1
        On Error Resume Next
        With OutMail
        '  .to = ""
          ' .CC = ""
        .BCC = "rsernowski@gmail.com"
        .Subject = " CANCEL AND CORRECT"
                     .Body = " CANCEL AND CORRECT. File name prior to this CANCEL and CORRECT  " & currentFileName
         .Attachments.Add wb1.FullName
            'wb1.FullName
            'You can add other files also like this
            '.Attachments.Add ("C:\test.txt")
            .send   'or use .Display
        End With
        On Error GoTo 0
        End With
        'ActiveWorkbook.Close SaveChanges:=False
        With Application
        .ScreenUpdating = True
        .EnableEvents = True
        End With
        MsgBox "The file was successfully sent "
        'End With
 
'Reformat the file to Mellon Format
Set wb1 = ActiveWorkbook
With wb1
'Add columns and format columns
    Columns("F:F").Hidden = False
    Columns("J:J").Hidden = False
    Columns("R:R").Hidden = False
    Columns("S:S").Hidden = False
    Columns(12).insert
    Columns(14).insert
    Columns(15).insert
    Columns(18).insert
    Columns(22).insert
    Columns(23).insert
    Columns(24).insert
    Columns(25).insert
    Columns(28).insert
    Columns(29).insert
    Columns(30).insert
    Columns(31).insert
    Columns(32).insert
    Columns(33).insert
    Columns(34).insert
    Columns(35).insert
    Columns("AB:AI").ColumnWidth = 1
    Columns("AR:AY").ColumnWidth = 1
    Columns("AL:AO").ColumnWidth = 1
    Columns("V:Y").ColumnWidth = 1
    Columns("R:R").ColumnWidth = 1
    Columns("N:O").ColumnWidth = 1
    Columns("L:L").ColumnWidth = 1
    Columns("C:C").EntireColumn.AutoFit
    Columns("U:U").EntireColumn.AutoFit
    Columns("D:E").NumberFormat = "mm/dd/yyyy"
    Columns("D:E").EntireColumn.AutoFit
    Columns("I:I").NumberFormat = "0.00000"
    Columns("H:H").NumberFormat = "0"
    Columns("I:I").EntireColumn.AutoFit
    Columns("M:M").NumberFormat = "0.00"
    Columns("O:O").NumberFormat = "0.00"
    Columns("Q:Q").NumberFormat = "0.00"
    Columns("S:S").NumberFormat = "0.00"
    Columns("T:T").EntireColumn.AutoFit
    Columns("AA:AA").NumberFormat = "General"
    Columns("L:S").EntireColumn.AutoFit
'First Delete the Header from Toogood
Rows("1:2").Delete
   End With
With ActiveSheet
   
    a = Left(ActiveWorkbook.Name, Len(ActiveWorkbook.FullName) - 4) 'removes the .xls
    a = Replace(a, "Mac", "") ' finds and removes the mac
    a = a & ".csv" ' adds the csv
    ActiveWorkbook.SaveAs FileName:="C:\Documents and Settings\rsernowski.R-SERNOWSKI\Desktop\" & a, FileFormat:=xlCSV
     'ActiveWorkbook.SaveAs FileName:="\\Share_folder\Common\Trade_tickets\Mackenzie\Blue" & format(Date, "yyyymmdd") & ".csv", FileFormat:=xlCSV
 ' wb1.Close SaveChanges:=False
'Open the webpage
 ' Set ie = CreateObject("Internetexplorer.Application")
  'ie.Visible = True
  'ie.Navigate "https://workbench.com/fileupload.do"
End With
End Sub

   
   

 

by: lorendaPosted on 2009-10-07 at 09:33:47ID: 25517247

You need to change this line


    a = Left(ActiveWorkbook.Name, Len(ActiveWorkbook.FullName) - 4) 'removes the .xls
 

to

a = Left(ActiveWorkbook.Name, Len(ActiveWorkbook.Name) - 4) 'removes the .xls

 

by: rsernowskiPosted on 2009-10-07 at 10:16:38ID: 25517661

Hi Lorenda

Very nice ,  Thank you. I will accept as solution but perhaps you can help me with the bottom part of the code
Open the webpage
 ' Set ie = CreateObject("Internetexplorer.Application")
  'ie.Visible = True
  'ie.Navigate "https://workbench.com/fileupload.do"
End With
End Sub
I have posted this question but as of yet no reply.  when the conversion to csv is done, a secure website opens up.  I put in user ID and password, and the site opens to a page where the user can browse to the desktop and locate the csv file we just saved. Works, but is it possible to save the file into memory, then when the webpage opens , the user focuses on the browse box  and presses CTRL+V to paste in the path?

 

by: lorendaPosted on 2009-10-07 at 11:31:04ID: 25518384

Hmm, not sure, I've never tried that. . .
If you were doing that, you would also have to "Pause" your code while you put your login credentials in

or

Have the VBA macro log you in

 

by: rsernowskiPosted on 2009-10-07 at 11:35:07ID: 25518410

Hi Lorenda

Ok, well there is a revolving password so the macro can't log the user in. If I can have the code copy the path,, then when the user goes to the website she just presses paste. what do you thinkl

 

by: lorendaPosted on 2009-10-07 at 11:45:47ID: 25518512

Truthfully, I think it will be more trouble than it is worth - Pausing macro code is extremely difficult. Maybe a better approach would be to automate an email that includes the path in the body of the email. She could simply cut and paste from there.

 

by: rsernowskiPosted on 2009-10-07 at 12:10:32ID: 25518771

Hi Lorenda
Good idea, but I think browse is the best at this point.
Thanks for all your help
Regards
RS

 

by: rsernowskiPosted on 2009-10-07 at 12:11:10ID: 31637737

Thanks for your help. FAst and efficient!

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...