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
terrypba1Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Martin LissOlder than dirtCommented:
Function ColLetterToNum(ByVal sColLetter As String) As Long
' Convert column letter to numeric
     ColLetterToNum = ActiveWorkbook.Worksheets(1).Columns(sColLetter).Column
End Function

Open in new window


Gives 5161
terrypba1Author Commented:
Yikes!
Martin LissOlder than dirtCommented:
Try this.
29092987.xlsm

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
SolarWinds® IP Control Bundle (IPCB)

Combines SolarWinds IP Address Manager and User Device Tracker to help detect IP conflicts, quickly identify affected systems, and help your team take near instantaneous action. Help improve visibility and enhance reliability with SolarWinds IP Control Bundle.

terrypba1Author Commented:
Very nice! Thank you Martin!
terrypba1Author Commented:
Martin's macro performs exactly as needed!
terrypba1Author Commented:
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
Martin LissOlder than dirtCommented:
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.
terrypba1Author Commented:
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!
terrypba1Author Commented:
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.
Martin LissOlder than dirtCommented:
If you supply a workbook with a small number of columns and 'Before' and 'After' type sheets, then I can probably help.
terrypba1Author Commented:
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
Martin LissOlder than dirtCommented:
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

terrypba1Author Commented:
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.
Martin LissOlder than dirtCommented:
That's good.
Martin LissOlder than dirtCommented:
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)
terrypba1Author Commented:
Thanks Martin--a great resource!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Images and Photos

From novice to tech pro — start learning today.