+ Reply to Thread
Results 1 to 2 of 2

Selection of loop data, move copy and paste data, and exclude blank rows

  1. #1
    Registered User
    Join Date
    04-16-2013
    Location
    Louisville
    MS-Off Ver
    Excel 2010
    Posts
    4

    Selection of loop data, move copy and paste data, and exclude blank rows

    Hi,
    I'm a new user of VBA and I've run into 3 issues. I'm trying to select states and counties, and based on this selection there will be a set of data for each individual state/county combination that I need to copy and paste into blank rows. In order to display the data I need to plug the number for each state (1, 2, 3, ..., 50) and for each county (1, 2, 3, ... , unknown number) into cells "E5" for state and "F5" for county. I have a table where I can grab these state/county number combinations. However, I run into the following issues:

    1. I need to copy the selection from the loop (i.e. 1 for state and 1 for county) and paste the (1, 1) into cells E5 and F5 so that data will be displayed based on this combination. Must be done for each single combination. I haven't been able to do it.
    2. Once the data is displayed, I copy the data and I need to paste it on a specific row below. The trick is that the new data must be pasted below to the previous state/county combo data. Let's say that I run combo (1, 1) and I just run combo (1, 2). The data from (1, 2) must be copied below the (1, 1) data. I must keep the data for all different combinations.
    3. I want the macro to not copy and paste anything if there's no data displayed and go to the next state/county combo.

    This is my code so far:

    Sub CopyAndPaste()

    Dim StateCounty As Range
    Dim Row As Range
    Dim Cell As Range

    Set StateCounty = Range("XFC279:XFD3475") 'Table of state/county combo.

    For Each Row In StateCounty.Rows
    For Each Cell In Row.Cells

    Range("G168").Select
    Range("G168:G258").Select
    Range(Selection, Selection.End(xlToRight)).Select
    Selection.Copy
    Range("A277").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=True

    Next Cell
    Next Row

    End Sub


    Any help would be greatly appreciated.

    Thanks
    Miguel

  2. #2
    Registered User
    Join Date
    04-16-2013
    Location
    Louisville
    MS-Off Ver
    Excel 2010
    Posts
    4

    Re: Selection of loop data, move copy and paste data, and exclude blank rows

    This is my most updated macro so far:

    Sub CopyAndPaste()

    Dim StateCounty As Range
    Dim Row As Range
    Dim Cell As Range
    Dim LastRow As Long
    Dim LastColumn As Long
    Dim ActiveRange As Range

    Set StateCounty = Range("XFC279:XFD281") 'Table of state/county combo. 3475 rows
    Set ActiveRange = Range("G168:DM258")

    For Each Row In StateCounty.Rows
    For Each Cell In Row.Cells

    Range("E5:F5") = Row.Value

    If Not ActiveRange Is Nothing Then

    LastRow = Range("DO" & Rows.Count).End(xlUp).Row
    'LastColumn = Cells(1, Cells.Columns.Count).End(xlToLeft).Column
    ActiveRange.Select
    Selection.Copy
    Range("DO" & LastRow).Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=True, Transpose:=True

    End If

    Next Cell
    Next Row

    End Sub

    Miguel

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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