+ Reply to Thread
Results 1 to 3 of 3

Create a macro to remove previous data from an excel and populate with data in another

Hybrid View

nupurs250 Create a macro to remove... 06-06-2023, 01:05 PM
ranman256 Re: Create a macro to remove... 06-06-2023, 01:48 PM
nupurs250 Re: Create a macro to remove... 06-06-2023, 02:27 PM
  1. #1
    Registered User
    Join Date
    06-03-2023
    Location
    New Jersey
    MS-Off Ver
    MS 3655
    Posts
    29

    Create a macro to remove previous data from an excel and populate with data in another

    Hi

    I am trying to create a macro or VBA script where I can remove the data stored previously and then automatically populate it with data stored in another workbook.

    Apart from this also looking for macro where I can club data from many workbooks into a single workbook and same sheet.

    Thanks a tons!

  2. #2
    Valued Forum Contributor ranman256's Avatar
    Join Date
    07-29-2012
    Location
    Kentucky
    MS-Off Ver
    Excel 2003
    Posts
    1,192

    Re: Create a macro to remove previous data from an excel and populate with data in another

    Here is code to cycle thru all excel files in given folder,
    then combine the data into 1 sheet.

    here, the given start folder is in: Range("B1").Value
    ex: C:\temp

    paste this code into a module, then run: CombineFiles

    Private wbSrc As Workbook, wbTarg As Workbook
    
    Sub CombineFiles()
    ScanFilesIn1Folder Range("B1").Value
    End Sub
    
    
    Private Sub ScanFilesIn1Folder(ByVal pvStartDir)
    Dim FileSystem As Object
    Dim Folder As Object
    Dim oFile As Object
    Dim i As Integer
    Dim wbStart As Workbook
    
    If IsNull(pvStartDir) Then
      MsgBox "No start folder"
      Exit Sub
    End If
    
    Set wbStart = ActiveWorkbook
    Set FileSystem = CreateObject("Scripting.FileSystemObject")
    Set Folder = FileSystem.GetFolder(pvStartDir)
    
    Range("K1").Value = "files"
    Range("K2").Select
    For Each oFile In Folder.Files
        
        If InStr(oFile.Name, ".xls") > 0 Then
           ActiveCell.Value = oFile
           i = i + 1
           
           AddData i, oFile
           
           wbStart.Activate
           ActiveCell.Offset(1, 0).Select  'next row of file trace
        End If
        
    skip1:
    Next
    wbTarg.Activate
    wbTarg.Save
    MsgBox "Done"
    
    Set oFile = Nothing
    Set Folder = Nothing
    Set FileSystem = Nothing
    Set wbSrc = Nothing
    Set wbTarg = Nothing
    Set wbStart = Nothing
    End Sub
    
    
    Private Sub AddData(ByVal pvFileNum, ByVal pvFile)
    Dim ws As Worksheet
    
    Workbooks.Open pvFile
    Set wbSrc = ActiveWorkbook
    
    For Each ws In Sheets
        ws.Activate
        
        If pvFileNum = 1 Then    'use the 1st file as the base sheet
           pvFileNum = 2
           Sheets(1).Select
           Sheets(1).Copy
           ActiveWorkbook.SaveAs "c:\temp\CombinedData.xls"
           Set wbTarg = ActiveWorkbook
           ActiveSheet.Name = "combined"
           Rows("1:5").Delete
           GoSub Move2Btm
        Else
           Rows("1:6").Delete
           Range("A1").Select
           ActiveSheet.UsedRange.Select
           Selection.Copy
           
            wbTarg.Activate
            ActiveSheet.Paste
            Application.CutCopyMode = False
            GoSub Move2Btm
            wbSrc.Activate
        End If
    Next
    wbSrc.Close False
    Set ws = Nothing
    Exit Sub
    
    '----------
    Move2Btm:
    '----------
    Range("A1").Select
    Selection.End(xlDown).Select
    ActiveCell.Offset(1, 0).Select  'next row
    Return
    End Sub

  3. #3
    Registered User
    Join Date
    06-03-2023
    Location
    New Jersey
    MS-Off Ver
    MS 3655
    Posts
    29

    Re: Create a macro to remove previous data from an excel and populate with data in another

    Ok thanks. Also can you help me with creating a macro or VBA script where I can remove the data stored previously and then automatically populate it with data stored in another workbook. I am able to create the macro but on running it, it populates the data only partially and not the complete data from the file it has to copy from

+ 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. Replies: 6
    Last Post: 12-13-2018, 10:31 AM
  2. Create Userform to Edit, View, Remove and Find next/ previous data
    By Faridwahidi in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 05-17-2015, 11:00 PM
  3. Create Calendar and auto-populate with Excel Data
    By mabesser in forum Excel Charting & Pivots
    Replies: 2
    Last Post: 05-10-2015, 05:06 PM
  4. Need help to create a MACRO to compare two excel sheet and populate the data
    By prashantrcz in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 12-04-2014, 07:24 AM
  5. How to create a macro to populate selected required data on to a table ?
    By chon in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-11-2013, 03:59 AM
  6. Replies: 11
    Last Post: 07-05-2013, 03:19 AM
  7. Macro - how to auto populate the data, based on the previous cell values
    By Avinash Kumar in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 09-21-2012, 02:03 PM

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