Username: Password:

Author Topic: Need some help from those good at Excel/Visual Basic  (Read 7524 times)

_tom_

  • Middleweight
  • *****
  • Posts: 8842
Need some help from those good at Excel/Visual Basic
« on: October 09, 2006, 10:40:10 PM »
I'm trying to create a button which when clicked will save the active worksheet as the number in a cell in the same worksheet. I found this but it doesnt seem to work. I get an error message in Visual Basic editor http://www.mrexcel.com/tip040.shtml

Mine dont have the Public Sub bits, just code for the button. I'll have a look at school tomorrow to show you exactly what all my buttons codes are but its definately not Public sub, something like "Click_Button_1()"

Any help would be great, coz this is really pissing me off

CaffeineJunkie

  • Welterweight
  • ****
  • Posts: 1306
Need some help from those good at Excel/Visual Basic
« Reply #1 on: October 09, 2006, 10:57:32 PM »
OK, i take it you're working in VBA not VB (so the built in editor for Excel)

When you have a button click event, you will have something along the lines of ....

Private Sub cmdButtonName_click()

Dim sFileName as string

if len(range("A4").value) <> 0 then
    sfilename = range("A4").value
    activeworkbook.saveas filename:=sFileName
else
    msgbox "Nothing in cell"
end if

end sub


what error were you getting when you ran it????




James
PRS Starla for sale, see Seconds out section...

_tom_

  • Middleweight
  • *****
  • Posts: 8842
Need some help from those good at Excel/Visual Basic
« Reply #2 on: October 09, 2006, 11:00:38 PM »
I'll have a look tomorrow, will try that code you suggested. I'll printscreen the error if it still comes up. Really need to get excel working on this computer again  :x

CaffeineJunkie

  • Welterweight
  • ****
  • Posts: 1306
Need some help from those good at Excel/Visual Basic
« Reply #3 on: October 09, 2006, 11:02:33 PM »
kk, although another alternative is to define a function which does the saving, and then you only need to build the code once, and then refer to it for each button you put in, making it easier to change if you have lots of buttons... but it depends on what you are trying to build
PRS Starla for sale, see Seconds out section...

_tom_

  • Middleweight
  • *****
  • Posts: 8842
Need some help from those good at Excel/Visual Basic
« Reply #4 on: October 09, 2006, 11:06:40 PM »
Well I dont know what the hell that means :lol:

Basically I've just got to create an order form/invoice for a company, and I only need 1 button to save each invoice as the invoice number I have on the sheet..

CaffeineJunkie

  • Welterweight
  • ****
  • Posts: 1306
Need some help from those good at Excel/Visual Basic
« Reply #5 on: October 09, 2006, 11:08:39 PM »
oh right, in that case use the code from above, although be sure to save it in the right place, else it will automatically save straight to the default directory

(i would use app.path, but i don't think that works in VBA, so i shall look it up in a sec)
PRS Starla for sale, see Seconds out section...

_tom_

  • Middleweight
  • *****
  • Posts: 8842
Need some help from those good at Excel/Visual Basic
« Reply #6 on: October 09, 2006, 11:12:46 PM »
Ok, cheers for the help. Hopefully can get this working so I can move on.. such a boring task

CaffeineJunkie

  • Welterweight
  • ****
  • Posts: 1306
Need some help from those good at Excel/Visual Basic
« Reply #7 on: October 09, 2006, 11:14:00 PM »
naah, i love programming me, usually spend most of my time in computing doing the work rather than the design etc.

for my current project i've already learnt one language, and now i have to learn another one, :D:D dunno why, but i love it all :D:D:D
PRS Starla for sale, see Seconds out section...

CaffeineJunkie

  • Welterweight
  • ****
  • Posts: 1306
Need some help from those good at Excel/Visual Basic
« Reply #8 on: October 09, 2006, 11:17:10 PM »
Quote from: CaffeineJunkie
activeworkbook.saveas filename:=sFileName


my mistake, this should be activesheet not activeworkbook for saving that sheet
PRS Starla for sale, see Seconds out section...

_tom_

  • Middleweight
  • *****
  • Posts: 8842
Need some help from those good at Excel/Visual Basic
« Reply #9 on: October 10, 2006, 11:41:17 AM »
Quote from: CaffeineJunkie
naah, i love programming me, usually spend most of my time in computing doing the work rather than the design etc.

for my current project i've already learnt one language, and now i have to learn another one, :D:D dunno why, but i love it all :D:D:D


haha I'm the other way round. Good at designing stuff, rubbish at programming what I want the stuff to do :lol:

Anyway, I tried your code, this is what it came up with:


ToneMonkey

  • Welterweight
  • ****
  • Posts: 2230
Need some help from those good at Excel/Visual Basic
« Reply #10 on: October 10, 2006, 11:56:08 AM »
Good luck mate, I did a fair bit of VB at uni (aswell as C, C++ and some other stuff that I can't remember) and I don't miss it at all.  Infact the vast majority has been filed in the "Don't think I'll be using this again" section of my brian.  It was a but odd to look at it again.

Good luck :wink:
Advice worth what you just paid for it.

CaffeineJunkie

  • Welterweight
  • ****
  • Posts: 1306
Need some help from those good at Excel/Visual Basic
« Reply #11 on: October 10, 2006, 02:23:50 PM »
thas nothing to do with the code, it's more to do with when you are executing the code, when i get home from college i'll take a closer look
PRS Starla for sale, see Seconds out section...

_tom_

  • Middleweight
  • *****
  • Posts: 8842
Need some help from those good at Excel/Visual Basic
« Reply #12 on: October 10, 2006, 03:17:38 PM »
Oh.. shows what I know then :lol: Cheers for all your help, hopefully can get this sorted out soon.

CaffeineJunkie

  • Welterweight
  • ****
  • Posts: 1306
Need some help from those good at Excel/Visual Basic
« Reply #13 on: October 10, 2006, 09:26:42 PM »
why do you have two "end sub" statements??
PRS Starla for sale, see Seconds out section...

CaffeineJunkie

  • Welterweight
  • ****
  • Posts: 1306
Need some help from those good at Excel/Visual Basic
« Reply #14 on: October 10, 2006, 09:28:54 PM »
and what is the name of your button???
PRS Starla for sale, see Seconds out section...