Solved

reference current directory

Posted on 2011-09-05
2
263 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

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