Copy Columns From Opened File into Active Worksheet, Last Two Columns Don't Paste

SkywardPalm

Board Regular
Joined
Oct 23, 2021
Messages
61
Office Version
  1. 365
Platform
  1. Windows
I have VBA code that pulls the column data from one workbook into another, and it pulls all but the last two columns based on headers.

VBA Code:
Sub Import_Data()
    ' Name                  Type                    Description
    '==================================================================================================
    Dim wbCurrent           As Workbook             ' Current workbook
    Dim wksCurrent          As Worksheet            ' Current sheet
    Dim rCurrentColHeaders  As Range                ' Current column headers

    Dim rCurVarColHeaders   As Range                ' Current Variance column headers (which will be searched through)
    
    Dim rColHead            As Range                ' Iterates through the Imported column headers
    Dim rMatchColHead       As Range                ' Gets the matching Variance column header
    
    Dim iNumCellsPerColumn  As Long                 ' Defines how many cells per column we're copying
    
    Dim CurrentFileToOpen As Variant
    
    ' init
    '==========================================
    ' headers
    Set rCurVarColHeaders = ThisWorkbook.Worksheets("Current").Range("A1:O1")
    ThisWorkbook.Worksheets("Current").Rows("2:" & Rows.Count).ClearContents
    ThisWorkbook.Worksheets("Variance").Rows("2:" & Rows.Count).ClearContents
    '
'
'   Import Current Master Data
'
    CurrentFileToOpen = Application.GetOpenFilename(Title:="Browse for Current Draft File", FileFilter:=" Excel Files(*.xls*),*xls*")
    If CurrentFileToOpen <> False Then
        Set wbCurrent = Application.Workbooks.Open(CurrentFileToOpen)
        Set wksCurrent = wbCurrent.Worksheets("Master")
        Set rCurrentColHeaders = wksCurrent.Range("A1:AZ1")

        ' set number of cells to copy per column (change to suit your needs)
        With wbCurrent.Worksheets("Master")
            iNumCellsPerColumn = .Cells(.Rows.Count, "A").End(xlUp).Row
        End With
        
        ' process
        '==========================================
        ' - loop through the Current column header cells
        ' -- try to find the matching column
        ' -- if a match is found, copy column cells from the Current to the Variance
        For Each rColHead In rCurrentColHeaders
            Set rMatchColHead = rCurVarColHeaders.Find(rColHead.Text, , xlValues, xlWhole)
            If Not (rMatchColHead Is Nothing) Then
                wksCurrent.Range(rColHead, rColHead.Offset(iNumCellsPerColumn, 0)).Copy
                rMatchColHead.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
                    :=False, Transpose:=False
            Application.CutCopyMode = False
            Else
                Debug.Print rColHead & " Header Not Found"
            End If
        Next rColHead
        wbCurrent.Close False
    End If
End Sub

The last two columns are both price columns that could be formulas.. the values are not pasting into the destination, though. Is there something I could be doing better or may be missing to pull this data?
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
There's nothing obviously wrong that jumps out ...

Presumably you've got the right header range: Set rCurrentColHeaders = wksCurrent.Range("A1:AZ1")?

And you're not getting any " Header Not Found" messages?

Assuming your "last two columns" are AY1 and AZ1, perhaps try: Set rCurrentColHeaders = wksCurrent.Range("AY1:AZ1") and step through the code to see what's happening?
 
Upvote 0
There's nothing obviously wrong that jumps out ...

Presumably you've got the right header range: Set rCurrentColHeaders = wksCurrent.Range("A1:AZ1")?

And you're not getting any " Header Not Found" messages?

Assuming your "last two columns" are AY1 and AZ1, perhaps try: Set rCurrentColHeaders = wksCurrent.Range("AY1:AZ1") and step through the code to see what's happening?
Thanks for checking out the syntax. Here is an example of the sheet that is being filled out prior to pulling data..
Variance Report.xlsm
ABCDEFGHIJKLMNO
1LOCRECNDCQTYTRADEGENERICSTRENGTHPACKAGE SIZEDOSE FORMMANUFACTURERDEA CLASSAHFSSOURCEPackage CostExtended
2
Current

Here is an example of the same file after data is pulled from the target file..
Variance Report.xlsm
ABCDEFGHIJKLMNO
1LOCRECNDCQTYTRADEGENERICSTRENGTHPACKAGE SIZEDOSE FORMMANUFACTURERDEA CLASSAHFSSOURCEPackage CostExtended
20024-SCRIPT PRO MACHINE Chaz06165862037310408.00ESCITALOPRAM OXALATEESCITALOPRAM OXALATE5MG/1100ORAL TABLET, FILM COATEDAUROBINDO PHARMA LIMITED28160420-ANTIDEPRESSANTS SELECTIVE SEROTONIN-REUPTAKE INHIBITORS
30014-ALPHA BAY #4JMM1094733507218330.00FEBUXOSTATFEBUXOSTAT40MG/130ORAL TABLET, FILM COATEDSUN PHARMACEUTICAL INDUSTRIES INC.92160000-ANTIGOUT AGENTS
40014-ALPHA BAY #4DCR1525199106232810.00ESTRADIOL AND NORETHINDRONE ACETATEESTRADIOL; NORETHINDRONE ACETATE.5; .1MG/1; MG/128TABLETBRECKENRIDGE PHARMACEUTICAL, INC.68160400-ESTROGENSA: Inventory Price File
50014-ALPHA BAY #4DCR153519910474288.00ESTRADIOL AND NORETHINDRONE ACETATEESTRADIOL; NORETHINDRONE ACETATE1; .5MG/1; MG/128TABLETBRECKENRIDGE PHARMACEUTICAL, INC.68160400-ESTROGENSA: Inventory Price File
Current

And below is an example of the data that is being imported based on the header. The file will be dynamic and the headers may be in different orders. The Package Cost and the Extended columns don't seem to be pulling in, it does print that the Package Cost and Extended Headers were not found.... Sometimes the cells hold dollar prices, sometimes it may have a formula that I need the values from.. There's no problems with columns that are hidden in the Current file.. I can't tell what is preventing the headers from being recognized, if that may be stopping the data from pulling.
CURRENT FILE.xlsm
AEFGHIJKMOPSTVWXYZAAAB
1LOCNDCScanned NDCQTYMIS DivisorMIS Count MethodItem NumberMed DescTRADESTRENGTHPACKAGE SIZEDOSE FORMMANUFACTURERDEA CLASSAHFSSOURCE Package Cost Unit Cost Extended $ 2,720,628.74
20024-SCRIPT PRO MACHINE 65862037310408.00100TABESCITALOPRAM OXALATE 5MG/1 ORAL TABLET, FILM COATEDESCITALOPRAM OXALATE5MG/1100ORAL TABLET, FILM COATEDAUROBINDO PHARMA LIMITED28160420-ANTIDEPRESSANTS SELECTIVE SEROTONIN-REUPTAKE INHIBITORS$ -$ -$ -
30014-ALPHA BAY #44733507218330.0030TABFEBUXOSTAT 40MG 30 FEBUXOSTAT40MG/130ORAL TABLET, FILM COATEDSUN PHARMACEUTICAL INDUSTRIES INC.92160000-ANTIGOUT AGENTS$ 98.03$ 3.27$ 98.03
40014-ALPHA BAY #45199106232810.001BLISTER2146454ESTRAD+NOR0.5/0.1MGBPBRE1X28@ 1ESTRADIOL AND NORETHINDRONE ACETATE.5; .1MG/1; MG/128TABLETBRECKENRIDGE PHARMACEUTICAL, INC.68160400-ESTROGENSA: Inventory Price File$ 98.19$ 98.19$ 981.90
50014-ALPHA BAY #4519910474288.001BLISTER1753169ESTRADIOL-NORETH 1-0.5 MG TAB 1ESTRADIOL AND NORETHINDRONE ACETATE1; .5MG/1; MG/128TABLETBRECKENRIDGE PHARMACEUTICAL, INC.68160400-ESTROGENSA: Inventory Price File$ 54.75$ 54.75$ 438.00
60026-UNIT OF USE BAY #1 518620258014.001BLISTER2324648NEXTSTELLIS 3-14.2 MG TABLET 1NEXTSTELLIS3-14.2MG28ORAL TABLETA: Inventory Price File$ 179.84$ 179.84$ 719.36
70041-ALPHA BAY 9004561500552.001BLISTER2480333SAVELLA TAB 4WK TITRAT PK KIT 1SAVELLA12.5-25-5055TAB DS PKALLERGAN, INC.28400000-FIBROMYALGIA AGENTSA: Inventory Price File$ 383.61$ 383.61$ 767.22
80026-UNIT OF USE BAY #1 00642747001006427470017.001BLISTER3972874SLYND 4 MG TABLET 1SLYND4MG/128TABLETEXELTIS USA, INC.68120000-CONTRACEPTIVESA: Inventory Price File$ 183.62$ 183.62$ 1,285.34
Master
Cell Formulas
RangeFormula
AB1AB1=SUM(AA:AA)
Z2:Z8Z2=IFERROR(Y2/H2,"Error")
AA2:AA8AA2=IFERROR(Z2*G2,"Error")
 
Upvote 0
I've noticed that those specific column headers are in Accounting format, which may be the issue.. if that were the issue, is there a way to bypass the format and treat it like text without making changes to the reference file?
 
Upvote 0
I've noticed that those specific column headers are in Accounting format, which may be the issue.. if that were the issue, is there a way to bypass the format and treat it like text without making changes to the reference file?
I solved the issue by changing the Headers in the Variance file to Accounting format, so it will pull the headers from the Current file with no problem. Is there a better way to resolve this?
 
Upvote 0
Solution
So you should be getting two " Header Not Found" messages. You could consider using MsgBox, rather than Debug.Print, to make this message more in-your-face.

The cell formatting shouldn't be an issue. Based on what you've posted, the mismatch in these two headers occurs because of leading/trailing spaces.

Your code could apply the TRIM function to both source and target headers to eliminate this possibility.
 
Upvote 0

Forum statistics

Threads
1,215,043
Messages
6,122,822
Members
449,096
Latest member
Erald

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top