RuinAerlin
New Member
- Joined
- Apr 8, 2016
- Messages
- 10
Hi all,
New to the forum and really quite new to macros in general - so apologies if you look at what I've written and go "WTF was she thinking?"
I have a few macros I need to run to get a particular output into something my database can import - and then a manual change files into .csv I'm sure there is a macro or a change to one of the macros I run to get this to work - but I'm struggling a little with how to combine them all.
1st macro:
2nd macro:
3rd macro:
I'd appreciate if someone can have a look at the ones I'm running to see if they can help me streamline, and if possible create a single macro that can "do it all" and save me spending hours a week changing to csv files. If you can explain, I can learn and perhaps do it myself next time.
New to the forum and really quite new to macros in general - so apologies if you look at what I've written and go "WTF was she thinking?"
I have a few macros I need to run to get a particular output into something my database can import - and then a manual change files into .csv I'm sure there is a macro or a change to one of the macros I run to get this to work - but I'm struggling a little with how to combine them all.
1st macro:
Code:
Sub SSEstep1()'
' SSEstep1 Macro
'
' All lines that begin with an apostrophe (') are remarks and are not
' required for the macro to run.
' Dimension Variables.
Dim ResultStr As String
Dim FileName As Variant
Dim FileNum As Integer
Dim Counter As Double
' Ask User for file's name.
FileName = Application _
.GetOpenFilename("Text Files (*.txt), *.txt")
' Check for no entry.
If FileName = False Then End
' Get next available file handle number.
FileNum = FreeFile()
' Open text file for input.
Open FileName For Input As #FileNum
' Turn screen updating off.
Application.ScreenUpdating = False
' Create a new workbook with one worksheet in it.
Workbooks.Add template:=xlWorksheet
Counter = 1
' Loop until the end of file is reached.
Do While Seek(FileNum) <= LOF(FileNum)
' Display importing row number on status bar.
Application.StatusBar = "Importing Row " & _
Counter & " of text file " & FileName
' Store one line of text from file to variable.
Line Input #FileNum, ResultStr
' Store variable data into active cell.
If Left(ResultStr, 1) = "=" Then
ActiveCell.Value = "'" & ResultStr
Else
ActiveCell.Value = ResultStr
End If
If ActiveCell.Row = 65536 Then
' If on the last row then add a new sheet.
ActiveWorkbook.Sheets.Add
Else
' If not the last row then go one cell down.
ActiveCell.Offset(1, 0).Select
End If
' Increment the counter by 1.
Counter = Counter + 1
' Start again at top of 'do while' statement.
Loop
' Close the open text file.
Close
' Remove message from status bar.
Application.StatusBar = False
End Sub
Code:
Sub cycle()
Dim wk As Worksheet
For Each wk In ActiveWorkbook.Worksheets
wk.Activate
Columns("A:A").Select
Selection.TextToColumns Destination:=Range("A1"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=True, Tab:=True, _
Semicolon:=False, Comma:=True, Space:=False, Other:=False, FieldInfo _
:=Array(Array(1, 9), Array(2, 1), Array(3, 9), Array(4, 9), Array(5, 4), Array(6, 1), _
Array(7, 1)), TrailingMinusNumbers:=True
Columns("A:A").Select
Selection.NumberFormat = "0"
Columns("B:B").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("B1").Select
ActiveCell.FormulaR1C1 = _
"=TEXT(RC[1],""DD/MM/YYYY"")&"" ""&TEXT(RC[2],""hh:mm"")"
Range("B1").Select
Selection.AutoFill Destination:=Range("B1:B65536")
Range("B1:B65536").Select
Columns("B:B").Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Columns("C:D").Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlToLeft
Next wk
End Sub
Code:
Sub CreateNewWBS()Dim wbThis As Workbook
Dim wbNew As Workbook
Dim ws As Worksheet
Dim strFilename As String
Set wbThis = ThisWorkbook
For Each ws In wbThis.Worksheets
strFilename = wbThis.Path & "/" & ws.Name
ws.Copy
Set wbNew = ActiveWorkbook
wbNew.SaveAs strFilename
wbNew.Close
Next ws
End Sub