Results 1 to 5 of 5

How to combine a code and a macro

Threaded View

  1. #1
    Registered User
    Join Date
    11-08-2014
    Location
    Malaysia
    MS-Off Ver
    2010
    Posts
    84

    Thumbs up How to combine a code and a macro

    Hello folks,
    I have a code and recorded macro. Could you please help me to merge the codes into one to be able to work with different range of data set?

    Code #1: Combine files in a folder into a workbook
    Sub MergeFiles()
        Dim x As Integer
            Const strDir = "C:\Mydoc\"
        Dim ThisWB As Workbook
        Dim wb As Workbook
        Dim ws As Worksheet 
        Dim strWS As String
        Dim sFileName As String
        Set ThisWB = ActiveWorkbook
        
        With Application.FileDialog(msoFileDialogOpen)
        
            .InitialFileName = strDir
            .Filters.Clear
            .Filters.Add "CSV Files", "*.csv"
            .Application.DisplayAlerts = False
            If .Show Then
                For x = 1 To .SelectedItems.Count
                    Set wb = Workbooks.Open(.SelectedItems(x))
                    sFileName = .SelectedItems(x)
                    Set ws = ThisWB.Worksheets.Add(before:=ThisWB.Worksheets(1))
                    ws.Name = ActiveSheet.Name
                    wb.Sheets(1).UsedRange.Copy ws.Range("A1")
                    wb.Close False
                Next x
            Else
                MsgBox "There were no files found."
            End If
        End With
    End Sub
    Code #2: A recorded macro to convert 5 columns worksheet to 3 columns (Combining YMD columns into "Date" column)
    The problem here is, I have to run the code for each worksheet separately, and I don't know how to do it for a workbook since the data range is not fixed.
    I couldn't use the "Range("A" & Rows.Count).End(xlUp).Row" function correctly.
    '2nd code for each sheet! How to do it for all sheets in a workbook?
    Sub Macro5to3col()
    'A recorded macro 
    'ColA = ID, ColB:D = Year,Month,Day & ColE = Value
    'The goal is: Deleting ID, and combining YMD cols into "Date" column
        
        Columns("A:A").Select
        Selection.Delete Shift:=xlToLeft    'delete column A
        Columns("D:D").Select               'insert two columns
        Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
        Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
        Range("D1").Select
        ActiveCell.FormulaR1C1 = "Datetemp"    'a temporary Date column to avoid #REF! error
        Range("D2").Select
        ActiveCell.FormulaR1C1 = "=DATE(RC[-3],RC[-2],RC[-1])"   '=Date(Y,M,D) function
        Selection.AutoFill Destination:=Range("D2:D23012")       'The range is not fixed
        Range("D2:D23012").Select
        Selection.Copy
        Range("E2").Select
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False         'Copy "Datetemp" column & paste as value in next column
        Application.CutCopyMode = False
        Selection.NumberFormat = "d/m/yyyy"
        Range("E1").Select
        ActiveCell.FormulaR1C1 = "Date"
        Columns("A:D").Select                 'delete YMD & Datetemp columns
        Selection.Delete Shift:=xlToLeft
    End Sub
    Attached Files Attached Files
    Last edited by Moriexcel; 12-20-2015 at 10:00 AM.

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Combine two pieces of code together
    By ttontis in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-29-2015, 12:41 PM
  2. [SOLVED] how to combine code
    By ammartino44 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-05-2015, 02:02 PM
  3. [SOLVED] Change Event Code too long - Need to combine 2 events into 1 macro
    By Sc0tt1e in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-15-2014, 03:57 AM
  4. [SOLVED] If code same combine the description
    By Klitos in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 03-28-2013, 05:16 AM
  5. [SOLVED] Combine code in VBA
    By clifton1230 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-16-2013, 12:21 AM
  6. [SOLVED] Using Combine macro to combine multiple worksheets - need to modify to paste formulas
    By DLSmith in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-26-2012, 09:07 AM
  7. combine code
    By Dennisli2000 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-12-2008, 12:17 AM

Tags for this Thread

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