+ Reply to Thread
Results 1 to 5 of 5

How to define Variables as Ranges and Copy them?

Hybrid View

  1. #1
    Registered User
    Join Date
    05-31-2012
    Location
    Groenlo, Netherlands
    MS-Off Ver
    Excel 2019
    Posts
    64

    How to define Variables as Ranges and Copy them?

    I want to create a Macro to be able to prepare a NEW Offer/Calculation based on an OLD Offer/Calculation.
    I use 3 Tabs in Excel for this:
    Tab 1: Contains the old calculation
    Tab 2: Should contain the new calculation
    Tab 3: Contains info about how a position in the old calculation matches a position in the new calculation.

    Tab 3 is added as an attachment.

    In the table A5:F22 of TAB3 the following info is given:
    A the position number of the new calc
    F the Range of the old calc [Tab3] that should be copied for this position.
    C the cell in [Tab2] where the the calculation should be pasted (at least the cell where the range should start)

    How can I create a Macro to do the following
    Copy the range from TAB1 defined in TAB3 Cel F5
    Paste the range starting in the Cel defined on TAB2 defined in TAB3 Cel C5
    Then give the Cel on TAB2 defined in TAB3 Cel C5 the value of A5

    Move on to the next row until No Values are defined in rows C and F
    Attached Files Attached Files

  2. #2
    Registered User
    Join Date
    05-31-2012
    Location
    Groenlo, Netherlands
    MS-Off Ver
    Excel 2019
    Posts
    64

    Re: How to define Variables as Ranges and Copy them?

    This is what I have so far:

    Sub C_COPY_BASISKALK()
    '
    ' Een nieuwe kalkulatie maken op basis van een oude kalkulatie
    '
    
    '    Application.ScreenUpdating = False
    '    Application.Calculation = xlCalculationManual
        
    '   Basiskalk kopieren en herbenoemen naar Copykalk
        Sheets("Basiskalk.").Copy before:=Sheets("Basiskalk.")
        Sheets("Basiskalk. (2)").Name = "Copykalk."
    
    '   Basiskalk kopieren en herbenoemen naar Copykalk2 tbv maken Macro, wordt later gewoon basiskalk
        Sheets("Basiskalk.").Copy before:=Sheets("Basiskalk.")
        Sheets("Basiskalk. (2)").Name = "Copykalk2."
    
    
    Dim VolgNr As Boolean
    Dim Positie As Range
    Dim Kalkulatie As Range
    
    Sheets("Copykalk").Select
    VolgNr = [A1].Value
    Positie = [C1].Value
    Kalkulatie = [F1].Value
    
    Sheets("Copykalk.").Select
    Kalkulatie.Select
    Selection.Copy
    Sheets("Copykalk2.").Select
    Positie.Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
    Positie.Value = VolgNr
    
     
    '    [A1].Select
        
    '    Application.Calculation = xlCalculationAutomatic
    '    Application.ScreenUpdating = True
        
    MsgBox "Fertig!"
        
    End Sub
    Last edited by Dubrock; 03-15-2014 at 06:00 PM.

  3. #3
    Registered User
    Join Date
    05-31-2012
    Location
    Groenlo, Netherlands
    MS-Off Ver
    Excel 2019
    Posts
    64

    Re: How to define Variables as Ranges and Copy them?

    Anyone an idea?

  4. #4
    Administrator FDibbins's Avatar
    Join Date
    12-29-2011
    Location
    Duncansville, PA USA
    MS-Off Ver
    Excel 7/10/13/16/365 (PC ver 2310)
    Posts
    53,048

    Re: How to define Variables as Ranges and Copy them?

    Dubroc, please use code tags with your code, thanks
    1. Use code tags for VBA. [code] Your Code [/code] (or use the # button)
    2. If your question is resolved, mark it SOLVED using the thread tools
    3. Click on the star if you think someone helped you

    Regards
    Ford

  5. #5
    Registered User
    Join Date
    05-31-2012
    Location
    Groenlo, Netherlands
    MS-Off Ver
    Excel 2019
    Posts
    64

    Re: How to define Variables as Ranges and Copy them?

    I managed to make the Macro work, but row for row. And its far to slow. Based on the first position which is 20 rows, it would take about 6 hours to do one calculation of about 3000 rows.

    Does anyone have ideas to make the macro significantly faster??

    Sub C_COPY_BASISKALK()
    '
    ' Een nieuwe kalkulatie maken op basis van een oude kalkulatie
    
        Application.ScreenUpdating = False
        Application.Calculation = xlCalculationManual
        
    '    Application.Calculation = xlCalculationAutomatic
    '    Application.ScreenUpdating = True
        
    '   Basiskalk kopieren en herbenoemen naar Copykalk
    ''    Sheets("Basiskalk.").Copy before:=Sheets("Basiskalk.")
    ''    Sheets("Basiskalk. (2)").Name = "Copykalk1"
    
    '   Basiskalk kopieren en herbenoemen naar Copykalk2 tbv maken Macro, wordt later gewoon basiskalk
    ''    Sheets("Basiskalk.").Copy before:=Sheets("Basiskalk.")
    ''    Sheets("Basiskalk. (2)").Name = "Copykalk2"
    
    Dim VolgNummer As Integer
    Dim RijNummer As Integer
    Dim VolgNummmerOud As Integer
    
    Sheets("Copykalk").Select
    ActiveSheet.Calculate
    
    RijNummer = 6
    VolgNummer = Cells(RijNummer, 1).Value
    VolgNummerOud = 100
    
    Sheets("Copykalk").Select
    [A1] = VolgNummer
    
    While VolgNummerOud < 999
    
    Application.Run "ZC_Copy_Pos"
    
    Sheets("Copykalk").Select
    RijNummer = RijNummer + 1
    VolgNummer = Cells(RijNummer, 1).Value
    [A1] = VolgNummer
    ActiveSheet.Calculate
    VolgNummerOud = [B1].Value
    
    Wend
    
    Sheets("Copykalk2").Select
       [A1].Select
        
        Application.Calculation = xlCalculationAutomatic
        Application.ScreenUpdating = True
        
    MsgBox "Fertig!"
    
    End Sub
    
    Sub ZC_Copy_Pos()
    
    Dim VolgNr As Integer
    Dim Pos As Integer
    Dim First As Integer
    Dim Last As Integer
    Dim RowNew As Integer
    Dim RowOld As Integer
    
    Sheets("Copykalk").Select
    VolgNr = [A1]
    
    Sheets("Copykalk").Select
    Pos = [C1]
    
    Sheets("Copykalk").Select
    First = [F1]
    Last = [G1]
    
    RowOld = First
    RowNew = Pos
    
    While RowOld < Last + 1
    
    'Copykalk2.[RowNew].Formula = Copykalk1.[RowOld].Formula
    
    Sheets("Copykalk1").Select
    Rows(RowOld).Copy
    
    Sheets("Copykalk2").Select
    Rows(RowNew).Select
    Selection.PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
    
    RowOld = RowOld + 1
    RowNew = RowNew + 1
    
    Wend
    
    'Selecting the cell in row 8 and column 1
        Cells(Pos, 2).Value = VolgNr
        
    End Sub

+ 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. Using variables to define a range
    By Aelyn in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 08-27-2011, 04:27 AM
  2. Can't find a way to define variables
    By Mole in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-18-2011, 02:42 PM
  3. need help to define a range using two variables
    By dschmitt in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-15-2010, 08:14 AM
  4. How to use variables to define range
    By Dolemite in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 08-12-2005, 02:05 PM
  5. How do you define variables in excel?
    By BigRon in forum Excel General
    Replies: 6
    Last Post: 04-16-2005, 04:06 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