+ Reply to Thread
Results 1 to 5 of 5

How to combine a code and a macro

Hybrid 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.

  2. #2
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,524

    Re: How to combine a code and a macro

    Try this,
    I used a sheet names "Start". When you run the code, the code will delete all the sheets except sheets("Start")
    
    Sub OPenMultipleWorkbooks()
    'Open Multiple .csv files, this workbooks name is, 'Compare Workbook.xlsm'
        Dim wb As Workbook
        Dim GetFile As Variant
    
        Set wb = ThisWorkbook
        Application.DisplayAlerts = False
        
        For Each Sheet In Sheets
            If Sheet.Name <> "Start" Then Sheet.Delete
        Next Sheet
        ChDrive "C:"
    
        GetFile = Application.GetOpenFilename(FileFilter:="CSV (*.CSV), *.CSV", Title:="Open CSV File", MultiSelect:=True)
        Application.ScreenUpdating = False
    
        On Error Resume Next
    
        If GetFile <> False Then
            On Error GoTo 0
            For i = 1 To UBound(GetFile)
    
                Workbooks.Open Filename:=GetFile(i)
                Sheets(1).Move Before:=wb.Sheets(1)
    
            Next i
        End If
        CombineDts
    End Sub
    Sub CombineDts()
        Dim LstRw As Long, rng As Range, sh As Worksheet
        Dim Frmla As String
    
        Frmla = "=DATEVALUE(C2&"" / ""&D2&"" / ""&B2)"
        For Each sh In Sheets
            With sh
                If .Range("A1") = "ID" Then
                    LstRw = .Cells(.Rows.Count, "A").End(xlUp).Row
                    Set rng = .Range("A2:A" & LstRw)
                    rng = Frmla
                    rng.Value = rng.Value
                    rng.NumberFormat = "dd/mm/yyyy"
                    .Range("A1") = "Date"
                    .Columns("B:D").Delete Shift:=xlToLeft
                End If
            End With
        Next sh
    End Sub
    Attached Files Attached Files
    Last edited by davesexcel; 12-20-2015 at 09:43 AM.

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

    Re: How to combine a code and a macro

    It works like a charm. Thank you very much for the time.
    May I ask you one more question? IF POSSIBLE !
    Is it possible to guide me how to do VLOOKUP in this code between the sheets based on "DATE" column?
    I can do it by using the excel VLOOKUP, but how can I extend it to multiple sheets?

  4. #4
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,524

    Re: How to combine a code and a macro

    Enter the date in Sheets("Starts") range("A2") and run the code.



    Sub FindDateValue()
        Dim d As Range, c As Range
        Dim sh As Worksheet
        Dim rws As Long, rng As Range
        
        Set d = Sheets("Start").Range("A2") 'date to find
        
        For Each sh In Sheets
        
            If sh.Name <> "Start" Then
                With sh
                    rws = .Cells(.Rows.Count, "A").End(xlUp).Row
                    Set rng = .Range("A2:A" & rws)
                    Set c = rng.Find(what:=d, lookat:=xlWhole)
                    If Not c Is Nothing Then
                        Sheets("Start").Cells(.Rows.Count, "A").End(xlUp).Offset(1, 0) = sh.Name
                        Sheets("Start").Cells(.Rows.Count, "A").End(xlUp).Offset(0, 1).Value = sh.Cells(c.Row, 2).Value
    
                    Else:
                    
                    End If
                End With
            End If
        Next sh
        
    End Sub

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

    Re: How to combine a code and a macro

    Thanks a million.

+ Reply to Thread

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