Hi everyone! I am relatively new to using macros and have been having some trouble using macros to compile data into a new excel sheet. My goal is to have a sheet that looks as follows:
I have written a macro that takes the Item Name and the Measured Value and posts it into a compileddata sheet. The code I'm using is:
I am looking to add some code which would paste the file name of the sheet from which the Item Name and Measured Value are taken in Column C, ideally filling every row. I hope the table above is helpful to visualize what I mean. Thank you!!
Item Name | Measured Value | Source File Name |
A | X | Patient1.xls |
B | X | Patient1.xls |
C | X | Patient1.xls |
A | X | Patient2.xls |
B | X | Patient2.xls |
C | X | Patient2.xls |
A | X | Patient3.xls |
I have written a macro that takes the Item Name and the Measured Value and posts it into a compileddata sheet. The code I'm using is:
VBA Code:
Sub DataTransposing()
strP = "F:\User\Macros\Datafolder"
strF = Dir(strP & "\*.xls")
Do While strF <> vbNullString
Workbooks.Open Filename:= _
"F:\User\Macros\CompiledData.xlsx"
Workbooks.Open (strP & "\" & strF)
Sheets("Sheet1").Activate
Range("A3:AZ3").Select 'Change as required
Selection.Copy
Windows("CompiledData.xlsx").Activate
'Change as required
Sheets("Sheet1").Activate
Range("B" & Rows.Count).End(xlUp).Offset(1).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=True
Application.CutCopyMode = False
Workbooks.Open (strP & "\" & strF)
Sheets("Sheet1").Activate
Range("A1:AZ1").Select 'Change as required
Selection.Copy
ActiveWorkbook.Save
ActiveWorkbook.Close
Windows("CompiledData.xlsx").Activate
'Change as required
Sheets("Sheet1").Activate
Range("A" & Rows.Count).End(xlUp).Offset(1).Select 'Change as required
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=True
Application.CutCopyMode = False
ActiveWorkbook.Sheets(1).Activate
ActiveWorkbook.Save
ActiveWorkbook.Close 'Closes the active workbook
strF = Dir()
Loop
End Sub
I am looking to add some code which would paste the file name of the sheet from which the Item Name and Measured Value are taken in Column C, ideally filling every row. I hope the table above is helpful to visualize what I mean. Thank you!!
Last edited by a moderator: