+ Reply to Thread
Results 1 to 8 of 8

VBA clarification required in using RANGE command

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

    VBA clarification required in using RANGE command

    Hey all. Hope all is good.
    I am a beginner in VBA and the below written one is my first code and I need some clarification from you guys. This question seems to be simple for you guys but actually I don't know how to consolidate it. And also, I don't even know whether its properly structured or not. But its working fine.

    I have just shown the Vlookup part in the below code. But actually this has a lot of nested if conditions and this code has approximately 25 lines of the similar type.

    My question is:
    1) I have used Range("A2") & Resize(9, 1) command in all the lines.
    Is there any option where I can use it only once in the beginning itself instead of writing it in all the lines. Range("A2") & Resize(9, 1) is same for all the lines.

    2) In every excel workbook, only Range("A2") & Resize(9, 1) values will be varying. The rest of the commands / values will be same. Is there any option where I can save / run this code as a new feature in excel and when I run, it has to ask for entering the Range & Resize.

    VBA code:
    Sub Macro1()
    '
    ' Macro1 Macro
    '

    '

    Range("A2").Offset(0, 1).Resize(9, 1) = "=VLOOKUP(RC[-1],Database.xlsx!R1C1:R300C24,6,0)"
    Range("A2").Offset(0, 4).Resize(9, 1) = "=VLOOKUP(RC[-4],Database.xlsx!R1C1:R300C24,15,0)"
    Range("A2").Offset(0, 2).Resize(9, 1) = "=VLOOKUP(RC[-2],Database.xlsx!R1C1:R300C24,10,0)"
    Range("A2").Offset(0, 3).Resize(9, 1) = "=VLOOKUP(RC[-3],Database.xlsx!R1C1:R300C24,13,0)"
    Range("A2").Offset(0, 5).Resize(9, 1) = "=VLOOKUP(RC[-5],Database.xlsx!R1C1:R300C24,19,0)"
    Range("A2").Offset(0, 6).Resize(9, 1) = "=VLOOKUP(RC[-6],Database.xlsx!R1C1:R300C24,22,0)"

    End Sub

    Thanks in advance for your time & suggestion.

  2. #2
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    MS365 Apps for enterprise
    Posts
    5,887

    Re: VBA clarification required in using RANGE command

    For the first question you can do something like...
    Please Login or Register  to view this content.
    As for your second question you can alter code to something like...
    Please Login or Register  to view this content.
    There are several ways to implement this across workbooks. Read link and follow direction.
    http://www.jkp-ads.com/Articles/DistributeMacro00.asp
    ?Progress isn't made by early risers. It's made by lazy men trying to find easier ways to do something.?
    ― Robert A. Heinlein

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

    Re: VBA clarification required in using RANGE command

    Hi CK.
    I just want to tell you that Its just simply "AWESOME". You have altered the program the way I wanted it.
    Thank you for the suggestion. Thanks a lot!!!!!

    If you don't mind and if you have some time, can you briefly explain me on the the below lines:
    Set rng = .InputBox(Prompt:="Select starting position", Type:=8) - What is the meaning of Type:=8 ?
    rowSize = .InputBox(Prompt:="Enter row size", Type:=1) - What is the meaning of Type:=1?
    colSize = .InputBox(Prompt:="Enter column size", Type:=1) - What is the meaning of Type:=1 ?

    Thank you once again.

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

    Re: VBA clarification required in using RANGE command

    Also in the same below code, can you please tell me how to paste as values.
    Like, in the first seven columns, I need to paste as values and the next two columns must be in formula itself and again next five columns must be pasted as values.

    Sub Demo()
    Dim rng As Range
    Dim colSize As Long, rowSize As Long
    With Application
    Set rng = .InputBox(Prompt:="Select starting position", Type:=8)
    rowSize = .InputBox(Prompt:="Enter row size", Type:=1)
    colSize = .InputBox(Prompt:="Enter column size", Type:=1)
    End With

    With rng.Resize(rowSize, colSize)
    .Offset(0, 1) = "=VLOOKUP(RC[-1],Database.xlsx!R1C1:R300C24,6,0)"
    .Offset(0, 4) = "=VLOOKUP(RC[-4],Database.xlsx!R1C1:R300C24,15,0)"
    'So on so forth
    End With
    End Sub

  5. #5
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    MS365 Apps for enterprise
    Posts
    5,887

    Re: VBA clarification required in using RANGE command

    Type:=8 means range selection (i.e. cell reference as range object). Type:=1 means numeric. See link for full list of Type argument for Application.Inputbox.
    https://msdn.microsoft.com/VBA/Excel...x-method-excel

    As for your last question... I'm not sure what you mean.

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

    Re: VBA clarification required in using RANGE command

    Hi CK. Sorry for not being clear in that case.
    From the code,
    .Offset(0, 1) = "=VLOOKUP(RC[-1],Database.xlsx!R1C1:R300C24,6,0)"
    .Offset(0, 4) = "=VLOOKUP(RC[-4],Database.xlsx!R1C1:R300C24,15,0)"

    The above 2 lines (same as above code) after running the macro will be in the formula type. But not as a Value.
    But I want that to be in the "Value" type and not in the "Formula" type after running the macro.

    Thanks for your time.

  7. #7
    Forum Contributor HaroonSid's Avatar
    Join Date
    02-28-2014
    Location
    india
    MS-Off Ver
    Excel 2013
    Posts
    2,080

    Re: VBA clarification required in using RANGE command

    MayBe
    Please Login or Register  to view this content.
    Use Code-Tags for showing your code :
    Please mark your question Solved if there has been offered a solution that works fine for you
    If You like solutions provided by anyone, feel free to add reputation using STAR *

  8. #8
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    MS365 Apps for enterprise
    Posts
    5,887

    Re: VBA clarification required in using RANGE command

    If HaroonSid's solution doesn't work. You can simply put value of cell back into cell.

    Ex:
    Please Login or Register  to view this content.

+ 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. Userform VBA command button code required or explained
    By lcartwright in forum Excel General
    Replies: 0
    Last Post: 02-03-2015, 06:17 PM
  2. [SOLVED] IF command use to get the required formula
    By krish T in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-31-2014, 05:43 PM
  3. Duplicates from Range - Code Clarification
    By jagadeesh.rt in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-10-2014, 05:25 AM
  4. Age clarification in excel
    By Pierre99 in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 04-16-2013, 01:22 PM
  5. [SOLVED] VBA Code: Clarification on Range usage ( counting last used cell in a column)
    By vidyuthrajesh in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-27-2012, 03:07 PM
  6. SQL command required
    By Vass in forum Excel General
    Replies: 1
    Last Post: 02-18-2006, 07:10 AM
  7. Beyond VLOOKUP Clarification
    By nebb in forum Excel Formulas & Functions
    Replies: 24
    Last Post: 09-06-2005, 12:05 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