A simple excel vba question

  • Thread starter Thread starter Deleted member 49549
  • Start date Start date
D

Deleted member 49549

Guest
Hi All.

a couple of questions for the excel vba'ers

I have a worksheet titled 'output', it only has data in column a. I'd like to save this sheet to d:/output[text in Input!B2].txt

bonus point - id like to change the value in Input!B2 through the values contained in

'System IP address'!$A$3:$A$15​

and after each change in value, save the file using the text in Input!B2

Cheers all!
 
Rapscallion said:
Hi All.

a couple of questions for the excel vba'ers

I have a worksheet titled 'output', it only has data in column a. I'd like to save this sheet to d:/output[text in Input!B2].txt

bonus point - id like to change the value in Input!B2 through the values contained in

'System IP address'!$A$3:$A$15

and after each change in value, save the file using the text in Input!B2

Cheers all!

Hi

I would create a cell, say B3, with the filename calculated as follows

= CONCATENATE("d:/output",Input!B2,".txt")

Then create a macro such as:

Public Sub SaveAsB3()
ThisFile = Range("B3").Value
ActiveWorkbook.SaveAs Filename:=ThisFile
End Sub

Hope this helps

Jake
 
Hi

I would create a cell, say B3, with the filename calculated as follows

= CONCATENATE("d:/output",Input!B2,".txt")

Then create a macro such as:

Public Sub SaveAsB3()
ThisFile = Range("B3").Value
ActiveWorkbook.SaveAs Filename:=ThisFile
End Sub

Hope this helps

Jake

Hi Jake - nearly there. I get the following error message:-
D:\/ cannot be accessed

Any ideas?
 
Bump - can anybody help regarding the above error message?
 
Should it not be:

= CONCATENATE("d:\output",Input!B2,".txt")

?
 
That indeed does fix error.

Now the problem i have is that rather than exporting sheet 'output' to a txt file, it saves the spreadsheet with a .txt suffix, resulting in a file that looks like this when opening in notepad.

Code:
\ ( [ $ ¬ - 2 ] \   # , # # 0 . 0 0 \ ) à      õÿ            À à  
   õÿ            À à  
   õÿ            À à  
   õÿ            À à  
   õÿ            À à  
   õÿ            À à  
   õÿ            À à  
   õÿ            À à  
   õÿ            À à  
   õÿ            À à  
   õÿ            À à  
   õÿ            À à  
   õÿ            À à  
   õÿ            À à  
   õÿ            À à                  À à  
   õÿ   ´       Ÿ à  
   õÿ   ´       * à  
   õÿ   ´       ª à  
   õÿ   ´       ® à  
   õÿ   ´       › à  
   õÿ   ´       ¯ à  
   õÿ   ´       ¬ à  
   õÿ   ´        à  
   õÿ   ´       ‹ à  
   õÿ   ´       ® à  
   õÿ   ´       ¬ à  
   õÿ   ´       ³ à     õÿ   ´       ž à     õÿ   ´        à     õÿ   ´       ‹ à     õÿ   ´       ¤ à     õÿ   ´       ± à     õÿ   ´       ´ à     õÿ   ´       ¾ à     õÿ   ´       Š à     õÿ   ´       ¹ à     õÿ   ´       ¤ à     õÿ   ´       ± à     õÿ   ´       µ à     õÿ   ´       * à     õÿ   ”—— – à     õÿ   ”ff¿¿ · à  
 + õÿ   ø        À à  
 ) õÿ   ø        À à  
 , õÿ   ø        À à  
 * õÿ   ø        À à     õÿ   ô        À à     ôÿ   ô        À à     õÿ   ´       ª à     õÿ   Ô P     À à     õÿ   Ô P     À à     õÿ   Ô       À à     õÿ   ô        À à     ôÿ   ô        À à     õÿ   ”—— ¯ à     õÿ   Ô `     À à     õÿ   ´       « à     õÿ            À à  
   õÿ   œ š à     õÿ   ”¿¿ – à  
 	 õÿ   ø        À à     õÿ   ô        À à     õÿ   Ô a  >  À à     õÿ   ô        À à          `       À à                À à       "         À à  !    "         À à      "         À à  "    "         À à  #    "         À à                  À à         (       À à  $       (       À à          (       À à  #       (       À à  %       (       À à     ¡   P       	 à     ¡  P       	 à     ¡            À à     ¡   x@ @ 	 à     ¡  x!@ @ 	 à     ¡  x@ @ 	 à     ¡  x@ @ 	 à     ¡  8@ @  À à     ¡   x!@ @ 	 à     ¡  x""@ @ 	 à     ¡   X       	 à     ¡  X       	 à  	   ¡   X       	 à    ¡  |@ @ 	 à    ¡  |@ @ 	 à     ¡  x@ @ 	 à    ¡  |@ @ 	 à    ¡  |@ @ 	 à     ¡   x       	 à    ¡  |@ @ 	 à     ¡   p       	 à     ¡  x@ @ 	 à     ¡   p        À à    ¡  |        À à    ¡  |        À à     ¡  x        À à     ¡  p        À à    ¡   t        À à     ¡   `        À à     ¡  x"@ @ $ à     ¡  x @ @ $ à          @       3 à          @       
 à      "  X       
 à  !    "  X       
 à  &              À à  &       H       À à  &       (@ @ À à  &       h@ @ 
 à  &       (@ @ À à  &       h@ @ 
 à  &       (!@ @ À à  &       (!@ @ À à  &       h@ @ 
 à  &       h@ @ 
 à  &       h!@ @ 
 à  &       h!@ @ 
 à  &       (@ @ À à  &       (@ @ À à  &       h!@ @ À à  &       h!@ @ À à  &       h!!@ @ À à  &       h!@ @ 
 à  &       h!@ @ 
 à  &       h!!@ @ 
 à  '    "  8@ @ À à  '    "  8@ @ À à  '    "  x!@ @ À à                 À à          @       À à  (              À à  )              À à  )       H       À à         H       ( à          @       ( à      (         À à  &             À à   1            À à    1            À à    1     D       
 à    1            À à      (  X       ( à      *  X       ( à    1     $       À à    1     d       À à   1  *  \       2 à      !         À à      (  X        à         H       
 à                 À à  *              À à  *       H       
 à        xUU@ @  
 à      "         À à               À à       #  0 w  @  À à       #  0 '  @  À à           Rw@ @  À à           R'@ @  À à           Rp@    À à       #  0 p     À à  +    "  8RR@ @  À à  +    "  8 R  @  À à           uU@ @  À à           WU@ @  À à                À à                À à     ¡	  x@ @ 	 à     ¡	  x!@ @ 	 à     ¡  x@ @ $ à     ¡  x @ @ $ à     ¡	  x @ @ $ à     ¡	  x @ @ $ à     ¡  x@ @ 	 à     ¡  x @ @ 	 à     ¡	  x @ @ 	 à     ¡	  x @ @ 	 à     ¡  8 @ @  À à     ¡  8 @ @  À à     ¡	  x        À à     ¡	  x@ @ 	 à     ¡	  x @ @ 	 à     ¡  x!@ @ 	 à     ¡  x !@ @ 	 à    ¡  | !@ @ 	 à    ¡	  | !@ @ 	 à    ¡	  | !@ @ 	 à     ¡
  x@ @ $ à     ¡
  x@ @ $ à     ¡
  x!@ @ $ à     ¡  p        À à     ¡	  x!@ @ 	 à  ,              À | |            É â‡¿$}( }                 
         0 0 _ )}( }                
         0 0 _ )}( }                
         0 0 _ )}( }                
         0 0 _ )}( }                
         0 0 _ )}( }                
         0 0 _ )}( }                
         0 0 _ )}( }                
         0 0 _ )}( }                
         0 0 _ )}( }            	    
         0 0 _ )}( }            
    
         0 0 _ )}( }                
         0 0 _ )}( }                
         0 0 _ )}( }            
    
         0 0 _ )}( }                
         0 0 _ )}( }                
         0 0 _ )}< }                
         0 0 _ )   ef    \ ( [ $}< }                
         0 0 _ )   ef    \ ( [ $}< }                
         0 0 _ )   ef    \ ( [ $}< }                
         0 0 _ )   ef    \ ( [ $}< }                
         0 0 _ )   ef    \ ( [ $}< }                
         0 0 _ )   ef	    \ ( [ $}< }                
         0 0 _ )   ÌL    \ ( [ $}< }                
         0 0 _ )   ÌL    \ ( [ $}< }                
         0 0 _ )   ÌL    \ ( [ $}< }                
         0 0 _ )   ÌL    \ ( [ $}< }                
         0 0 _ )   ÌL    \ ( [ $}< }                
         0 0 _ )   ÌL	    \ ( [ $}< }                
          0 0 _ )   23    \ ( [ $}< }                
          0 0 _ )   23    \ ( [ $}< }                
          0 0 _ )   23    \ ( [ $}< }                
          0 0 _ )   23    \ ( [ $}< }                 
          0 0 _ )   23    \ ( [ $}< }            !    
          0 0 _ )   23	    \ ( [ $}< }            "    
          0 0 _ )         \ ( [ $}< }            #    
          0 0 _ )         \ ( [ $}< }            $    
          0 0 _ )         \ ( [ $}< }            %    
          0 0 _ )         \ ( [ $}< }            &    
          0 0 _ )         \ ( [ $}< }            '    
          0 0 _ )     	    \ ( [ $}< }            (    
     œ ÿ 0 0 _ )
 
Try:

ActiveWorkbook.SaveAs Filename:=ThisFile, FileFormat :=xlText
 
Hi Charlie - the script seems to be saving the current worksheet as a .txt rather than the worksheet output?

Also - when i run this script, the workbook i have open is renamed to the filename given in the script - should i not be using an export function?
 
Had another look at it and incorporated your 'bonus' point:

Public Sub updateInput()
Dim ipadd As Variant
Dim ipAddRange As Range
Set ipAddRange = Worksheets("System IP address").Range("A3:A15")
For Each ipadd In ipAddRange
Worksheets("Input").Range("B2") = ipadd
SaveUpdate
Next ipadd
End Sub

Public Sub SaveUpdate()
outFile = "D:\"&Worksheets("Input").Range("B2") & ".txt"
Worksheets("Update").Copy
' 1
ActiveWorkbook.SaveAs Filename:=outFile, FileFormat:=xlText
ActiveWorkbook.Close
' 2
End Sub

If you don't want the prompts asking if you want to save each file, or overwrite if they already exist, then add
Application.DisplayAlerts = False at ' 1
and
Application.DisplayAlerts = True at ' 2
 
Last edited:
Hi Charlie - thanks for that, will give it a try in the morning
 
Almost perfect, but on output Excel is doubling double quotes
eg
pre-login-message "This system is for the use of authorized users only."
becomes
pre-login-message ""This system is for the use of authorized users only."""

Any ideas?
 
Where does the message come from? When you say on output, do you mean in the output file?
 
yep, the output of a concatenation.

home again, so will post the formula used and a screenshot of the output.

thanks for taking the time
 
Hmmmm, starting to get to the llimits of my vba and it's difficult without seeing what you are actually doing.
 
Have investigated further,

apparently excel is seeing my output as text if there is a comma in the line. The recommended solution is to print to rather than export. Unfortunately my VB skills are puny..

Change
Code:
ActiveWorkbook.SaveAs Filename:=outFile, FileFormat:=xlText
to
Code:
ActiveWorkbook.SaveAs Filename:=outFile, FileFormat:=xlTextPrinter

See if that helps.

What your trying to do sounds unusual to say the least and perhaps there may be an entirely better way of doing it. I could look at the excel file if you want to send it to me.
 
Appears to work great!

Thank you Charlie and Chris, I've had a pat on the back for saving the engineers quite a bit of time, may i pass that pat on the back on to you both!
 
Change
Code:
ActiveWorkbook.SaveAs Filename:=outFile, FileFormat:=xlText
to
Code:
ActiveWorkbook.SaveAs Filename:=outFile, FileFormat:=xlTextPrinter
See if that helps.

I was going to suggest that as the simple solution but those links you pointed at Phil made me think it was going to be something far more complicated!
 
Last edited:
Everything looks complicated to me in VB! Never took much time to learn vb so difficult to comprehend what's gone wrong
B-)
 
Back
Top