+ Reply to Thread
Results 1 to 2 of 2

Increment Cell n-times

Hybrid View

  1. #1
    Registered User
    Join Date
    01-30-2013
    Location
    CDO, Philippines
    MS-Off Ver
    Excel 2010
    Posts
    1

    Increment Cell n-times

    Hi, I'm very new to excel programming. I have a very limited background in C++ too and no background in VBA at all. I don't even know if my descriptive title is correct. But anyway, I just want to know if it would be possible to increment a cell index so that after each loop of the function, the active cell selected is exactly n-rows away from the previous one.

    Say, I have a macro that 'extracts' data from 30 worksheets and pastes these 'extracted' data into a single worksheet. After each 'run' in each of the 30 worksheets, the macro pastes the data into the destination worksheet, just below the cells where it has already pasted the data from the other worksheets. My current macro is lengthy and ugly because I copied and pasted the same function 30 times, so that I can change the activecell index 30 times. Am I making any sense?

    I'm trying to shorten code by calling the same function 30 times, but with the activecell already incremented after each 'run' so that the next pasted data will be in place. Any help?

  2. #2
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Increment Cell n-times

    Hi, yangmuffins,

    VBA has loops which could do the job:
    Sub sample()
    Dim ws As Worksheet
    Dim wsTarg As Worksheet
    Dim lngFirstFree As Long
    
    Set wsTarg = Sheets("Summary")
    For Each ws In ThisWorkbook.Worksheets
      If Not ws.Name = wsTarg.Name Then
        lngFirstFree = wsTarg.Cells(Rows.Count, "A").End(xlUp).Row + 1
        ws.UsedRange.Copy wsTarg.Cells(lngFirstFree, "A")
      End If
    Next ws
    Set wsTarg = Nothing
    End Sub
    This code will go through all the worksheets in the workbook with the code. If the name of the worksheet doesn´t match "Summary" (alter to suit) then the first empty row on Summary is put into the variable lngFirstFree for the copying.

    Ciao,
    Holger
    Use Code-Tags for showing your code: [code] Your Code here [/code]
    Please mark your question Solved if there has been offered a solution that works fine for you

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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