+ Reply to Thread
Results 1 to 28 of 28

Macro to Copy Cells From One Workbook into Another as Text

Hybrid View

  1. #1
    Registered User
    Join Date
    08-18-2012
    Location
    St. Louis
    MS-Off Ver
    Excel 2010
    Posts
    18

    Question Macro to Copy Cells From One Workbook into Another as Text

    Hello,

    I have 86 separate files that analyze account volume in the 86 different sales territories. Each file has four tabs, one for each product category. I need to compile a master for the entire organization and would like to use a macro to do so. The account information begins at row 11 (column a) on each tab in the individual files. The macro would essentially need to take the cells, starting at row 11 and paste their values into the next available row in the corresponding tab (column a) in the Master file. If possible I'd also like the macro to paste back over the copied rows, in the individual files, as values as well, and then save the file under a different name in the same folder (e.g. C:\X\Y\example.xlsx->C:\X\Y\example1.xlsx where example.xlsx would be the original individual file and example1.xlsx would be the individual file with the values pasted over the formulas).

    Thanks in advance for your help!

  2. #2
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Macro to Copy Cells From One Workbook into Another as Text

    'WORKBOOKS W/MULTIPLE SHEETS MERGED INTO SHEETS IN CONSOLIDATION WORKBOOK
    Here's a macro for collecting data from all sheets in all files in a specific folder merging into matching sheets.

    The parts of the code that need to be edited are colored to draw your attention.

    ================
    To copy from row11 and then paste as values, you'd need to change this one line of code:
                        .Range("A2:A" & LR).EntireRow.Copy wsMain.Range("A" & NR)
    to these lines of code, this also changes the original workbook to values only:
                        .Range("A11:A" & LR).EntireRow.Copy 
                         wsMain.Range("A" & NR).PasteSpecial xlPasteValuesAndNumberFormats
                        .UsedRange.Copy
                        .Range("A1").PasteSpecial xlPasteValues

    To save the original file with other filenames would be a change here:
                wbData.Close False
    to this, perhaps:
                fName = Left(fName, InStrRev(fName, ".") - 1) & "1.xlsx"
                wbData.SaveAs fPath & fName, FileFormat:=51
                wbData.Close False
    I think this is a patently bad idea, by they way. You're in the process of cycling through files in a folder and then you start adding files to that folder.... I bet this macro would run out of control.

    I'd recommend you have a folder INSIDE this folder called "Values" and you save these new copies into that folder instead.

                fName = Left(fName, InStrRev(fName, ".") - 1) & "1.xlsx"
                wbData.SaveAs fPath & "\Values\" & fName, FileFormat:=51
                wbData.Close False
    Last edited by JBeaucaire; 12-27-2019 at 10:14 PM.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  3. #3
    Registered User
    Join Date
    08-18-2012
    Location
    St. Louis
    MS-Off Ver
    Excel 2010
    Posts
    18

    Re: Macro to Copy Cells From One Workbook into Another as Text

    Quote Originally Posted by JBeaucaire View Post

    I think this is a patently bad idea, by they way. You're in the process of cycling through files in a folder and then you start adding files to that folder.... I bet this macro would run out of control.

    I'd recommend you have a folder INSIDE this folder called "Values" and you save these new copies into that folder instead.

    [/CODE]
    If I'm reading your post correctly, the macro would actually open all of the individual files in the folder and paste their contents as values into the corresponding tab in the master?? If so I could definitely see how saving the new individual (value) files in the folder could be a problem, otherwise, I don't follow how it would.

  4. #4
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Macro to Copy Cells From One Workbook into Another as Text

    No, the macro is grabbing files "one at a time" from the folder you designate. If the number of files in that SAME folder were to get bigger and bigger as the macro proceeds, it would never end. It would eventually start finding the files you had just added and open them and do the same thing... kind of crazy.

    I suppose you could test each filename before you open it to make sure it doesn't have "1.xlsx" in the filename....

    But I'd still save new files to a different folder to not mix them.

  5. #5
    Registered User
    Join Date
    08-18-2012
    Location
    St. Louis
    MS-Off Ver
    Excel 2010
    Posts
    18

    Re: Macro to Copy Cells From One Workbook into Another as Text

    Hey Jerry,

    I followed the directions for #4 in the linked site and made the edits recommended above. In the process I went with your suggestion and created a "values" folder so the macro would not run endlessly. I then created a master file in the same folder as the data files, opened that one, created 4 tabs, named them as they're named in the individual files, and ran the macro (which is saved in a different file that contains all of my macros) and it returned an error that reads "Run-time error '9': Subscript out of range."

    Here is the code, with the problem (as identified by the debugger) in yellow font:

    Dim wbData As Workbook, wbMain As Workbook
        Dim wsMain As Worksheet, wsData As Worksheet
        Dim LR As Long, NR As Long
        Dim fPath As String, fName As String
    
        Set wbMain = ThisWorkbook
    
                                        'if files are stored in separate directory edit fPath
        fPath = ThisWorkbook.Path & "\"     'don't forget the final \
                                        
        fName = Dir(fPath & "*.xlsx")        'start looping through files one at a time
        Application.ScreenUpdating = False
    
        Do While Len(fName) > 0
            If fName <> ThisWorkbook.Name Then
                Set wbData = Workbooks.Open(fPath & fName)
                For Each wsData In wbData.Worksheets
                    Set wsMain = wbMain.Sheets(wsData.Name)
                    NR = wsMain.Range("A" & Rows.Count).End(xlUp).Row + 1
                    With wsData
                        LR = .Range("A" & .Rows.Count).End(xlUp).Row
                        .Range("A11:A" & LR).EntireRow.Copy
                         wsMain.Range("A" & NR).PasteSpecial xlPasteValuesAndNumberFormats
                        .UsedRange.Copy
                        .Range("A1").PasteSpecial xlPasteValues
                    End With
                Next wsData
                
                fName = Left(fName, InStrRev(fName, ".") - 1) & "1.xlsx"
                wbData.SaveAs fPath & "\Values\" & fName, FileFormat:=51
                wbData.Close False
            End If
            
            fName = Dir                 'queue up next filename
        Loop
    
        Application.ScreenUpdating = True
    End Sub

    Any ideas??


    Thanks again for your help.
    Last edited by JBeaucaire; 03-08-2013 at 01:43 PM. Reason: Added code tags, as per forum rules. Don't forget!

  6. #6
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Macro to Copy Cells From One Workbook into Another as Text

    My bad, this will probably need to be fixed, too:
    wbData.SaveAs fPath & "Values\" & fName, FileFormat:=51

  7. #7
    Registered User
    Join Date
    08-18-2012
    Location
    St. Louis
    MS-Off Ver
    Excel 2010
    Posts
    18

    Re: Macro to Copy Cells From One Workbook into Another as Text

    Jerry,

    I opened one of the individual sheets, copied the tab names and pasted them as the tab names in the master and I'm still getting the same error....any ideas??

  8. #8
    Registered User
    Join Date
    08-18-2012
    Location
    St. Louis
    MS-Off Ver
    Excel 2010
    Posts
    18

    Re: Macro to Copy Cells From One Workbook into Another as Text

    I also tried selecting the tabs in the individual files, copying the tabs to the master, and clearing off all the data, so, in theory, the names would be identical and got the same error

  9. #9
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Macro to Copy Cells From One Workbook into Another as Text

    "Run-time error '9': Subscript out of range."

    This means an named object cannot be found by the exact name specified. This is often due to simple misspellings, but other times to hidden spaces and such that make tab names LOOK the same but are not when examined very closely. You will need to identify the exact error in sheet tab names, because this is exactly what that error means.

  10. #10
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Macro to Copy Cells From One Workbook into Another as Text

    send me your master file and a source file giving the error, I'll test it out.

    Click GO ADVANCED and use the paperclip icon to post up a copy of your workbooks.

  11. #11
    Registered User
    Join Date
    08-18-2012
    Location
    St. Louis
    MS-Off Ver
    Excel 2010
    Posts
    18

    Re: Macro to Copy Cells From One Workbook into Another as Text

    Is there a way I can send them to you privately?? I'd rather not post the files to a public forum....

  12. #12
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Macro to Copy Cells From One Workbook into Another as Text

    One would expect you replace sensitive data with junk data, whether sent privately or posted to the forum. To test this particular problem you could actually replace the data with absolute garbage since it's irrelevant. We're just trying to examine your sheet names and how the macro is coercing them.

  13. #13
    Registered User
    Join Date
    08-18-2012
    Location
    St. Louis
    MS-Off Ver
    Excel 2010
    Posts
    18

    Re: Macro to Copy Cells From One Workbook into Another as Text

    I gotcha.

    I've attached an individual and master with dummy data in them.

    Thanks again for the help!
    Attached Files Attached Files

  14. #14
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Macro to Copy Cells From One Workbook into Another as Text

    Anyway, I took the two files above and the macro from further up and it worked bingo bango. Need to update this, though, never expect people to skip the top rows/columns, but some peeps, do...
                        .UsedRange.Copy
                        .UsedRange.PasteSpecial xlPasteValues

  15. #15
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Macro to Copy Cells From One Workbook into Another as Text

    Um, wouldn't your edited macro be in the Master? I don't see it installed there.

  16. #16
    Registered User
    Join Date
    08-18-2012
    Location
    St. Louis
    MS-Off Ver
    Excel 2010
    Posts
    18

    Re: Macro to Copy Cells From One Workbook into Another as Text

    No, I put the macro in another file where I store all the macros I use regularly. Do you think this could be the issue??

  17. #17
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Macro to Copy Cells From One Workbook into Another as Text

    Maybe. THe original macro uses a THISWORKBOOK reference, I expected the macro to be installed in the Master. That's why the sheet names aren't matching up, it's looking at your other workbook.

    Be sure to make the edit I noted above.

  18. #18
    Registered User
    Join Date
    08-18-2012
    Location
    St. Louis
    MS-Off Ver
    Excel 2010
    Posts
    18

    Re: Macro to Copy Cells From One Workbook into Another as Text

    Jerry,

    I made the change and dropped that macro into the master file. Worked like a charm!....but it brought up another issue I didn't think of. Is there a way we can paste the value for "Our Avg Share in Top 80%" as a value in the next empty column to the right of opportunity? This number, the cell it's in, and the column it would be pasted to differ across the different sheets, however they are uniform from file to file, meaning in sheet "Category A" (across all individual files) it's in cell M8 and would need to be pasted to column Q, but in sheet "Category C" the value is in cell G8 and would need to be pasted into column K.

    Also, I'm only interested in stores that are in the top 80%. that being said is there any way to edit the macro so that it only copies rows in which the value in the column marked "Cumulative % of Business" is less than .8?? If not no biggie, the issue above is much more important.

    Any thoughts??

    Thanks again.

  19. #19
    Registered User
    Join Date
    08-18-2012
    Location
    St. Louis
    MS-Off Ver
    Excel 2010
    Posts
    18

    Re: Macro to Copy Cells From One Workbook into Another as Text

    Quote Originally Posted by balandri View Post
    Jerry,

    I made the change and dropped that macro into the master file. Worked like a charm!....but it brought up another issue I didn't think of. Is there a way we can paste the value for "Our Avg Share in Top 80%" as a value in the next empty column to the right of opportunity? This number, the cell it's in, and the column it would be pasted to differ across the different sheets, however they are uniform from file to file, meaning in sheet "Category A" (across all individual files) it's in cell M8 and would need to be pasted to column Q, but in sheet "Category C" the value is in cell G8 and would need to be pasted into column K.

    Thanks again.
    I don't see the part, in your code, where it's doing this....

  20. #20
    Registered User
    Join Date
    08-18-2012
    Location
    St. Louis
    MS-Off Ver
    Excel 2010
    Posts
    18

    Re: Macro to Copy Cells From One Workbook into Another as Text

    Jerry,

    I was able to get the first part done myself, any insight on that second question?? The top 80%?

  21. #21
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Macro to Copy Cells From One Workbook into Another as Text

    Have you tried using the autofilter? You can filter a column of data using the autofilter to only shows rows with a value of .8 or less (is less right?) before you copy and paste.

  22. #22
    Registered User
    Join Date
    08-18-2012
    Location
    St. Louis
    MS-Off Ver
    Excel 2010
    Posts
    18

    Re: Macro to Copy Cells From One Workbook into Another as Text

    I have not, reason being, I'd have to open all 86 individual files, apply the filter, and then run this macro. If that's the case it's probably just as easy to sort the master by that column and delete out all the accounts that are above .8.

    Thanks again.

  23. #23
    Registered User
    Join Date
    08-18-2012
    Location
    St. Louis
    MS-Off Ver
    Excel 2010
    Posts
    18

    Re: Macro to Copy Cells From One Workbook into Another as Text

    And yes, "less" is correct. In the individual files the accounts are sorted by volume in descending order, so once that "Cumulative % of Business" column reaches .8, we've accounted for the top 80%.

  24. #24
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Macro to Copy Cells From One Workbook into Another as Text

    Heavens, no, you'd have the macro autofilter for you before the copy command.

  25. #25
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Macro to Copy Cells From One Workbook into Another as Text

    Assuming the column to filter is column 13 (column M) the first row of data is row11 (apply filter to row 10), something like this:

                    With wsData
                        .UsedRange.Copy
                        .UsedRange.PasteSpecial xlPasteValues
                        .Rows(10).AutoFilter
                        .Rows(10).AutoFilter Field:=13, Criteria1:="<=0.8"
                        LR = .Range("A" & .Rows.Count).End(xlUp).Row
                        .Range("A11:A" & LR).EntireRow.Copy
                        wsMain.Range("A" & NR).PasteSpecial xlPasteValuesAndNumberFormats
                        .AutoFilterMode = False
                    End With

  26. #26
    Registered User
    Join Date
    08-18-2012
    Location
    St. Louis
    MS-Off Ver
    Excel 2010
    Posts
    18

    Re: Macro to Copy Cells From One Workbook into Another as Text

    Hey Jerry,

    The code you posted for this last part is fine, the problem is the column and row vary from sheet to sheet, which is completely my fault for not making them uniform. That being said I tried to work around it by writing that code into the part where I copy the "Cumulative Share Avg." value in each sheet (because in that part I go sheet by sheet), but it gave me the "Run-Time Error '91': Object Variable or With Block Variable Not Set" error. I've copied the code below with the portion the debugger highlights in yellow text. I think this is because at this point in the code WSMain has not been defined, I'm just not sure how to fix it.....


    Sub CopyValuesToMaster()
    '
    ' CopyValuesToMaster Macro This macro copies all of the individual TR files to a master district file as values so it's editable without blowing up the sheet
    '
    
    '
        Dim wbData As Workbook, wbMain As Workbook
        Dim wsMain As Worksheet, wsData As Worksheet
        Dim LR As Long, NR As Long
        Dim fPath As String, fName As String
    
        Set wbMain = ThisWorkbook
    
                                        'if files are stored in separate directory edit fPath
        fPath = ThisWorkbook.Path & "\"     'don't forget the final \
                                        
        fName = Dir(fPath & "*.xlsx")        'start looping through files one at a time
        Application.ScreenUpdating = False
    
        Do While Len(fName) > 0
            If fName <> ThisWorkbook.Name Then
                Set wbData = Workbooks.Open(fPath & fName)
        Sheets("Snuff").Select
        Range("Q12").Select
        ActiveCell.FormulaR1C1 = "=R8C13"
        Range("Q12").Select
        Selection.AutoFill Destination:=Range("Q12:Q1000"), Type:=xlFillDefault
        Range("Q12:Q1000").Select
                    With ActiveSheet
                        .UsedRange.Copy
                        .UsedRange.PasteSpecial xlPasteValues
                        .Rows(11).AutoFilter
                        .Rows(11).AutoFilter Field:=12, Criteria1:="<=0.8"
                        LR = .Range("A" & .Rows.Count).End(xlUp).Row
                        .Range("A11:A" & LR).EntireRow.Copy
                        wsMain.Range("A" & NR).PasteSpecial xlPasteValuesAndNumberFormats
                        .AutoFilterMode = False
                    End With
        Sheets("Cigars").Select
        Range("Q12").Select
        ActiveCell.FormulaR1C1 = "=R8C11"
        Range("Q12").Select
        Selection.AutoFill Destination:=Range("Q12:Q1000"), Type:=xlFillDefault
        Range("Q12:Q1000").Select
                    With ActiveSheet
                        .UsedRange.Copy
                        .UsedRange.PasteSpecial xlPasteValues
                        .Rows(11).AutoFilter
                        .Rows(11).AutoFilter Field:=12, Criteria1:="<=0.8"
                        LR = .Range("A" & .Rows.Count).End(xlUp).Row
                        .Range("A11:A" & LR).EntireRow.Copy
                        wsMain.Range("A" & NR).PasteSpecial xlPasteValuesAndNumberFormats
                        .AutoFilterMode = False
                    End With
        Sheets("LL").Select
        Range("K12").Select
        ActiveCell.FormulaR1C1 = "=R8C7"
        Range("K12").Select
        Selection.AutoFill Destination:=Range("K12:K1000"), Type:=xlFillDefault
        Range("K12:K1000").Select
                    With ActiveSheet
                        .UsedRange.Copy
                        .UsedRange.PasteSpecial xlPasteValues
                        .Rows(11).AutoFilter
                        .Rows(11).AutoFilter Field:=6, Criteria1:="<=0.8"
                        LR = .Range("A" & .Rows.Count).End(xlUp).Row
                        .Range("A11:A" & LR).EntireRow.Copy
                        wsMain.Range("A" & NR).PasteSpecial xlPasteValuesAndNumberFormats
                        .AutoFilterMode = False
                    End With
        Sheets("Snus").Select
        Range("K12").Select
        ActiveCell.FormulaR1C1 = "=R8C7"
        Range("K12").Select
        Selection.AutoFill Destination:=Range("K12:K1000"), Type:=xlFillDefault
        Range("K12:K1000").Select
                    With ActiveSheet
                        .UsedRange.Copy
                        .UsedRange.PasteSpecial xlPasteValues
                        .Rows(11).AutoFilter
                        .Rows(11).AutoFilter Field:=6, Criteria1:="<=0.8"
                        LR = .Range("A" & .Rows.Count).End(xlUp).Row
                        .Range("A11:A" & LR).EntireRow.Copy
                        wsMain.Range("A" & NR).PasteSpecial xlPasteValuesAndNumberFormats
                        .AutoFilterMode = False
                    End With
        Sheets("Overall ($)").Select
        Range("T11").Select
        ActiveCell.FormulaR1C1 = "=R8C7"
        Range("T11").Select
        Selection.AutoFill Destination:=Range("T11:T1000"), Type:=xlFillDefault
        Range("T11:T1000").Select
                    With ActiveSheet
                        .UsedRange.Copy
                        .UsedRange.PasteSpecial xlPasteValues
                        .Rows(10).AutoFilter
                        .Rows(10).AutoFilter Field:=14, Criteria1:="<=0.8"
                        LR = .Range("A" & .Rows.Count).End(xlUp).Row
                        .Range("A10:A" & LR).EntireRow.Copy
                        wsMain.Range("A" & NR).PasteSpecial xlPasteValuesAndNumberFormats
                        .AutoFilterMode = False
                    End With
                For Each wsData In wbData.Worksheets
                    Set wsMain = wbMain.Sheets(wsData.Name)
                    NR = wsMain.Range("A" & Rows.Count).End(xlUp).Row + 1
                    With wsData
                        LR = .Range("A" & .Rows.Count).End(xlUp).Row
                        .Range("A11:A" & LR).EntireRow.Copy
                         wsMain.Range("A" & NR).PasteSpecial xlPasteValuesAndNumberFormats
                        .UsedRange.Copy
                        .UsedRange.PasteSpecial xlPasteValues
                    End With
                Next wsData
                
                fName = Left(fName, InStrRev(fName, ".") - 1) & "1.xlsx"
                wbData.SaveAs fPath & "Values\" & fName, FileFormat:=51
                wbData.Close False
            End If
            
            fName = Dir                 'queue up next filename
        Loop
    
        Application.ScreenUpdating = True
    End Sub
    Last edited by balandri; 03-13-2013 at 11:49 AM.

  27. #27
    Registered User
    Join Date
    08-18-2012
    Location
    St. Louis
    MS-Off Ver
    Excel 2010
    Posts
    18

    Re: Macro to Copy Cells From One Workbook into Another as Text

    Ok, I changed the code to the following, and it looks like it worked:


    Sub CopyValuesToMaster()
    '
    ' CopyValuesToMaster Macro This macro copies all of the individual TR files to a master district file as values so it's editable without blowing up the sheet
    '
    
    '
        Dim wbData As Workbook, wbMain As Workbook
        Dim wsMain As Worksheet, wsData As Worksheet
        Dim LR As Long, NR As Long
        Dim fPath As String, fName As String
    
        Set wbMain = ThisWorkbook
    
                                        'if files are stored in separate directory edit fPath
        fPath = ThisWorkbook.Path & "\"     'don't forget the final \
                                        
        fName = Dir(fPath & "*.xlsx")        'start looping through files one at a time
        Application.ScreenUpdating = False
    
        Do While Len(fName) > 0
            If fName <> ThisWorkbook.Name Then
                Set wbData = Workbooks.Open(fPath & fName)
        Sheets("Snuff").Select
        Range("Q12").Select
        ActiveCell.FormulaR1C1 = "=R8C13"
        Range("Q12").Select
        Selection.AutoFill Destination:=Range("Q12:Q1000"), Type:=xlFillDefault
        Range("Q12:Q1000").Select
                    With ActiveSheet
                        .UsedRange.Copy
                        .UsedRange.PasteSpecial xlPasteValues
                        .Rows(11).AutoFilter
                        .Rows(11).AutoFilter Field:=12, Criteria1:="<=0.8"
                        LR = .Range("A" & .Rows.Count).End(xlUp).Row
                        .Range("A11:A" & LR).EntireRow.Copy
                    End With
        Sheets("Cigars").Select
        Range("Q12").Select
        ActiveCell.FormulaR1C1 = "=R8C11"
        Range("Q12").Select
        Selection.AutoFill Destination:=Range("Q12:Q1000"), Type:=xlFillDefault
        Range("Q12:Q1000").Select
                    With ActiveSheet
                        .UsedRange.Copy
                        .UsedRange.PasteSpecial xlPasteValues
                        .Rows(11).AutoFilter
                        .Rows(11).AutoFilter Field:=12, Criteria1:="<=0.8"
                        LR = .Range("A" & .Rows.Count).End(xlUp).Row
                        .Range("A11:A" & LR).EntireRow.Copy
                    End With
        Sheets("LL").Select
        Range("K12").Select
        ActiveCell.FormulaR1C1 = "=R8C7"
        Range("K12").Select
        Selection.AutoFill Destination:=Range("K12:K1000"), Type:=xlFillDefault
        Range("K12:K1000").Select
                    With ActiveSheet
                        .UsedRange.Copy
                        .UsedRange.PasteSpecial xlPasteValues
                        .Rows(11).AutoFilter
                        .Rows(11).AutoFilter Field:=6, Criteria1:="<=0.8"
                        LR = .Range("A" & .Rows.Count).End(xlUp).Row
                        .Range("A11:A" & LR).EntireRow.Copy
                    End With
        Sheets("Snus").Select
        Range("K12").Select
        ActiveCell.FormulaR1C1 = "=R8C7"
        Range("K12").Select
        Selection.AutoFill Destination:=Range("K12:K1000"), Type:=xlFillDefault
        Range("K12:K1000").Select
                    With ActiveSheet
                        .UsedRange.Copy
                        .UsedRange.PasteSpecial xlPasteValues
                        .Rows(11).AutoFilter
                        .Rows(11).AutoFilter Field:=6, Criteria1:="<=0.8"
                        LR = .Range("A" & .Rows.Count).End(xlUp).Row
                        .Range("A11:A" & LR).EntireRow.Copy
                    End With
        Sheets("Overall ($)").Select
        Range("T11").Select
        ActiveCell.FormulaR1C1 = "=R8C7"
        Range("T11").Select
        Selection.AutoFill Destination:=Range("T11:T1000"), Type:=xlFillDefault
        Range("T11:T1000").Select
                    With ActiveSheet
                        .UsedRange.Copy
                        .UsedRange.PasteSpecial xlPasteValues
                        .Rows(10).AutoFilter
                        .Rows(10).AutoFilter Field:=14, Criteria1:="<=0.8"
                        LR = .Range("A" & .Rows.Count).End(xlUp).Row
                        .Range("A10:A" & LR).EntireRow.Copy
                    End With
                For Each wsData In wbData.Worksheets
                    Set wsMain = wbMain.Sheets(wsData.Name)
                    NR = wsMain.Range("A" & Rows.Count).End(xlUp).Row + 1
                    With wsData
                        LR = .Range("A" & .Rows.Count).End(xlUp).Row
                        .Range("A11:A" & LR).EntireRow.Copy
                         wsMain.Range("A" & NR).PasteSpecial xlPasteValuesAndNumberFormats
                        .AutoFilterMode = False
                        .UsedRange.Copy
                        .UsedRange.PasteSpecial xlPasteValues
                    End With
                Next wsData
                
                fName = Left(fName, InStrRev(fName, ".") - 1) & "1.xlsx"
                wbData.SaveAs fPath & "Values\" & fName, FileFormat:=51
                wbData.Close False
            End If
            
            fName = Dir                 'queue up next filename
        Loop
    
        Application.ScreenUpdating = True
    End Sub
    Do you see any potential issues with it?? Also, it runs fairly slow, is it because I'm making it go sheet by sheet??

    Thanks again for all your help.

  28. #28
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Macro to Copy Cells From One Workbook into Another as Text

    Maybe this:
    Option Explicit
    
    Sub CopyValuesToMaster()
    ' This macro copies all of the individual TR files to a master district
    ' file as values so it's editable without blowing up the sheet
        
    Dim wbData As Workbook, wbMain As Workbook
    Dim wsMain As Worksheet, wsData As Worksheet
    Dim LR As Long, NR As Long
    Dim fPath As String, fName As String
    
    Set wbMain = ThisWorkbook
    NR = wsMain.Range("A" & Rows.Count).End(xlUp).Row + 1   'next empty row on main
    fPath = ThisWorkbook.Path & "\"             'don't forget the final \
    
    fName = Dir(fPath & "*.xlsx")               'start looping through files one at a time
    Application.ScreenUpdating = False          'speed up macro by turning off screen
    
    Do While Len(fName) > 0
        If fName <> ThisWorkbook.Name Then
            Set wbData = Workbooks.Open(fPath & fName)              'open the found file
                With wbData.Sheets("Snuff")                         'process a specific sheet
                    LR = .Range("A" & .Rows.Count).End(xlUp).Row    'find last row with data
                    .Range("Q12:Q" & LR).FormulaR1C1 = "=R8C13"
                End With
                With Sheets("Cigars")
                    LR = .Range("A" & .Rows.Count).End(xlUp).Row    'find last row with data
                    .Range("Q12:Q" & LR).FormulaR1C1 = "=R8C11"
                End With
                With Sheets("LL")
                    LR = .Range("A" & .Rows.Count).End(xlUp).Row    'find last row with data
                    .Range("K12:K" & LR).FormulaR1C1 = "=R8C7"
                End With
                With Sheets("Snus")
                    LR = .Range("A" & .Rows.Count).End(xlUp).Row    'find last row with data
                    .Range("K12:K" & LR).FormulaR1C1 = "=R8C7"
                End With
                With Sheets("Overall ($)")
                    LR = .Range("A" & .Rows.Count).End(xlUp).Row    'find last row with data
                    .Range("T11:T" & LR).FormulaR1C1 = "=R8C7"
                End With
    
                For Each wsData In wbData.Worksheets
                    Set wsMain = wbMain.Sheets(wsData.Name)
                    NR = wsMain.Range("A" & Rows.Count).End(xlUp).Row + 1
                    With wsData
                        LR = .Range("A" & .Rows.Count).End(xlUp).Row
                        .Range("A11:A" & LR).EntireRow.Copy
                         wsMain.Range("A" & NR).PasteSpecial xlPasteValuesAndNumberFormats
                        .AutoFilterMode = False
                        .UsedRange.Copy
                        .UsedRange.PasteSpecial xlPasteValues
                    End With
                Next wsData
    
                fName = Left(fName, InStrRev(fName, ".") - 1) & "1.xlsx"
                wbData.SaveAs fPath & "Values\" & fName, FileFormat:=51
                wbData.Close False
            End If
    
            fName = Dir                 'queue up next filename
        Loop
    
        Application.ScreenUpdating = True
    End Sub

    The goal in final tweaking is to remove all "selecting" and "Activating" and send all your commands to the target sheets/workbooks using your variables.

+ 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