Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
Update:
Enclosed below is a sample VBA code that I found on joinedupdata.com. I need help making two modifications: (1) remove the criteria that repeated header rows are deleted and (2) see if there's a way to separate the concatenated data from each Excel file by a blank row in the combined sheet that has the filename of the following table in the left-most cell.
Dim firstRowHeaders As Boolean
Dim fso As Object
Dim dir As Object
Dim filename As Variant
Dim wb As Workbook
Dim s As Sheet1
Dim thisSheet As Sheet1
Dim lastUsedRow As Range
Dim file As String
On Error GoTo ErrMsg
Application.ScreenUpdating = False
firstRowHeaders = True 'Change from True to False if there are no headers in the first row
Set fso = CreateObject("Scripting.FileSystemObject")
'PLEASE NOTE: Change <<Full path to your Excel files folder>> to the path to the folder containing your Excel files to merge
Set dir = fso.Getfolder("<<Full path to your Excel files folder>>")
Set thisSheet = ThisWorkbook.ActiveSheet
For Each filename In dir.Files
'Open the spreadsheet in ReadOnly mode
Set wb = Application.Workbooks.Open(filename, ReadOnly:=True)
'Copy the used range (i.e. cells with data) from the opened spreadsheet
If firstRowHeaders And i > 0 Then 'Only include headers from the first spreadsheet
Dim mr As Integer
mr = wb.ActiveSheet.UsedRange.Rows.Count
wb.ActiveSheet.UsedRange.Offset(1, 0).Resize(mr - 1).Copy
Else
wb.ActiveSheet.UsedRange.Copy
End If
'Paste after the last used cell in the master spreadsheet
If Application.Version < "12.0" Then 'Excel 2007 introduced more rows
Set lastUsedRow = thisSheet.Range("A65536").End(xlUp)
Else
Set lastUsedRow = thisSheet.Range("A1048576").End(xlUp)
End If
'Only offset by 1 if there are current rows with data in them
If thisSheet.UsedRange.Rows.Count > 1 Or Application.CountA(thisSheet.Rows(1)) Then
Set lastUsedRow = lastUsedRow.Offset(1, 0)
End If
lastUsedRow.PasteSpecial
Application.CutCopyMode = False
Next filename
ThisWorkbook.Save
Set wb = Nothing
#If Mac Then
'Do nothing. Closing workbooks fails on Mac for some reason
#Else
'Close the workbooks except this one
For Each filename In dir.Files
file = Right(filename, Len(filename) - InStrRev(filename, Application.PathSeparator, , 1))
Workbooks(file).Close SaveChanges:=False
Next filename
#End If
Application.ScreenUpdating = True
ErrMsg:
If Err.Number <> 0 Then
MsgBox "There was an error. Please try again. [" & Err.Description & "]"
End If
I've been trying (without much success) to find a way to merge multiple Excel spreadsheets into one. I'm using MATLAB to analyze experimental data. A dozen Excel spreadsheets go in and an equal amount come out.
Spreadsheet Structure:
The data in each Excel file is only on the first sheet (Sheet 1).
Each sheet has four columns of data (with headers) and a variable number of data rows underneath.
Each Excel file has a unique filename.
Example:
Header 1 | Header 2 | Header 3 | Header 4
1111 22222 3333 4444
11122 11223 33344 33444
etc etc etc etc
Preferred Merging Behavior:
1) Multiple Excel files are merged into one sheet on a single new spreadsheet.
2) Column headers are maintained during the merge.
3) Instead of adding each successive data set to the bottom of the previous one ("vertical" addition), it would be great if the columns could be placed side-by-side ("horizontal" addition) with a one-column break in-between.
4) The filename of each original file is placed into a row just above the first column header.
5) Preferably cross-platform (Windows/Mac OS X). However, if VBA with ActiveX is the only way to go, that's also fine.
Sample Output:
Filename1 Filename2
Header 1 | Header 2 | Header 3 | Header 4 Header 1 | Header 2 | Header 3 | ...
111 22222 33333 4444 1111 222222 44444
Data... Data... Data... Data... Data... Data... Data...
A simple loop through the workbooks in the same folder as the master workbook should suffice.
Sub collect_wb_data()
Dim wbm As Workbook, wb As Workbook
Dim fp As String, fn As String, nc As Long
'Application.ScreenUpdating = False
Set wbm = ThisWorkbook
With wbm.Worksheets("sheet1") 'set this properly to the receiving worksheet in the master workbook
fp = wbm.Path
fn = "*.xl*"
fn = Dir(fp & Chr(92) & fn)
Do While CBool(Len(fn))
If Not fn = .Parent.Name Then
Set wb = Workbooks.Open(Filename:=fp & Chr(92) & fn, _
UpdateLinks:=False, _
ReadOnly:=True)
nc = nc + 1
.Cells(1, nc) = Left(fn, InStr(1, fn, Chr(46)) - 1)
wb.Worksheets(1).Cells(1, 1).CurrentRegion.Copy Destination:=.Cells(2, nc)
wb.Close SaveChanges:=False
Set wb = Nothing
nc = .Cells(2, Columns.Count).End(xlToLeft).Offset(0, 1).Column
End If
fn = Dir
Loop
'.parent.save 'Uncomment to save before finishing operation
End With
Set wbm = Nothing
Application.ScreenUpdating = True
End Sub
Oddly, there has been scant mention of just how the list of workbooks to be processed was intended to be derived. I've used a simply file mask on the same folder that the master workbook resides in but I have left it easy to change. If specific files are to be processed, a multiple list can be made from a standard File Open dialog instead. A hard-coded array of workbook names is another option.
I've left a couple of commands (e.g. screen updating disabled, saving before finishing) commented out. You might want to uncomment these once you are satisfied with the method(s).
Related
I have a macro which is designed to autofilter for certain criteria and then hide certain columns and copy what is left to the appropriate file. Sometimes the file filters correctly, but sometimes it stops on the Selection.AutoFilter line with a RE 1004 error, "Method of range class failed". This usually happens if I run the macro immediately after opening the file. If I reset the entire sheet with a macro I have to unhide everything, it filters correctly.
If it does filter correctly, it omits certain columns when pasting to the destination file. Those columns are the first one right after a handful of blank ones. I need it to copy either all visible columns except the header, or can even be changed to columns A - X, as that is the extent of the information required.
Here is the macro
Sub OO_Away_Lay_1()
'
' OO Away Lay v1 Macro
' This macro will filter for 1x2
'
Dim ws As Worksheet, lc As Long, lr As Long
Set ws = ActiveSheet
'range from A1 to last column header and last row
lc = ws.Cells(1, ws.Columns.Count).End(xlToLeft).Column
lr = ws.Cells.Find("*", after:=ws.Range("A1"), LookAt:=xlPart, _
SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
With ws.Range("A1", ws.Cells(lr, lc))
.HorizontalAlignment = xlCenter
Selection.AutoFilter
.AutoFilter Field:=24, Criteria1:="Draw", Operator:=xlFilterValues
If .Rows.Count - 1 > 0 Then
On Error Resume Next
.Columns("L:S").EntireColumn.Hidden = True
.Columns("U:W").EntireColumn.Hidden = True
.Columns("Y:CK").EntireColumn.Hidden = True
.Offset(1, 0).Resize(.Rows.Count - 1).SpecialCells(xlCellTypeVisible).Copy
On Error GoTo 0
Else
Exit Sub
End If
End With
Workbooks("Predictology_Trading Template v3.1.xlsm").Sheets("OO Away Lay v1") _
.Range("A" & Rows.Count).End(xlUp).Offset(1).PasteSpecial xlPasteValues
Application.CutCopyMode = False
End Sub
Any thoughts on fixing it so it autofilters all the time and also copies all of the required data?
cheers
I've been given the scut job of correcting some hundred or so code testing reports that have been filled out incorrectly by a senior coder who has more import work to do.
Unluckily for me all the files are ms-word documents. But luckily for the formatting is all the same and the errors are all made in the same cells in the same table.
In the past I wrote a bash to edit to change single quotes to double quotes on multiple xml files. But that was with a linux machine. This time around I have only a window machine.
Any hints where to begin?
The answer was to use VBA. I built two subroutines.
The first subRoutine loops through the directory and
opens each *.doc file it finds. Then on the open document file it calls
the second subRoutine. After the second subRoutine is finished the document
is saved and then closed.
Sub DoVBRoutineNow()
Dim file
Dim path As String
path = "C:\Documents and Settings\userName\My Documents\myWorkFolder\"
file = Dir(path & "*.doc")
Do While file <> ""
Documents.Open FileName:=path & file
Call editCellsTableRow2
ActiveDocument.Save
ActiveDocument.Close
file = Dir()
Loop
End Sub
~~~~~~
The second subRoutine only works if all documents have the same formating.
For example: The second row of the only table in the document has cells numbered 6, 7, 8. These contain "dd/MM/yyyy" , "Last Name", "First Name"
These cells need to be changed to "yyyy/MM/dd", "Surname", "Given Name"
Sub editCellsTableRow2()
Application.ScreenUpdating = False
Dim Tbl As Table, cel As Cell, i As Long, n As Long
With ActiveDocument
For Each Tbl In .Tables
Tbl.Rows(2).Alignment = xlCenter
For Each cel In Tbl.Rows(2).Cells
If cel.ColumnIndex = 6 Then
cel.Range.Text = vbCrLf + "yyyy/MM/dd"
End If
If cel.ColumnIndex = 7 Then
cel.Range.Text = vbCrLf + "Surname"
End If
If cel.ColumnIndex = 8 Then
cel.Range.Text = vbCrLf + "Given Name"
End If
Next cel
Next Tbl
End With
Set cel = Nothing: Set Tbl = Nothing
Application.ScreenUpdating = True
End Sub
I am using draw to mark up a pdf format index map. So in grid 99, the text hyperlinks to map99.pdf
There are 1000's of grid cells - is there a way for a (macro) to scan for text in a sheet that is like
Text in File | Link to add
99|file:///c:/maps/map99.pdf
100|file:///c:/maps/map100.pdf
and add links to the relevant file whenever the text is found (99,100 etc).
I don't use libre much but happy to implement any programatic solution.
Ok, after using xray to drill through enumerated content, I finally have the answer. The code needs to create a text field using a cursor. Here is a complete working solution:
Sub AddLinks
Dim oDocument As Object
Dim vDescriptor, vFound
Dim numText As String, tryNumText As Integer
Dim oDrawPages, oDrawPage
Dim oField, oCurs
Dim numChanged As Integer
oDocument = ThisComponent
oDrawPages = oDocument.getDrawPages()
oDrawPage = oDrawPages.getByIndex(0)
numChanged = 0
For tryNumText = 1 to 1000
vDescriptor = oDrawPage.createSearchDescriptor
With vDescriptor
'.SearchString = "[:digit:]+" 'Patterns work in search box but not here?
.SearchString = tryNumText
End With
vFound = oDrawPage.findFirst(vDescriptor)
If Not IsNull(vFound) Then
numText = vFound.getString()
oField = ThisComponent.createInstance("com.sun.star.text.TextField.URL")
oField.Representation = numText
oField.URL = numText & ".pdf"
vFound.setString("")
oCurs = vFound.getText().createTextCursorByRange(vFound)
oCurs.getText().insertTextContent(oCurs, oField, False)
numChanged = numChanged + 1
End If
Next tryNumText
MsgBox("Added " & numChanged & " links.")
End Sub
To save relative links, go to File -> Export as PDF -> Links and check Export URLs relative to file system.
I uploaded an example file here that works. For some reason your example file is hanging on my system -- maybe it's too large.
Replacing text with links is much easier in Writer than in Draw. However Writer does not open PDF files.
There is some related code at https://forum.openoffice.org/en/forum/viewtopic.php?f=20&t=1401.
I've created a form within Access which uses a cross-tab query as its data source.
The column headings for the query are 1, 2, 3, 4 and 5 representing week numbers.
The values display items such as 3/3 = 100.00% or 0/13 = 0.00% or 3/14 = 21.00%.
I've added conditional formatting to the text boxes on the form.
Expression Is Right([2],7)="100.00%" works and displays the figure in bold red when the percentage is 100.
Expression is Val(Right([2],7))=100 also works - converting the text value to a numeric value.
The problem I'm having is that I'm not always looking for 100% - it depends on the value within a table. What I'm trying to do is
Val(Right([2],7))=(SELECT ParamValue*100 FROM tbl_System WHERE Param='SampleSize') - this doesn't work.
Neither does:
Eval(Val(Right([2],7))=(SELECT ParamValue*100 FROM tbl_System WHERE Param='SampleSize'))
or
Val(Right([2],7))=EVAL(SELECT ParamValue*100 FROM tbl_System WHERE Param='SampleSize')
or
Val(Right([2],7))=DLookUp("ParamValue","tbl_System","Param= 'SampleSize'")*100
or
Val(Right([2],7))=Eval(DLookUp("ParamValue","tbl_System","Param= 'SampleSize'")*100)
The SQL for the cross-tab query is:
TRANSFORM NZ(Sum(Abs([Include])),0) & "/" & NZ(Count(*),0) & " = " &
FormatPercent(NZ(Round(Sum(Abs(Include))/Count(*),2),0),2)
SELECT tbl_TMP_PrimaryDataSelection.TeamMember
FROM tbl_TMP_PrimaryDataSelection
GROUP BY tbl_TMP_PrimaryDataSelection.TeamMember
PIVOT tbl_TMP_PrimaryDataSelection.WeekNum In (1,2,3,4,5)
I don't think you can use a function in there, be it system or user-defined.
But you can define the FormatCondition dynamically at runtime, like this:
Dim txtFld As TextBox
Dim objFrc As FormatCondition
Dim strExpr As String
Set txtFld = Me!myTextBox
' Remove existing FormatConditions
txtFld.FormatConditions.Delete
' The dynamic expression
strExpr = "Val(Right([2],7))=" & DLookUp("ParamValue","tbl_System","Param='SampleSize'")*100
' Assign a new FormatCondition to text box
Set objFrc = txtFld.FormatConditions.Add(acExpression, , strExpr)
' Set the format
objFrc.ForeColor = &HFF0000
This example simply removes and recreates all FormatConditions. If you have a fixed number of conditions, you can also use the FormatCondition.Modify method (see online help).
Edit:
The final code I have used executes on the Form_Load event and adds a format to each of the five weekly text boxes:
Private Sub Form_Load()
Dim aTxtBox(1 To 5) As TextBox
Dim x As Long
Dim oFrc As FormatCondition
Dim sExpr As String
With Me
Set aTxtBox(1) = .Wk1
Set aTxtBox(2) = .Wk2
Set aTxtBox(3) = .Wk3
Set aTxtBox(4) = .Wk4
Set aTxtBox(5) = .Wk5
For x = 1 To 5
aTxtBox(x).FormatConditions.Delete
sExpr = "Val(Right([" & x & "],7))>=" & DLookup("ParamValue", "tbl_System", "Param='SampleSize'") * 100
Set oFrc = aTxtBox(x).FormatConditions.Add(acExpression, , sExpr)
oFrc.ForeColor = RGB(255, 0, 0)
Next x
End With
End Sub
Edit 2
Yes, defining FormatConditions via VBA is especially useful when dealing with multiple controls in a loop. You can do this in Design View too and save the FormatConditions permanently, simply to avoid going through the FormatConditions dialogs one by one. Or if the customer later decides that he'd rather have a different color. :)
Note: You could use Set aTxtBox(x) = Me("Wk" & x) in the loop. But actually you don't need multiple TextBox variables, you can simply re-use it.
I have created a User Form in VBA so that call centre staff can submit their numbers to our tracking spreadsheet at the end of each day. In its current design, the form successfully submits data to another sheet in the workbook. As I discovered, macro-enabled spreadsheets can't be shared (each staff member will submit at roughly 4pm), so I am looking at making a copy of the user form spreadsheet for each staff member (around 15) and directing it to submit to a shared spreadsheet every day.
i.e. 15 or so staff members use "User Form.xlsm" to submit to "Tracking Spreadsheet.xlsx" all around 4pm each day.
Q1: Do I need to make the "Tracking Spreadsheet.xlsx" a Shared workbook in case more than one staff member submits their end of day form at once?
Q2: Do I need to insert VBA code in "User Form.xlsm" that actively opens "Tracking Spreadhseet.xlsx" or can I just reference "Tracking Spreadhseet.xlsx"?
Q3: Where have I gone wrong in the code below? I'm new to VBA. I have structured my code for the submission button as follows, but it just adds data to the Daily_Tracking_Dataset sheet in the current workbook, rather than the new one:
First, I tried to change the workbook,
then i make the relevant sheet in the workbook active,
then I determine the first empty row,
then I transfer the information from the form's textboxes to the new workbook.
Private Sub Button_Submit_Click()
'Change Workbook
Dim nwb As Workbook
Set nwb = Workbooks.Open("G:\Tracking Spreadsheet.xlsx")
Dim emptyRow As Long
'Make Daily_Tracking_Dataset active
Daily_Tracking_Dataset.Activate
'Determine emptyRow
emptyRow = WorksheetFunction.CountA(Range("A:A")) + 1
'Transfer Information
Cells(emptyRow, 1).Value = TextBox1.Value
Cells(emptyRow, 2).Value = lstName.Value
Cells(emptyRow, 3).Value = txtROIT.Value
Cells(emptyRow, 4).Value = txtROISub.Value
Cells(emptyRow, 5).Value = txtRefsT.Value
Cells(emptyRow, 6).Value = txtRefsC.Value
Cells(emptyRow, 7).Value = txtRefsSub.Value
Cells(emptyRow, 8).Value = txtReSubT.Value
Cells(emptyRow, 9).Value = txtReSubSub.Value
End Sub
Try this sample below:
Private Sub TextBox1_afterupdate()
Dim pro As Workbook
Set pro = Workbooks.Open("F:\DOCUMENTS\Proration.xlsm")
Workbooks("proration").Sheets("sheet1").Range("i20").End(xlUp).Offset(1, 0).Value = UserForm1.TextBox1.Value
pro.Save
pro.Close True
End Sub
Regarding Q1/Q2: yes, you will need to add code to open the worksheet, and it may be better to open it Shared, at least if you do not save and close the tracking spreadsheet file immediately after inserting the data.
Did you try an Access database or something similar, where you can more easily add the required information and do not need to worry about concurrent accesses to the data "sheet"?
Regarding Q3: you did not state what is going wrong with your code at the moment.
Edit:
Regarding Q3: Try using something like nwb.Sheets( "daily_tracking_dataset" ).Cells(emptyRow, 1).Value = TextBox1.Value and be aware that emptyRow also needs to be determined using nwb, e.g. using a combination of Offset and Move(xlDown) (see Excel: Move selection to next blank row in specific column, and enumerate selection by date and type).
Thanks for the help. I ended up using the emptyrow method below:
'Begin Transfer Information and Change Workbook
Dim nwb As Workbook
Set nwb = Workbooks.Open("G:\Time To Complete Dataset.xlsx")
'Determine emptyRow
Dim emptyRow As Long
emptyRow = WorksheetFunction.CountA(nwb.Sheets("daily_tracking_dataset").Range("A:A")) + 1
'Transfer Information
With nwb.Sheets("daily_tracking_dataset")
'Datebox
.Cells(emptyRow, 1).Value = CDate(txtDate.Text)
'Listbox
.Cells(emptyRow, 2).Value = lbName.List(lbName.ListIndex)
'Textbox
.Cells(emptyRow, 3).Value = txtROT.Value
End With
ActiveWorkbook.Save
ActiveWindow.Close
End Sub