Disclaimer: This is not my code. I took a macro from a regular xlsm file and placed it on a xslb file to have it handy for analysis. The macro works correctly in the xlsm, but encounters a variable issue on the xlsb file.
ColumnToFormat identifies a specific column. I verified that the column is present within the worksheet on my Locals window.
Main Sub
ColumnToFormat = Application.WorksheetFunction.Match("OR_TR_OLD_BAL", ActiveSheet.Rows(1), False)
Call FormatAmounts
When I start running FormatAmounts, however, the variable disappears and Excel throws a RunTime Error 1004; Application Defined or Object Defined Error. When testing the xlsm file, this does not happen. The variable gets passed from the main sub to the helper sub. See FormatAmounts sub below.
Sub FormatAmounts()
ActiveSheet.Columns(ColumnToFormat).Select
Selection.TextToColumns Destination:=Range(ActiveSheet.Columns(ColumnToFormat).Address), DataType:= _
xlDelimited, TextQualifier:=xlNone, ConsecutiveDelimiter:=False, _
Tab:=False, Semicolon:=False, Comma:=False, Space:=False, Other:= _
False, FieldInfo:=Array(1, 1), TrailingMinusNumbers:=True
Selection.NumberFormat = "_(* #,##0.00_);_(* (#,##0.00);_(* ""-""??_);_(#_)"
End Sub
Pass the ColumnToFormat value over to the sub procedure and add error control in the main sub by using Application.Match to a variant and testing the variant for error.
sub main()
dim ColumnToFormat as variant
ColumnToFormat = Application.Match("OR_TR_OLD_BAL", ActiveSheet.Rows(1), 0)
if not iserror(ColumnToFormat ) then FormatAmounts clng(ColumnToFormat)
end sub
Sub FormatAmounts(c as long)
with ActiveSheet.Columns(c)
.TextToColumns Destination:=.cells(1), DataType:=xlDelimited, TextQualifier:=xlNone, ConsecutiveDelimiter:=False, _
Tab:=False, Semicolon:=False, Comma:=False, Space:=False, Other:=False, _
FieldInfo:=Array(1, 1), TrailingMinusNumbers:=True
.NumberFormat = "_(* #,##0.00_);_(* (#,##0.00);_(* ""-""??_);_(#_)"
end with
End Sub
Related
I am trying to get my Text To Columns to work in my VBA script, but it's not working for me.
I understand how it works in excel and even used the macro record to get it working, but when adding it to my script nothing happens. I even looked at online forums. What I'm doing is extracting data from one workbook to the other and the data that I am getting needs to be converted to Text To Columns once the data is transferred to the other workbook.
I have two separate Sub functions that I used below: Neither of them worked. One of them is from the macro record and the other I found online.
Code from Macro Record below:
Sub TextToCol()
Selection.TextToColumns Destination:=Range("D3"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=True, Tab:=False, _
Semicolon:=False, Comma:=False, Space:=True, Other:=False, OtherChar _
:="" & Chr(10) & "", FieldInfo:=Array(Array(1, 1), Array(2, 1), Array(3, 1), Array(4, 1)), _
TrailingMinusNumbers:=True
End Sub
Code from online forum below:
Sub TextToCol()
Selection.Range("D3:D8").TextToColumns _
DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, _
ConsecutiveDelimiter:=True, _
Space:=True
End Sub
The code below is what I am using to connect to source workbook:
'Save Above Created New Workbook
sWorkbook.SaveAs Filename:="C:\Users\username\Desktop\fileName\Test.xlsx"
FileToOpen = Application.GetOpenFilename(Title:="Browse for your File & Import Range", FileFilter:="Excel Files (*.xls*),*xls*")
Set OpenBook = Application.Workbooks.Open(FileToOpen)
Below is the code to send data to the destination workbook:
Workbooks("Test.xlsx").Worksheets("Sheet1").Range("D" & counter).value = dataThatWasExtracted
What am I doing wrong?
I would like to format an Excel file to "General", but it's not working for all cells (some are custom, percentage...).
Here is my code:
With ActiveSheet.Range("A1").Resize(LineIndex, 1)
.Value = WorksheetFunction.Transpose(strLine)
.NumberFormat = "General"
'DEFINE THE OPERATION FULLY!!!!
.TextToColumns Destination:=.Cells(1), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, _
Tab:=False, Semicolon:=False, Comma:=False, Space:=False, _
Other:=True, OtherChar:="|"
End With
If you want to format all cells to General, then use something like:
Sub dural()
ActiveSheet.Cells.NumberFormat = "General"
End Sub
I have around 30 files that I want to perform the same analysis on. Ideally, I want VBA to open all files and extract a certain (identical) piece of information. However, I don't know how to write a function for it. I was thinking of saving all the files as CSV and importing to python (a language I'm familiar with) which would make it easier for my data analysis. The reason I'm importing into excel is because python has difficulty reading .TX0 files (parsing line by line would be time consuming).
This is what i Have for the csv conversion. How do i translate this into the extraction of multiple files?
Sub TX0_CSV()
'
' TX0_CSV Macro
'
'
Workbooks.OpenText Filename:="X/.....fid002.TX0", Origin:= _
xlMSDOS, StartRow:=1, DataType:=xlDelimited, TextQualifier:=xlDoubleQuote _
, ConsecutiveDelimiter:=False, Tab:=True, Semicolon:=False, Comma:=True _
, Space:=False, Other:=False, FieldInfo:=Array(Array(1, 1), Array(2, 1), _
Array(3, 1), Array(4, 1)), TrailingMinusNumbers:=True
Range("A1:O13").Select
Range("O13").Activate
Selection.ClearContents
Rows("15:16").Select
Selection.Delete Shift:=xlUp
ActiveWindow.SmallScroll Down:=9
Range("A20:H29").Select
Range("H29").Activate
Selection.ClearContents
Range("J19").Select
ActiveWorkbook.SaveAs Filename:="X:\Joey\FID002.csv", FileFormat:=xlCSV, _
CreateBackup:=False
End Sub
You could convert it to a sub and then repeatedly call it.
Here is an example (Untested) Also note how we got rid of .Activate and .Select. You may want to see This
Sub ConvertFile(flName As String, newFileName As String)
Dim wb As Workbook
Dim ws As Worksheet
Workbooks.OpenText Filename:=flName, Origin:= _
xlMSDOS, StartRow:=1, DataType:=xlDelimited, TextQualifier:=xlDoubleQuote _
, ConsecutiveDelimiter:=False, Tab:=True, Semicolon:=False, Comma:=True _
, Space:=False, Other:=False, FieldInfo:=Array(Array(1, 1), Array(2, 1), _
Array(3, 1), Array(4, 1)), TrailingMinusNumbers:=True
Set wb = ActiveWorkbook
Set ws = wb.Sheets(1)
With ws
.Range("A1:O13").ClearContents
.Rows("15:16").Delete Shift:=xlUp
.Range("A20:H29").ClearContents
End With
wb.SaveAs Filename:=newFileName, FileFormat:=xlCSV
wb.Close (False)
End Sub
Example
Sub Sample()
ConvertFile "C:\Test1.dat", "C:\Test2.dat"
End Sub
I have tried creating a macro that automatically formats a column value to change date and time just to date, however when the macro is run the cell values dont automatically update.
I currently need to make the following function selection happen using VBA for excel 2010.
Data/Text to Columns/Next button (keep default "Delimited")/Next button (keep default "tab")/Next button (keep default "General"/Finish button
I have tried recording functions but the VBA doesn't work. HELPPPPPPPPPPPPPPPPPPPPP!!!!!
Here is my code so far.
' Autochange_Extractdata_Date_Format Macro
'
Sheets("Data Extract").Select
Columns("AK:AK").Select
Selection.NumberFormat = "m/d/yyyy"
Columns("AK:AK").Select
Selection.TextToColumns Destination:=Range("AK1"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
Semicolon:=False, Comma:=False, Space:=False, Other:=False, FieldInfo _
:=Array(1, 1), TrailingMinusNumbers:=True
End Sub
From the symptoms you described it sounds like your field is initially in TEXT format, that's why changing it to number format doesn't do anything. When you click into the cell it automatically converts it to a number.
Try this
Columns("AK:AK").NumberFormat = "m/d/yyyy"
Columns("AK:AK").Value = Columns("AK:AK").Value '//This will convert each properly
Columns("AK:AK").Select
Selection.TextToColumns Destination:=Range("AK1"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
Semicolon:=False, Comma:=False, Space:=False, Other:=False, FieldInfo _
:=Array(1, 1), TrailingMinusNumbers:=True
Because i couldnt work out how to update the cell value i managed to work this code to change the cell value instead from Date time just to Date.
works a treat!!!
' Autochange_Extractdata_Date_Format Macro
Sheets("Data Extract").Select
Columns("AK:AK").Select
Selection.NumberFormat = "m/d/yyyy"
Range("AK4:AK999999").Select
Dim c As Range
For Each c In Selection
Dim v As String
v = c.value
If c.value <> Empty Then
c.value = DateValue(c.value)
Else
Exit For
End If
Next
Range("A1").Select
End Sub
I have code for delimited columns for one sheet. How do I make this code for more than sheets with looping?
Sub Macro1()
Selection.TextToColumns Destination:=ActiveCell, DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, _
Semicolon:=False, Comma:=False, Space:=False, Other:=True, OtherChar _
:="_", FieldInfo:=Array(Array(1, 1), Array(2, 1)), TrailingMinusNumbers:=True
ActiveCell.Offset(0, 1).Columns("A:A").EntireColumn.Select
Selection.NumberFormat = "dd-mm-yyyy;#"
End Sub
Sub t()
Dim sh As Worksheet
For Each sh In ActiveWorkbook.Sheets
'do what you want, for example,
Range("A1:B10").TextToColumns Destination:=ActiveCell, DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, _
Semicolon:=False, Comma:=False, Space:=False, Other:=True, OtherChar _
:="_", FieldInfo:=Array(Array(1, 1), Array(2, 1)), TrailingMinusNumbers:=True
ActiveCell.Offset(0, 1).Columns("A:A").EntireColumn.Select
Selection.NumberFormat = "dd-mm-yyyy;#"
Next
End Sub
However, you should not use Selection. You should use a range instead, for e.g. Range("A1:B10")
Try it like this
Sub forEachWs()
Dim x As Long
Dim ws As Sheets
For x = 1 To 12'<--to as how many is required
ThisWorkbook.Sheets(x).Activate
Call Macro1 '<---the name of your macro
Range("A2").Select
End Sub