+ Reply to Thread
Results 1 to 6 of 6

Alternate solution or Edit in the VBA required

  1. #1
    Registered User
    Join Date
    06-26-2017
    Location
    India
    MS-Off Ver
    2010
    Posts
    81

    Alternate solution or Edit in the VBA required

    Hey all. Hope all is good.


    First of all, thanks for your time in reading this request.


    I am new to VBA and I don't know whether the below written code is properly used or not.


    Below is the VBA code which I have written for applying Vlookup to certain columns. And Its working fine.


    Sub Macro1()
    '
    ' Macro1 Macro
    '

    '
    ActiveCell.Offset(0, 1) = "=VLOOKUP(RC[-1],Database.xlsx!R1C1:R300C24,6,0)"
    ActiveCell.Offset(0, 4) = "=VLOOKUP(RC[-4],Database.xlsx!R1C1:R300C24,12,0)"
    ..............continues for few more columns with row number / row cell being fixed.
    End Sub



    But to run this code properly, everytime I need to select the correct cell from where offset has to be done in each and every workbook as I have used "ActiveCell" option.


    Can anyone tune the above code or suggest any other alternate method where I can apply this formula by fixing the cell .


    Example:
    Consider A5 has some code.
    So, Lookup value will be "$A5" and Column_index_num will be varying everytime (i.e Column_index_num will be $A6, $A7, $A10 and so on).

    Thank you all in advance for your suggestions.

  2. #2
    Valued Forum Contributor
    Join Date
    03-16-2017
    Location
    UK
    MS-Off Ver
    2016
    Posts
    371

    Re: Alternate solution or Edit in the VBA required

    A very quick solution would be to make the code select the correct cell before it uses ActiveCell. This will avoid you having to re-write the code.

    e.g. if you always need to select cell A5 then put at the top:
    Please Login or Register  to view this content.
    or shorthand (but no intellisense)
    Please Login or Register  to view this content.
    .Select changes your cursor to that cell and activates it. It will then be your ActiveCell even if you ran the macro from a different cell.

  3. #3
    Registered User
    Join Date
    06-26-2017
    Location
    India
    MS-Off Ver
    2010
    Posts
    81

    Re: Alternate solution or Edit in the VBA required

    Hi Stormin,

    Thanks for your time and the solution.
    But I am really sorry to bother you now.
    Can you kindly show me on how do I use that option Range("A5").Select with the above code that I have mentioned ?

  4. #4
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    6,289

    Re: Alternate solution or Edit in the VBA required

    You could also use the With construct, so the selection does not need to change. For example, if you normally choose G5 before running the code:

    Please Login or Register  to view this content.
    Note that you don't have to use the R1C1 form of the formula. Note too that it is a bad idea to name your worksheets with an extension that makes them look like files (unless you really wanted a file):

    Please Login or Register  to view this content.

    You can also enter entire columns of formulas like so

    Please Login or Register  to view this content.
    Bernie Deitrick
    Excel MVP 2000-2010

  5. #5
    Registered User
    Join Date
    06-26-2017
    Location
    India
    MS-Off Ver
    2010
    Posts
    81

    Re: Alternate solution or Edit in the VBA required

    Hi Bernie Deitrick,

    Its working as per my requirement!!!!!! Thanks for the solution.

    Thank you. Thanks a lot!!!!!!

  6. #6
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    6,289

    Re: Alternate solution or Edit in the VBA required

    You're very welcome. Thanks for letting me know my suggestion helped.

+ 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. Alternate solution for vlookup to return all possible results
    By Excelrae in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 11-05-2015, 09:10 PM
  2. Replies: 4
    Last Post: 06-25-2013, 09:29 AM
  3. Replies: 3
    Last Post: 06-20-2013, 03:51 AM
  4. F2 and Enter alternate Macro or VBA solution required
    By truvraj in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 09-27-2012, 01:46 PM
  5. Replies: 2
    Last Post: 12-08-2011, 09:07 AM
  6. Replies: 2
    Last Post: 10-25-2010, 03:10 PM
  7. Need a solution to edit numbers
    By KarambaStar in forum Excel General
    Replies: 5
    Last Post: 02-14-2008, 12:26 PM

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