Link to home
Start Free TrialLog in
Avatar of terrypba1
terrypba1

asked on

Pasting over hidden columns in Excel

Hi--
For the wonderful world of DataSync, I need to populate a spreadsheet A that has many hundreds of columns, only say, 40 of which I need to fill. Needless to say they are not consecutive columns.
I can export the data I need from a separate database with those 40 columns, in the order I need, into a separate spreadsheet B. (approx 40 rows worth)
My goal is to hide the unneeded columns in Spreadsheet A, so only the 40 columns I need are showing. That I can do.
Then paste the columns from Spreadsheet A  in to  Spreadsheet B, with data only filling the visible columns, skipping the hidden columns. That, after much research, I can't do.
Any tips?
Ideally I would be able to do this from one sheet into another.
I've attached a png of a portion of Spreadsheet A, with unneeded columns hidden . (Last column is GPM--Anyone know how may total columns that is? Just curious)
Fuse-sample.png
SOLUTION
Avatar of Martin Liss
Martin Liss
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of terrypba1
terrypba1

ASKER

Yikes!
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Very nice! Thank you Martin!
Martin's macro performs exactly as needed!
A little puzzled by the "relative" nature of the macro. Seems like the cell in the "to" spreadsheet, sheet1 must be selected in the same row and column as the selection in the "From" spreadsheet to work. Otherwise it runs but nothing happens. But I can work with it!
29092987.xlsm
Sorry but I didn't have a workbook to work with and I didn't see anything about selecting data in your question so I just provided a generic solution which copies the contents of the source columns, one by one, to the visible destination columns. Let me know if I can help you further.
Hmm. If I am copying 4 rows beginning with row 60 in sheet two, it pastes starting with row 56 in sheet 1. But also blows away everything in the rows above!
The goal is to end up with headings in Sheet 2 to remind me what the 40 columns are if I need to play with the data further. Then select and paste the data from sheet 2 only into Sheet 1, keeping the 5161 headings it originally had, but only populating the 40 columns visible from sheet 2.
If you supply a workbook with a small number of columns and 'Before' and 'After' type sheets, then I can probably help.
Must have been operator error. I can now copy from sheet 6 in the attached without losing headers in sheet 1, FS_item, and it will start in the right row. Still scratching my head a bit--but happily so. Many thanks!
test-with-macro.xlsm
Does this help?
Sub PasteOverHidden()

Dim col As Range
Dim intCol As Integer
Dim wsSource As Worksheet
Dim wsDest As Worksheet

Set wsSource = ThisWorkbook.Sheets("Sheet6")
Set wsDest = ThisWorkbook.Sheets("FS_Item")

' Don't update the screen until we're done. This
' speeds things up and avoids any flickering
Application.ScreenUpdating = False

' Ititialize the variable that will be used to keep track
' of the next column to write to
intCol = 1

' Precess every column in the source sheet
For Each col In wsSource.UsedRange.Columns
    If wsDest.Columns(intCol).Hidden = False Then
        ' The destimation column represented by intCol isn't hidden so
        ' copy contents of the source column represented by col to
        ' the cell in the rirst row of the desination column. Note that
        ' the whole column is copied and not just the first cell.
        col.Copy Destination:=wsDest.Cells(1, intCol)
        ' Set up the next column to be written to
        intCol = intCol + 1
    Else
        ' The column is represented bu intCol is hidden
        ' so loop through the columns until a non-hidden
        ' one is found
        Do Until wsDest.Columns(intCol).Hidden = False
            intCol = intCol + 1
        Loop
        ' Copy to that column
        col.Copy Destination:=wsDest.Cells(1, intCol)
        ' Set up the next column to be written to
        intCol = intCol + 1
    End If
Next

Application.ScreenUpdating = True
End Sub

Open in new window

Now I get it. The entire column is overwritten, headers included. As long as my source sheet has the correct headers (copied using the visible cells only option of Go To Special to paste from sheet 1, I'm all set.
That's good.
I forgot to say...

You’re welcome and I’m glad I was able to help.

If you expand the “Full Biography” section of my profile you’ll find links to some articles I’ve written that may interest you.

Marty - Microsoft MVP 2009 to 2017
              Experts Exchange Most Valuable Expert (MVE) 2015, 2017
              Experts Exchange Top Expert Visual Basic Classic 2012 to 2017
              Experts Exchange Top Expert VBA (current)
Thanks Martin--a great resource!