+ Reply to Thread
Results 1 to 1 of 1

Saving copy of data and then running macro on all active worksheets in saved copy?

  1. #1
    Registered User
    Join Date
    12-29-2012
    Location
    New Jersey
    MS-Off Ver
    Excel 2007
    Posts
    5

    Saving copy of data and then running macro on all active worksheets in saved copy?

    Hi all,

    First off, thanks in advance for all your help. I currently have about 81,000 asc files (essentially tables) that I would like to compile together in sets of three (I haven't even gotten to that step yet). Each contain latitude, longitude, and a methane column reading that I would like to merge into one excel file. For example (3 was skipped):

    20030108_1CH4.asc
    20030108_1LAT.asc
    20030108_1LONG.asc
    20030108_2CH4.asc
    20030108_2LAT.asc
    20030108_2LONG.asc
    20030108_4CH4.asc
    20030108_4LAT.asc
    20030108_4LONG.asc
    ...

    I wanted to merge all the 1s into 3 columns in one worksheet, the 2s into 3 columns in a second, etc. However, I wanted to have a backup of the original worksheet since you can't undo a macro, but for some reason the For Next loop isn't working; it tries to redo the macro on the same worksheet.

    Sub WorksheetLoop()
    Dim Current As Worksheet

    ActiveSheet.Copy
    With ActiveWorkbook
    .SaveAs "C:\AdvRS" & .Sheets(1).Name
    .Close 0
    End With


    ' Loop through all of the worksheets in the active workbook.

    For Each Current In Worksheets

    If ActiveSheet.Name Like "*LAT" Then
    Rows("1:1").Delete
    Columns("A:A").TextToColumns Destination:=Range("A1"), DataType:=xlDelimited, _
    TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, _
    Semicolon:=False, Comma:=True, Space:=False, Other:=True, OtherChar:= _
    ":", FieldInfo:=Array(Array(1, 9), Array(2, 1), Array(3, 1), Array(4, 1), Array(5, 1), _
    Array(6, 1), Array(7, 1), Array(8, 1), Array(9, 1), Array(10, 1)), TrailingMinusNumbers _
    :=True

    Dim I As Long, k As Long, j As Integer
    Application.ScreenUpdating = False
    Columns(1).Insert
    I = 0
    k = 1
    While Not IsEmpty(Cells(k, 2))
    j = 2
    While Not IsEmpty(Cells(k, j))
    I = I + 1
    Cells(I, 1) = Cells(k, j)
    Cells(k, j).Clear
    j = j + 1
    Wend
    k = k + 1
    Wend
    Application.ScreenUpdating = True
    Rows("1:1").Insert
    Range("A1") = "LAT"
    Else
    If ActiveSheet.Name Like "*CH4" Then
    Rows("1:1").Delete
    Columns("A:A").TextToColumns Destination:=Range("A1"), DataType:=xlDelimited, _
    TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, _
    Semicolon:=False, Comma:=True, Space:=False, Other:=True, OtherChar:= _
    ":", FieldInfo:=Array(Array(1, 9), Array(2, 1), Array(3, 1), Array(4, 1), Array(5, 1), _
    Array(6, 1), Array(7, 1), Array(8, 1), Array(9, 1), Array(10, 1)), TrailingMinusNumbers _
    :=True


    Application.ScreenUpdating = False
    Columns(1).Insert
    I = 0
    k = 1
    While Not IsEmpty(Cells(k, 2))
    j = 2
    While Not IsEmpty(Cells(k, j))
    I = I + 1
    Cells(I, 1) = Cells(k, j)
    Cells(k, j).Clear
    j = j + 1
    Wend
    k = k + 1
    Wend
    Application.ScreenUpdating = True
    Rows("1:1").Insert
    Range("A1") = "CH4"
    Else
    If ActiveSheet.Name Like "*LONG" Then
    Rows("1:1").Delete
    Columns("A:A").TextToColumns Destination:=Range("A1"), DataType:=xlDelimited, _
    TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, _
    Semicolon:=False, Comma:=True, Space:=False, Other:=True, OtherChar:= _
    ":", FieldInfo:=Array(Array(1, 9), Array(2, 1), Array(3, 1), Array(4, 1), Array(5, 1), _
    Array(6, 1), Array(7, 1), Array(8, 1), Array(9, 1), Array(10, 1)), TrailingMinusNumbers _
    :=True


    Application.ScreenUpdating = False
    Columns(1).Insert
    I = 0
    k = 1
    While Not IsEmpty(Cells(k, 2))
    j = 2
    While Not IsEmpty(Cells(k, j))
    I = I + 1
    Cells(I, 1) = Cells(k, j)
    Cells(k, j).Clear
    j = j + 1
    Wend
    k = k + 1
    Wend
    Application.ScreenUpdating = True
    Rows("1:1").Insert
    Range("A1") = "Long"
    Else
    MsgBox "This is not a LAT/LONG/CH4 table!"
    End If
    End If
    End If


    Next


    End Sub


    PS Sorry for the wacky formatting; I was using Notepad and probably forgot to turn off the word wrap option. Thanks again!
    Attached Files Attached Files

+ 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. Copy data between worksheets, data drills down when saved.
    By peperammi69 in forum Excel General
    Replies: 1
    Last Post: 08-09-2013, 06:01 PM
  2. Multi process macro - save open WB, copy data to new WB, del saved...
    By Alexm963 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-31-2012, 01:42 AM
  3. Copy formulas down on all active worksheets to last row of data
    By pacman76 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-12-2012, 10:33 AM
  4. Copy between (2) active Worksheets
    By clemsoncooz in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-17-2012, 06:02 PM
  5. [SOLVED] Copy data from 3rd worksheets and pasting to active worksheet
    By KimberlyC in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-28-2005, 10:06 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