Solved

reference current directory

Posted on 2011-09-05
2
272 Views
Last Modified: 2012-06-22
Hello. I kindly received the macro below. I would like to change it so that it references the current directory where the starting file is located. This macro creates a bunch of new files based on the starting file and I want them all to be in the same directory as the starting file, instead of hard coding a directory in the macro. Can anyone suggest the solution to this?
The directory is currently hard coded as
"C:\Users\Bill\Desktop\Analysis\n-1AttrSelOneR\
Thank you.
Sub decrementrows()
    Application.DisplayAlerts = False
    lastrow = Range("A" & Rows.Count).End(xlUp).Row
    For I = lastrow To lastrow - 158 Step -1
        Cells(I, 1).EntireRow.Delete
        fname = "C:\Users\Bill\Desktop\Sony Analysis\n-1AttrSelOneR\StartingFile -" & lastrow - I + 1 & ".csv"
        ActiveWorkbook.SaveAs Filename:=fname, FileFormat:=xlCSV
    Next I
    Application.DisplayAlerts = True
End Sub

Open in new window

0
Comment
Question by:onyourmark
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 6

Accepted Solution

by:
TinTombStone earned 500 total points
ID: 36482747

This assumes that the starting file is the active workbook






Sub decrementrows()
Dim curDir As String
curDir = ActiveWorkbook.Path
    Application.DisplayAlerts = False
    lastrow = Range("A" & Rows.Count).End(xlUp).Row
    For I = lastrow To lastrow - 158 Step -1
        Cells(I, 1).EntireRow.Delete
        fname = curDir & "\StartingFile -" & lastrow - I + 1 & ".csv"
        ActiveWorkbook.SaveAs Filename:=fname, FileFormat:=xlCSV
    Next I
    Application.DisplayAlerts = True
End Sub


MsgBox curDir

Open in new window

0
 

Author Closing Comment

by:onyourmark
ID: 36482790
Great thanks.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question