I have been trying all day to do something pretty simple. I am an absolute newbie with VB so I am sure I've missed something.
I have been trying to use MID to split up the numbers in a column on a spreadsheet.
Here is what I have so far (I have been trying to do only one to make sure it works):
Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = True
Set objWorkbook = objExcel.Workbooks.Open("C:\Documents and Settings\<username>\Desktop\New.csv")
Set objWorksheet = objWorkbook.Worksheets(1)
objWorksheet.Activate
sub_str = Mid(A1, 1, 4)
So the application opens, the worksheet is active, then... nothing. No error or anything. It's like it literally stops there and ignores the last line altogether. The numbers that I want to split look like the below in Excel. They are just dates that are backwards, hence the wanting to split, so I can separate and put it the right way round.
20140101
20140127
20140303
20140310
20140310
20140310
20140310
20140418
20140419
Any help is very appreciated!
Try this:
Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = True
Set objWorkbook = objExcel.Workbooks.Open("C:\Users\USER\Desktop\new1.csv")
Set objWorksheet = objWorkbook.Worksheets(1)
objWorksheet.Activate
rowCount=objWorksheet.usedrange.rows.count
Set rngA=objWorksheet.Range("A1:A" & rowCount)
'Set rngB=rngA.offset(,1) 'objWorksheet.Range("B1")
with objWorksheet
for each cell in rngA
sub_strY = Mid(cell.value, 1, 4)
sub_strM=Mid(cell.value, 5, 2)
sub_strD=Mid(cell.value, 7, 2)
'msgbox sub_strY
'msgbox sub_strM
'msgbox sub_strD
strDate=sub_strD & "/" & sub_strM & "/" & sub_strY
msgbox strDate
'cell.offset(,1).value=strDate ''to another column
cell.value=strDate ''to overwrite
next
end with
You can use Mid in vbscript. The problem is not there. The problem is in A1 in the line sub_str = Mid(A1, 1, 4).
A1 has been treated like a variable. It's always best to work with objects. Also if you want 20140419 to be changed to 04192014 then you actually do not need Mid. You can use Left and Right for this.
I am assuming that the data is in the format yyyymmdd and you want output as mmddyyyy. If you want the output as ddmmyyyy, then you will have to use Mid. like this
sDate = Right(.Range("A" & i).Value, 2) & _
Mid(.Range("A" & i).Value, 5, 2) & _
Left(.Range("A" & i).Value, 4)
Is this what you are trying?
Const xlUp = -4162
Dim oXLApp, oXLwb, oXLws
Dim lRow, i
Dim sFile, sDate
'~~> Change this to the relevant file
sFile = "C:\Users\Siddharth Rout\Desktop\book1.xlsx"
'~~> Establish an EXCEL application object
On Error Resume Next
Set oXLApp = GetObject(, "Excel.Application")
'~~> If not found then create new instance
If Err.Number <> 0 Then
Set oXLApp = CreateObject("Excel.Application")
End If
Err.Clear
On Error GoTo 0
'~~> Hide Excel
oXLApp.Visible = False
'~~> Open files
Set oXLwb = oXLApp.Workbooks.Open(sFile)
'~~> Set the worksheet you want to work with
Set oXLws = oXLwb.Sheets(1)
'~~> work with the worksheet
With oXLws
lRow = .Range("A" & .Rows.Count).End(xlUp).Row
For i = 1 To lRow
'~~> 20140101 becomes 01012014. If the Col A is formatted
'~~> as number then the leading `0` will disappear as
'~~> shown in the screenshot below
sDate = Right(.Range("A" & i).Value, 4) & Left(.Range("A" & i).Value, 4)
.Range("A" & i).Value = sDate
Next
End With
'~~> Close and save
oXLwb.Close (True)
'~~> CLEANUP (VERY IMPROTANT)
Set oXLws = Nothing
Set oXLwb = Nothing
oXLApp.Quit
Set oXLApp = Nothing
MsgBox "DONE" 'OR wscript.echo "Done"
Screenshots:
Before
After
The Mid function is not a VBscript function but a VBA function therefore if anyway this might work:
objExcel.Mid([A1], 1, 4)
instead of this:
Mid(A1, 1, 4)
If this does not work a you need to run Excel then try putting all the logic in the Excel function and executing it from VBscript if needed (example below):
RunMacro
Sub RunMacro()
dim xl
Set xl = CreateObject("Excel.application")
Dim xlBook
Dim sCurPath
path = CreateObject("Scripting.FileSystemObject").GetAbsolutePathName(".")
Set xl = CreateObject("Excel.application")
Set xlBook = xl.Workbooks.Open(path & "\Book1.xlsm", 0, True)
xl.Application.Visible = False
xl.DisplayAlerts = False
xl.Application.run "Book1.xlsm!Module.MyMacro"
xl.ActiveWindow.close
Set xlBook = Nothing
xl.Quit
Set xl = Nothing
End Sub
Related
I used this macro a few days ago and all worked well but now it does not work properly. I get an execution error at the very beginning of the with statement, or i get another automation error.
I checked if my file exists and it exists, checked if it's found or not: all ok but when i create the excel object and begin with statement i get an error
ActiveDocument.Application.ScreenUpdating = False
Dim strSite As Site, intRow As Long, rg As Object, tmp As String, lastCol As Long, i As Long 'varibles pour derniere colonne du fichier excel et la ligne de la trigramme recherche
Dim xlapp As Object, xlbook As Object, currentcell As Object, nextcell As Object, src As Object
Dim found As String, filename
'creation du objet Excel
On Error Resume Next
Set xlapp = GetObject(, "Excel.Application")
If err Then
Set xlapp = CreateObject("Excel.Application")
End If
On Error GoTo 0
filename = "FichierTrigrammes.xlsx"
found = Dir(folderPath & "\" & "FichierTrigrammes.xlsx")
MsgBox found
If found <> vbNullString Then
' to be changed to the real File Name, if not it will not work
Set xlbook = xlapp.workbooks.Open(filename:=folderPath & filename): xlapp.Visible = False 'does not open the file, read only => faster to get the info
' searching for the Trigramm
With xlbook.sheets(1)
intRow = xlbook.sheets(1).Cells.Find(what:=strTrigram).Row
'getting the address -> to get the row therafter
'find the last non blank cell in specific row
Set currentcell = xlbook.sheets(1).Range("a" & intRow)
Do While Not IsEmpty(currentcell)
Set nextcell = currentcell.Offset(0, 1)
If nextcell.Value = currentcell.Value Then
currentcell.EntireRow.Delete
End If
Set currentcell = nextcell
Loop
lastCol = .Range(currentcell.Address).Column
For i = 1 To lastCol
Select Case .Cells(1, i).Value
Case "Type Site"
strSite.type = .Cells(intRow, i).Value
Case "Nom Site"
strSite.nomSite = .Cells(intRow, i).Value
End Select
Next i
End With
'Set xlapp = Nothing: Set xlbook = Nothing ' pour ne pas sauvegarder le document
End If
ActiveDocument.Application.ScreenUpdating = True
getSiteInfo = strSite
End Function
First issue
If you use the Range.Find method it might be that nothing is found so you will always need to test for that case.
You need always to specify the LookAt parameter for Find as xlWhole or xlPart otherwise VBA will use whatever the user or VBA used before (there is no default). If you don't specify it you never know what you get.
So something like this:
Dim FoundAt As Range
'…
FoundAt = xlbook.sheets(1).Cells.Find(What:=strTrigram, LookAt:=xlWhole)
If Not FoundAt Is Nothing Then '
intRow = FoundAt.Row
'All your other code
Else
MsgBox "'" & strTrigram & "' was not found."
End If
If you use Late Binding in Word then define the following constants:
Const xlWhole As Long = 1
Const xlPart As Long = 2
to make them available in Word.
Second issue
Note that with the following code both Set xlapp might fail and both errors will be hidden because of On Error Resume Next.
On Error Resume Next
Set xlapp = GetObject(, "Excel.Application")
If err Then
Set xlapp = CreateObject("Excel.Application")
End If
On Error GoTo 0
Change it to
On Error Resume Next
Set xlapp = GetObject(, "Excel.Application")
On Error GoTo 0
If xlapp Is Nothing Then
Set xlapp = CreateObject("Excel.Application")
End If
Third issue
You test if folderPath & "\" & "FichierTrigrammes.xlsx" exists but you open something different folderPath & filename.
Change it to
filename = "FichierTrigrammes.xlsx"
found = Dir(folderPath & Application.PathSeparator & filename)
and use that to open the file
Set xlbook = xlapp.workbooks.Open(filename:=folderPath & Application.PathSeparator & filename)
Private Sub Submit_Click()
'----------The Script below writes values to Word Doc ----------------------------------------
Dim wApp As Object
Dim wDoc As Object
'We need to continue through errors since if Word isn't
'open the GetObject line will give an error
'On Error Resume Next
Set wApp = GetObject(, "Word.Application")
'We've tried to get Word but if it's nothing then it isn't open
If wApp Is Nothing Then
Set wApp = CreateObject("Word.Application")
End If
'It's good practice to reset error warnings
On Error GoTo 0
'Open your document and ensure its visible and activate after opening
Set wDoc = wApp.Documents.Open(Filename:="C:\Documents\example.docx ", ReadOnly:=False)
With wDoc
.Bookmarks("bookmark1").Range.Text = Me.TextBox1.Value 'how do I also insert the TextBox1.Value to the next empty row in worksheet?
'so far I got this to do it but everytime i click submit it puts it in the same cell instead of the next row
Sheet6.Range("H2").Value = Me.TextBox6.Value
End With
wApp.Visible = True
'set default file name and file path
ProposedFileName = Format(Now(), "DDMMMYYYY") & TextBox1.Value & "-" & ".doc"
ProposedFilePath = "C:\Documents\"
With wApp.FileDialog(msoFileDialogSaveAs)
wDoc.SaveAs2 ProposedFilePath & ProposedFileName, _
FilterIndex = 1, _
FileFormat:=wdFormatDocument
End With
End Sub
Hi all,
The code above is just a part of my script which works fine when the userform textbox value gets inserted to bookmark1 in word doc, but how do I also insert this textbox value to worksheet row for example goes under column header "name"?
Thank you.
I have finally managed to solve it by adding the code
Dim LastRow As Long, ws As Worksheet
Set ws = Sheets(2)
LastRow = ws.Range("A" & Rows.Count).End(xlUp).Row + 1 'Finds the last blank row
ws.Range("A" & LastRow).Value = TextBox1.Value 'Adds the TextBox1 into Col A & Last Blank Row
I'm exporting to Excel and I need to insert numbers in front of cells that allready have numbers in It. Here is my full code:
Private Sub cmdExport_Click()
Dim Results As Recordset
Dim Numbering As Integer
Dim FileName As String
Dim FilePath As String
Dim wb As Excel.Workbook
Dim XcelFile As Excel.Application
FileName = "TEST" & Format(Date, "dd/mm/yyyy") & ".xlsx"
FilePath = CurrentProject.Path & "\" & FileName
Set XcelFile = New Excel.Application
Set wb = XcelFile.Workbooks.Add
Set Results = Forms![MyForm].Form.RecordsetClone
With wb
XcelFile.ScreenUpdating = False
For Numbering = 0 To Results.Fields.Count - 1
XcelFile.Cells(1, Numbering + 1).Value = Results.Fields(Numbering).Name
Next Numbering
Results.MoveFirst
XcelFile.Range("A2").CopyFromRecordset Results
For Each cell In XcelFile.Range("A:A")
cell.Value = "100" & cell.Value
Next cell
.SaveAs FileName:=FilePath, FileFormat:=51
XcelFile.ScreenUpdating = True
End With
wb.Close
Set XcelFile = Nothing
End Sub
As you see, I have tried with this:
For Each cell In XcelFile.Range("A:A")
cell.Value = "100" & cell.Value
Next cell
But, unfortunally, nothing happens. How could I solve this?
For Each cell In XcelFile.Range("A:A")
should be
Dim sh As Excel.Worksheet
Set sh = wb.ActiveSheet
For Each cell In sh.Range("A:A")
me thinks.
This applies for all other places where you have XcelFile.Cells now, change them to sh.Cells.
Note:
Dim XcelFile As Excel.Application
is a really really confusing name, IMHO.
I have a some command buttons that send access tables through to an excel spreadsheet and undergo some formatting and entering some formulas in them. The other command buttons work, but this one falls over at the LastRowInventory line.
I am sure its something to do with oBook but I can't quite figure out how to fix it. I think its because it is attempting to get an object it has already got. It runs smoothly every second time, but does not close the excel process. My attempts at resolving this over the last couple hours have not worked.
The error I have been getting is as follows:
Run-time error '462': The remote server machine does not exist or is unavailable
Any help is appreciated. I believe it is a simple fix but just can't quite get it, I'm pretty new to programming. The code is below.
Private Sub INVENTORYLIST_Click()
DTable = InputBox("Input Table Name")
'****************************TRANSFER TO EXCEL********************************
Dim strWorksheetPathTable As String
strWorksheetPathTable = "O:\GData\Downstream_LNG\Data Mgmt\CEDA\Reports\" & DTable & "\" & DTable & ".xls"
DoCmd.TransferSpreadsheet transfertype:=acExport, _
spreadsheettype:=acSpreadsheetTypeExcel12, _
TableName:=("" & DTable & "_INVENTORY LIST"), FileName:=strWorksheetPathTable, _
hasfieldnames:=True, _
Range:="InventoryList"
'****************************FORMAT INVENTORY SHEET***********************************
Dim xlApp As Object
Dim xlWB As Object
Set xlApp = CreateObject("Excel.Application")
Dim oBook As Excel.Workbook
Dim InventoryListSheet As Excel.Worksheet
Dim SummarySheet As Excel.Worksheet
Set xlWB = xlApp.Workbooks.Open("" & strWorksheetPathTable & "")
Set oBook = GetObject("" & strWorksheetPathTable & "")
Set InventoryListSheet = oBook.Sheets("InventoryList")
Set SummarySheet = oBook.Sheets("Summary")
With xlWB
With InventoryListSheet
'Some Spreadesheet Formatting in here
End With
End With
'****************************CREATE OE STATUS BREAKDOWN CALCULATIONS ON SUMMARY SHEET**********************
Dim LastRowInventory As Long
LastRowInventory = oBook.Sheets("InventoryList").Range("A" & Rows.Count & "").End(xlUp).Row
With xlWB
With SummarySheet
'Some Spreadsheet Formulas here
End With
End With
'*********************************ORDER WORKSHEETS*************************************
With xlWB
.Sheets("InventoryList").Select
.Sheets("InventoryList").Move Before:=oBook.Sheets(1)
.Sheets("Summary").Select
.Sheets("Summary").Move Before:=oBook.Sheets(1)
End With
If Not SummarySheet Is Nothing Then
Set SummarySheet = Nothing
End If
If Not InventoryListSheet Is Nothing Then
Set InventoryListSheet = Nothing
End If
If Not oBook Is Nothing Then
Set oBook = Nothing
End If
If Not xlWB Is Nothing Then
xlWB.Save
xlWB.Close
Set xlWB = Nothing
End If
If Not xlApp Is Nothing Then
xlApp.Quit
Set xlApp = Nothing
End If
DoCmd.SetWarnings True
MsgBox ("INVENTORY SHEET HAS BEEN CREATED.")
End Sub
Try this:
LastRowInventory = ActiveSheet.Range("B" & Rows.Count).End(xlUp).Row
Or if that doesn't work try:
LastRowInventory = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row
Does this help you?
EDIT:
LastRowInventory = InventoryListSheet.Range("A" & InventoryListSheet.Rows.Count & "").End(xlUp).Row
By specifying the sheet that the rows are to be counted on the issue is fixed.
Here I have some VBA code that outputs a ton of files into Excel files. My question is, from this, is there anyway for it to Format the excel file a bit? What I would like to do is make the Columns bold and make the columns fit the size of the header as well.
Sub OutPutXL()
Dim qdf As QueryDef
Dim rs As DAO.Recordset
Set qdf = CurrentDb.QueryDefs("OutputStudents")
Set rs = CurrentDb.OpenRecordset("Teachers")
Do While Not rs.EOF
qdf.SQL = "SELECT * FROM Students WHERE contact='" & rs!contact & "'"
''Output to Excel
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel8, _
qdf.Name, "C:\Users\chrisjones\Documents\ProjectionsFY14\Teachers\" _
& rs!contact & ".xls", True
rs.MoveNext
Loop
End Sub
this is a quick and dirty combination of Phil.Wheeler's Code and my previous input, for me this is working. Don't forget to add Excel's Object Library in your Access-Macro.
Sub doWhatIWantTheDirtyWay()
pathToFolder = "C:\Users\Dirk\Desktop\myOutputFolder\"
scaleFactor = 0.9
Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = False
objExcel.DisplayAlerts = False
Set objFso = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFso.GetFolder(pathToFolder)
For Each objFile In objFolder.Files
If objFso.GetExtensionName(objFile.path) = "xls" Then
Set objWorkbook = objExcel.Workbooks.Open(objFile.path)
For Each sh In objWorkbook.Worksheets
If sh.UsedRange.Address <> "$A$1" Or sh.Range("A1") <> "" Then
With sh
columncount = .Cells(1, 256).End(xlToLeft).Column
For j = 1 To columncount
With .Cells(1, j)
i = Len(.Value)
.ColumnWidth = i * scaleFactor
.Font.Bold = True
End With
Next
End With
End If
Next
objWorkbook.Close True
End If
Next
objExcel.Quit
End Sub
Yes it is possible! This is hacked together from one of my codes, might need a bit of editing before it works...
'This deals with Excel already being open or not
On Error Resume Next
Set xl = GetObject(, "Excel.Application")
On Error GoTo 0
If xl Is Nothing Then
Set xl = CreateObject("Excel.Application")
End If
Set XlBook = GetObject(filename)
'filename is the string with the link to the file ("C:/....blahblah.xls")
'Make sure excel is visible on the screen
xl.Visible = True
XlBook.Windows(1).Visible = True
'xl.ActiveWindow.Zoom = 75
'Define the sheet in the Workbook as XlSheet
Set xlsheet1 = XlBook.Worksheets(1)
'Then have some fun!
with xlsheet1
.range("A1") = "some data here"
.columns("A:A").HorizontalAlignment = xlRight
.rows("1:1").font.bold = True
end with
'And so on...
I have come across this problem a couple of times as well. As #Remou said, you will need to open excel to format xls files, this modification of your code silently opens Excel and that should get you in the right direction. Remember to add a reference to the Microsoft Excel Object Library in your VBA project.
Sub OutPutXL()
Dim qdf As QueryDef
Dim rs As DAO.Recordset
Dim xl as Excel.Application
Dim wb as Object
Dim strFile as string
Set qdf = CurrentDb.QueryDefs("OutputStudents")
Set rs = CurrentDb.OpenRecordset("Teachers")
Set xl = New Excel.Application
xl.DisplayAlerts = False
Do While Not rs.EOF
qdf.SQL = "SELECT * FROM Students WHERE contact='" & rs!contact & "'"
'Output to Excel
strFile = "C:\Users\chrisjones\Documents\ProjectionsFY14\Teachers\" & rs!contact & ".xls"
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel8, qdf.Name, strFile, True
'Start formatting'
Set wb = xl.Workbooks.Open(strFile)
With wb.Sheets(qdf.name)
'Starting with a blank excel file, turn on the record macro function'
'Format away to hearts delight and save macro'
'Past code here and resolve references'
End With
wb.save
wb.close
set wb = Nothing
rs.MoveNext
Loop
xl.quit
set xl = Nothing
End Sub
You could (depending on the number of files) make a template for each file you are outputting. In the long run if someone needs to change the formatting they can change the template which is going to be easier on you now that you don't have to sift through a bunch of excel formatting garbage. You could even let a qualified end user do it.
It's one of the biggest problems I have with excel sheets if I wrote the VBA I am responsible until I die for it. This way (in theory) they should be able to change a column, without changing how the data is outputted, just presented without you.
+1 To open the excel file itself and format it using that automation though.