PC Review


Reply
Thread Tools Rate Thread

how can I make a macro prompt for a portion of a file name?

 
 
=?Utf-8?B?RGF2aWQgUGVsaXp6YXJpLCBJUyBNYW5hZ2Vy?=
Guest
Posts: n/a
 
      29th Jun 2007
I am trying to modify the macro below so that it will prompt the user for the
4 digit year and two digit month, the rest of the path and filename will
remain static... The users will need to run this on a monthly basis, we
don't want them changing the path or "Costs_Plant_HWM.txt" part of the
statement. Help?
Dim FileNum As Long, i As Long
Dim y As Variant
Dim lastrow As Integer
Range("a1").Select
Selection.End(xlDown).Select
lastrow = ActiveCell.Row
Range("a1").Select
Selection.End(xlToRight).Select
last_col = ActiveCell.Column
FileNum = FreeFile
Open "c:\200706_Costs_Plant_HCM.txt" For Append As #FileNum
For i = 1 To lastrow
With Application.WorksheetFunction
y = .Transpose(.Transpose(Range(Cells(i, 1), Cells(i, last_col))))
End With
Print #FileNum, "~" + Join(y, "~") + "~"
Next
Close #FileNum
 
Reply With Quote
 
 
 
 
=?Utf-8?B?QUtwaGlkZWx0?=
Guest
Posts: n/a
 
      29th Jun 2007
Create an Input Box like something like this

Set MyInput = InputBox("Enter 4 digit year and 2 digit month")

Then in your code insert MyInput in the place that you want it to be

"David Pelizzari, IS Manager" wrote:

> I am trying to modify the macro below so that it will prompt the user for the
> 4 digit year and two digit month, the rest of the path and filename will
> remain static... The users will need to run this on a monthly basis, we
> don't want them changing the path or "Costs_Plant_HWM.txt" part of the
> statement. Help?
> Dim FileNum As Long, i As Long
> Dim y As Variant
> Dim lastrow As Integer
> Range("a1").Select
> Selection.End(xlDown).Select
> lastrow = ActiveCell.Row
> Range("a1").Select
> Selection.End(xlToRight).Select
> last_col = ActiveCell.Column
> FileNum = FreeFile
> Open "c:\200706_Costs_Plant_HCM.txt" For Append As #FileNum
> For i = 1 To lastrow
> With Application.WorksheetFunction
> y = .Transpose(.Transpose(Range(Cells(i, 1), Cells(i, last_col))))
> End With
> Print #FileNum, "~" + Join(y, "~") + "~"
> Next
> Close #FileNum

 
Reply With Quote
 
=?Utf-8?B?RGF2aWQgUGVsaXp6YXJp?=
Guest
Posts: n/a
 
      29th Jun 2007
Thanks, AKphidelt, however, much of the code below is "borrowed", I can
figure out the input, but how would I craft the Open line?

"AKphidelt" wrote:

> Create an Input Box like something like this
>
> Set MyInput = InputBox("Enter 4 digit year and 2 digit month")
>
> Then in your code insert MyInput in the place that you want it to be
>
> "David Pelizzari, IS Manager" wrote:
>
> > I am trying to modify the macro below so that it will prompt the user for the
> > 4 digit year and two digit month, the rest of the path and filename will
> > remain static... The users will need to run this on a monthly basis, we
> > don't want them changing the path or "Costs_Plant_HWM.txt" part of the
> > statement. Help?
> > Dim FileNum As Long, i As Long
> > Dim y As Variant
> > Dim lastrow As Integer
> > Range("a1").Select
> > Selection.End(xlDown).Select
> > lastrow = ActiveCell.Row
> > Range("a1").Select
> > Selection.End(xlToRight).Select
> > last_col = ActiveCell.Column
> > FileNum = FreeFile
> > Open "c:\200706_Costs_Plant_HCM.txt" For Append As #FileNum
> > For i = 1 To lastrow
> > With Application.WorksheetFunction
> > y = .Transpose(.Transpose(Range(Cells(i, 1), Cells(i, last_col))))
> > End With
> > Print #FileNum, "~" + Join(y, "~") + "~"
> > Next
> > Close #FileNum

 
Reply With Quote
 
Rick Rothstein \(MVP - VB\)
Guest
Posts: n/a
 
      29th Jun 2007
Insert the following code immediately after the FileNum=FreeFile
statement...

YearMonth = Trim$(InputBox("Enter the year followed by the " & _
"2-digit month", "Get New YearMonth"))
Loop Until YearMonth Like "######" Or YearMonth = ""
If YearMonth = "" Then Exit Sub
' You should do some error checking here to make
' sure the entry has a year part that makes sense
' to your process and that the month part is a
' number less than 13.

and change the Open statement to this...

Open YearMonth & "_Costs_Plant_HCM.txt" For Append As #FileNum

Remember to Dim the YearMonth variable as a String. The code as written will
keep popping the InputBox in your users face until he/she enters a 6-digit
number or hits OK without entering anything. If he/she enters nothing, the
macro (an assumed Sub, not Function) will end. I showed a section where you
should put some kind of error checking so you can filter out non-sense
entries (a year that doesn't make sense or a month number not between 01 and
12).

Rick


"David Pelizzari, IS Manager"
<(E-Mail Removed)> wrote in message
news:F2F8BBF4-6805-4A4A-A0C5-(E-Mail Removed)...
>I am trying to modify the macro below so that it will prompt the user for
>the
> 4 digit year and two digit month, the rest of the path and filename will
> remain static... The users will need to run this on a monthly basis, we
> don't want them changing the path or "Costs_Plant_HWM.txt" part of the
> statement. Help?
> Dim FileNum As Long, i As Long
> Dim y As Variant
> Dim lastrow As Integer
> Range("a1").Select
> Selection.End(xlDown).Select
> lastrow = ActiveCell.Row
> Range("a1").Select
> Selection.End(xlToRight).Select
> last_col = ActiveCell.Column
> FileNum = FreeFile
> Open "c:\200706_Costs_Plant_HCM.txt" For Append As #FileNum
> For i = 1 To lastrow
> With Application.WorksheetFunction
> y = .Transpose(.Transpose(Range(Cells(i, 1), Cells(i, last_col))))
> End With
> Print #FileNum, "~" + Join(y, "~") + "~"
> Next
> Close #FileNum


 
Reply With Quote
 
=?Utf-8?B?RGF2aWQgUGVsaXp6YXJp?=
Guest
Posts: n/a
 
      29th Jun 2007
Thanks, Rick, that did it! I left out the error checking, I will let the
bean counters figure it out, they can always do it again.

"Rick Rothstein (MVP - VB)" wrote:

> Insert the following code immediately after the FileNum=FreeFile
> statement...
>
> YearMonth = Trim$(InputBox("Enter the year followed by the " & _
> "2-digit month", "Get New YearMonth"))
> Loop Until YearMonth Like "######" Or YearMonth = ""
> If YearMonth = "" Then Exit Sub
> ' You should do some error checking here to make
> ' sure the entry has a year part that makes sense
> ' to your process and that the month part is a
> ' number less than 13.
>
> and change the Open statement to this...
>
> Open YearMonth & "_Costs_Plant_HCM.txt" For Append As #FileNum
>
> Remember to Dim the YearMonth variable as a String. The code as written will
> keep popping the InputBox in your users face until he/she enters a 6-digit
> number or hits OK without entering anything. If he/she enters nothing, the
> macro (an assumed Sub, not Function) will end. I showed a section where you
> should put some kind of error checking so you can filter out non-sense
> entries (a year that doesn't make sense or a month number not between 01 and
> 12).
>
> Rick
>
>
> "David Pelizzari, IS Manager"
> <(E-Mail Removed)> wrote in message
> news:F2F8BBF4-6805-4A4A-A0C5-(E-Mail Removed)...
> >I am trying to modify the macro below so that it will prompt the user for
> >the
> > 4 digit year and two digit month, the rest of the path and filename will
> > remain static... The users will need to run this on a monthly basis, we
> > don't want them changing the path or "Costs_Plant_HWM.txt" part of the
> > statement. Help?
> > Dim FileNum As Long, i As Long
> > Dim y As Variant
> > Dim lastrow As Integer
> > Range("a1").Select
> > Selection.End(xlDown).Select
> > lastrow = ActiveCell.Row
> > Range("a1").Select
> > Selection.End(xlToRight).Select
> > last_col = ActiveCell.Column
> > FileNum = FreeFile
> > Open "c:\200706_Costs_Plant_HCM.txt" For Append As #FileNum
> > For i = 1 To lastrow
> > With Application.WorksheetFunction
> > y = .Transpose(.Transpose(Range(Cells(i, 1), Cells(i, last_col))))
> > End With
> > Print #FileNum, "~" + Join(y, "~") + "~"
> > Next
> > Close #FileNum

>
>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Prompt for a file name in a script macro Phil Hibbs Microsoft Access Macros 0 28th Oct 2010 02:03 PM
RE: Prompt for file name in a macro =?Utf-8?B?TG91aXN2aWxsZSBDYXJkaW5hbHM=?= Microsoft Excel Misc 5 19th Apr 2005 02:39 PM
Re: Prompt for file name in a macro Dave Peterson Microsoft Excel Misc 1 15th Apr 2005 03:51 PM
How do I make a macro prompt for user input =?Utf-8?B?Q2hhZA==?= Microsoft Excel Misc 1 23rd Nov 2004 04:40 PM
Getting excel macro to prompt for a file NealUK Microsoft Excel Discussion 5 19th Dec 2003 01:02 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 08:47 PM.