+ Reply to Thread
Results 1 to 15 of 15

Is there a way to Set Desination with a variable?

  1. #1
    Registered User
    Join Date
    04-05-2019
    Location
    cape town
    MS-Off Ver
    2016
    Posts
    16

    Question Is there a way to Set Desination with a variable?

    Hi guys, im just having a bit of trouble with my code.

    I have code that outputs data into a cell.

    Originally it was coded with:

    Please Login or Register  to view this content.
    and it worked perfectly.

    But i realised that i need it to output data in other columns too so that each result would be under its appropriate column.

    and so i tried doing this

    Please Login or Register  to view this content.
    but i kept getting an error message that said
    "duplicate delcaration" and it highlighted the line

    Please Login or Register  to view this content.
    I guess this means im declaring "x" in the "Set destination" line aswell.

    My intention was to make it a variable so i could put it in a loop and each result could be placed in its relevant column. but im stuck at this issue. Ive been looking for an answer for a few hours and cant come up with a way of doing it. Is there a way i can work around this?

    Thank you for any help.
    kind regards.
    Last edited by FanAnd22; 08-20-2020 at 10:21 AM.

  2. #2
    Banned User!
    Join Date
    02-06-2020
    Location
    Iowa City, IA, USA
    MS-Off Ver
    2016 - 365 / 2007
    Posts
    2,014

    Re: Is there a way to Set Desination with a variable?

    the reason you're getting the error is because you are using ''x'' twice. it doesn't matter where it's at. I believe, the option explicit statement in that language requires a coder to write dim [var] as [type] when declaration variables. if you don't write that, you can use anything in code without issuing a formal declaration.

  3. #3
    Registered User
    Join Date
    04-05-2019
    Location
    cape town
    MS-Off Ver
    2016
    Posts
    16

    Re: Is there a way to Set Desination with a variable?

    Quote Originally Posted by vba_php View Post
    the reason you're getting the error is because you are using ''x'' twice. it doesn't matter where it's at. I believe, the option explicit statement in that language requires a coder to write dim [var] as [type] when declaration variables. if you don't write that, you can use anything in code without issuing a formal declaration.

    Hi vba_php. Thank you for the reply. I had tried removing the line:
    Please Login or Register  to view this content.
    I thought it might help. but it just had a different error. instead it highlighted the Line:

    Please Login or Register  to view this content.
    And said: Runtime errot "1004":

    application defined or object defined error

    So yeah, its been quite confusing.

    I hope i haven't misunderstood.

    Thank you for trying.

  4. #4
    Banned User!
    Join Date
    02-06-2020
    Location
    Iowa City, IA, USA
    MS-Off Ver
    2016 - 365 / 2007
    Posts
    2,014

    Re: Is there a way to Set Desination with a variable?

    well you tried to fix it. that's great! so exactly do you want to do? you said something about a variable and you attempted to use an array for something....

  5. #5
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Is there a way to Set Desination with a variable?

    Try this.
    Please Login or Register  to view this content.
    If posting code please use code tags, see here.

  6. #6
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,516

    Re: Is there a way to Set Desination with a variable?

    Re: "But i realized that i need it to output data in other columns too so that each result would be under its appropriate column."
    How would you determine these "other columns"?

    Maybe show us your whole code because it looks like it might need some updating.

  7. #7
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,430

    Re: Is there a way to Set Desination with a variable?

    It also looks from your code fragments that you are using the variable X before you assign a non zero value to it, which would give the 1004 error.
    Cheers
    Andy
    www.andypope.info

  8. #8
    Banned User!
    Join Date
    02-06-2020
    Location
    Iowa City, IA, USA
    MS-Off Ver
    2016 - 365 / 2007
    Posts
    2,014

    Re: Is there a way to Set Desination with a variable?

    Quote Originally Posted by Andy Pope View Post
    It also looks from your code fragments that you are using the variable X before you assign a non zero value to it, which would give the 1004 error.
    I was under the assumption knew what they were doing and/or had written code before. if I'm not mistaken, you can use any number var without assigning a val to it. a dec defaults it to 0.

  9. #9
    Forum Expert rorya's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365 Ent Monthly Channel / Insiders Beta
    Posts
    8,903

    Re: Is there a way to Set Desination with a variable?

    That is true but Cells(22, 0) is going to cause an error as Andy said.
    Rory

  10. #10
    Banned User!
    Join Date
    02-06-2020
    Location
    Iowa City, IA, USA
    MS-Off Ver
    2016 - 365 / 2007
    Posts
    2,014

    Re: Is there a way to Set Desination with a variable?

    Quote Originally Posted by rorya View Post
    That is true but Cells(22, 0) is going to cause an error as Andy said.
    and why is that? is it because an unassigned val to a var that is not dec'd is inside a function? if so, I would like to know. that would be something to add to my KB. and if that is true, that's why i don't like office programs. PHP/javascript frameworks wouldn't do that. I know PHP doesn't.

  11. #11
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,430

    Re: Is there a way to Set Desination with a variable?

    The column value needs to be a valid one, regardless of the variable type being used to store it.

    The Msgbox's will error so comment out as you run and do your investigation.

    Please Login or Register  to view this content.

  12. #12
    Banned User!
    Join Date
    02-06-2020
    Location
    Iowa City, IA, USA
    MS-Off Ver
    2016 - 365 / 2007
    Posts
    2,014

    Re: Is there a way to Set Desination with a variable?

    Quote Originally Posted by Andy Pope View Post
    The column value needs to be a valid one, regardless of the variable type being used to store it.
    isn't a default of 0 good enough? if 0 is there without a dec, and with a literal dec, what does it matter? must be a quirk of VBA?

  13. #13
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,430

    Re: Is there a way to Set Desination with a variable?

    No the issue here is that ZERO is not a valid column reference.

    It's not a quirk

  14. #14
    Banned User!
    Join Date
    02-06-2020
    Location
    Iowa City, IA, USA
    MS-Off Ver
    2016 - 365 / 2007
    Posts
    2,014

    Re: Is there a way to Set Desination with a variable?

    no kidding!? oh my goodness, I guess I never did check. I rarely use CELLS() or the .column property. thanks for pointing that out andy.

  15. #15
    Registered User
    Join Date
    04-05-2019
    Location
    cape town
    MS-Off Ver
    2016
    Posts
    16

    Smile Re: Is there a way to Set Desination with a variable?

    Thank you guys. It all works!

    Please Login or Register  to view this content.
    The suggestion to use:

    Please Login or Register  to view this content.
    As well as to move the "Set destination" line after the initial declarations,solved everything!

    Thank you. Thank you.

    Everything is working perfectly.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Object variable or With block variable not set when copying chart to outlook email body
    By jprlimey in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 10-09-2019, 04:17 PM
  2. [SOLVED] Macro to select a variable cell range and copy it down a variable amount of times
    By JPoFresh in forum Excel Programming / VBA / Macros
    Replies: 15
    Last Post: 07-18-2019, 09:17 PM
  3. Replies: 3
    Last Post: 02-17-2015, 11:53 PM
  4. Workbook desination
    By Mick Hassell in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 01-20-2015, 01:19 PM
  5. Replies: 4
    Last Post: 07-12-2013, 12:14 PM
  6. Replies: 6
    Last Post: 12-21-2012, 08:03 AM
  7. Replies: 1
    Last Post: 10-27-2012, 02:54 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1