Consulting

Results 1 to 2 of 2

Thread: The sheet selection depends on the variable?

  1. #1
    VBAX Contributor
    Joined
    Nov 2009
    Posts
    114
    Location

    The sheet selection depends on the variable?

    [VBA]
    Sub addloop()


    For addi = 1 To 1000


    ActiveCell.Offset(0, -1).Select
    Range(Selection, ActiveCell.Offset(0, 2)).Select
    Selection.Copy
    Windows("Statament.xls").Activate
    Sheets("A").Select
    Range("C4").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False

    Windows("Registration.xls").Activate
    ActiveCell.Offset(0, 3).Select
    Range(Selection, ActiveCell.Offset(0, 4)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.Copy

    Windows("Statement.xls").Activate
    Sheets("A").Select
    Range("B7").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False
    Application.Run "statement.xls'!Macro1"
    Windows("Registration.xls").Activate
    ActiveCell.Offset(0, -2).Select
    Selection.End(xlDown).Select


    Next addi



    End Sub

    [/VBA]


    Since the Sheetname chosen in "statement.xls" depend on variable


    If i have the variable { 1= A , 2 = T , 3 = K ...... 1000 = D }
    How to insert into above code so that the outcome are following:


    Sheets("A").Select for i = 1

    Sheets("T").Select for i = 2

    Sheets("K").Select for i = 3

    Sheets("D").Select for i = 1000


    Thanks!

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Try to avoid Activating and Selecting; use a range variables instead.
    This may not be correct (its hard to follow without data), but shows how to use an array of sheet names and variable references.
    [VBA]Option Explicit

    Sub addloop()


    Dim Arr()
    Dim addi As Long
    Dim Cel As Range
    Dim wbStat As Workbook
    Dim wbReg As Workbook


    Set wbStat = Workbooks("Statement.xls")
    Set wbReg = Workbooks("Registration.xls")

    Arr = Array("A", "K", "T", "D")

    Set Cel = ActiveCell.Offset(0, -1)

    For addi = 1 To 1000
    Range(Cel, Cel.Offset(0, 2)).Copy
    wbStat.Sheets(Arr(addi)).Range("C4").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False

    Set Cel = Cel.Offset(0, 3)
    Range(Cel, Cel.Offset(0, 4).End(xlDown)).Copy

    wbStat.Sheets(Arr(addi)).Range("B7").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False
    Application.Run "statement.xls'!Macro1"
    Set Cel = Cel.Offset(0, -2).End(xlDown)
    Next addi
    End Sub
    [/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

Posting Permissions

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