OK, I am trying to make a macro-enabled workbook template. I am fairly new at marcoing so there is gaps in my knowledge on how to code. I would like help writing explicit code on what I want it to do.
This is what the first sheet looks like. The template has 19 sheets.
1.png (image 1)
Briefly, the stuff in columns A and B should be straight forward. B1 is the folder path to where all the data files are located.
Column D is an arbitrary numbering system which can change depending on what data we have, so don't use that as a counter reference.
Column E is what sheet all the data goes. As you can see, there are 16 sheets and each sheet can hold 9 file names.
Column F, G, and H are copy-pasted from a command prompt batch file:
in this case, it is:
cd Desktop\12-025 11-487 11-607 11-356
dir /b *res > output.txt
where the output file is in alphabetical order so the user can just paste the RL..., RT..., and RD... files to the correct column.
If there is a way to do this in Excel, that would be great, but the current method for file name extraction works pretty well
So I written code and pseudo code for what I want to do for the template. For the parts written in pseudo code, I need some help
Public Wkd, Wks As Workbook
Public Wsd, Wss As Worksheet
Private Sub CommandButton1_Click()
Set Wkd = ActiveWorkbook
Set Wsd = Wkd.ActiveSheet
2.png (Image 2)
*need help here*
'Set the 16 worksheets where data is imported
'Set the raw data sheet
'for all 16 data import worksheets .Range("A3:BA102").ClearContents
On Error Resume Next
Pth = Wsd.Range("B1").Value
ChDir Pth
*need help here*
'I probably need to write a subroutine and pass all the variables:
..so basically, the raw data result files all have the ".res" extension but for those familiar with the text "origin code," it's equal to 437.
An example of a raw data file looks like this: (there are 81 to 144 raw data files that must be imported to this template:
Tensile Test Analysis (SOP MTL-010)
TAnalyze version:
1 0 4
File:
L:\TestFile\09-486\RDHC
Analyzed By:
Pulled by:
FRANK
Project No:
09-486
Date Tested:
12/07/2009
Date Analyzed:
12/07/2009
Material:
BH210
Specimen:
RDHC
Width:
12.7508
mm
Thickness (stripped):
1.92532
mm
YPE Present?
YPE
Lower Yield Point:
264.5446
MPa
Upper Yield Point:
268.8933
MPa
% YPE:
0.5418773
Tensile Strength:
369.6377
MPa
% Uniform Elong:
16.86088
% Total Elong:
33.42488
r Value:
1.10
15.1% strain
n Value:
0.1656188
K:
586.8695
MPa
n Value Strain Range
10.0% - 16.9%
Note:
""
True Stress - True Plastic Strain Data
(through end of uniform elongation)
True Plastic True Stress
Strain (ksi) (MPa)
0.00000 0.9 6.2
-0.00004 3.3 23.0
0.00010 7.3 50.4
...
Engineering Stress - Strain Data
(entire stress - strain curve)
Strain Stress
(%) (ksi) (MPa)
0.00 0.9 6.2
0.01 3.3 23.0
0.02 6.3 43.5
...
*need serious help here*
So basically, I don't know how to write this part of the code:
What I want to do is take each individual file name (image 1) from columns F, G, and H and import one-by-one into the "raw data" sheet (image 2).
I need a nested for loop.
basically, the loop should reach each individual file name horizontally across the rows and then zig-zag down the columns:
So, from image 1, it should read:
RLH12p
RTH12p
RDH12p
RLH22p
RTH22p
RDH22p
RLH32p
RTH32p
RDH32p
...etc (note: there is no pattern with the file names. It could be named anything)
For the first file name, I want "RLH12p" to attach a ".res" extension behind it and then import it from the correct file location to the "raw data" sheet.
upon recording myself, I got the code:
Workbooks.OpenText Filename:="TEXT;C:\Users\Desktop\12-025 11-487 11-607 11-356\RLH12p.res", Origin:=437, StartRow:=1, _
DataType:=xlFixedWidth, FieldInfo:=Array(Array(0, 1), Array(10, 1), _
Array(20, 1)), TrailingMinusNumbers:=True
Not sure if I could use this
Here is the code to process the raw data. I don't know how to activate the "raw data" sheet for this code to work there:
LR = Ws.Range("A" & Rows.Count).End(xlUp).Row
For i = 1 To LR
Del = Ws.Range("A" & i).Value
If Del = "Strain" Then
Ws.Range("1:" & i - 4).Delete Shift:=xlShiftUp
Exit For
End If
Next i
For i = 1 To LR
Reg = Ws.Range("A" & i).Value
If Reg = "(%)" Then
Ws.Range("A" & i).CurrentRegion.Cut Destination:=Ws.Range("E1")
Exit For
End If
Next i
Ws.Range("1:4").Delete Shift:=xlShiftUp
Ws.Range("B1").EntireColumn.Delete
Ws.Range("F1").EntireColumn.Delete
Now, the raw data sheet looks like this:
3.png (image 3)
I want to copy the data set of the first region and paste it into the designated sheet (refer to the caption under image 1):
For the data collected from RLH12p, the first set of data needs to be pasted in column A and B, the second set needs to be pasted in column J and K
4.png (image 4)
So for the first sheet,
RLH12p has data in AB and JK
RTH12p has data in DE and MN
RDH12p has data in GH and PQ
RLH22p has data in ST and ABAC
RTH22p has data in VW and AEAF
RDH22p has data in YZ and AHAI
RLH32p has data in AKAL and ATAU
RTH32p has data in ANAO and AWAX
RDH32p has data in AQAR and AZBA
this needs to be looped
Now, for the next set of 9 file names, the next sheet needs to be activated. All sheets look congruent. But for RLH42p to RDH62p, it undergoes the same scheme.
.....this was the old code. obviously, the new code is different but you can refer to it:
For i = 2 To LRNm
Nm = Wsd.Range("BE" & i).Value
ONm = Nm & ".xls"
Application.Workbooks.Open Filename:=ONm
Set Wks = ActiveWorkbook
Set Wss = ActiveSheet
If Wks.Name = Wkd.Name Then GoTo Line1
Wss.Range("F4").Select
Wss.Range("F1").EntireColumn.Delete
Wss.Range("B1").EntireColumn.Delete
LRT = Wss.Range("A" & Rows.Count).End(xlUp).Row
LRE = Wss.Range("D" & Rows.Count).End(xlUp).Row
Wss.Range("D:E,A:B").ColumnWidth = 13.29
With Wss.Range("A4:A" & LRT)
.NumberFormat = "0.00000"
.HorizontalAlignment = xlCenter
End With
With Wss.Range("B5:B" & LRT)
.NumberFormat = "0.0"
.HorizontalAlignment = xlCenter
End With
With Wss.Range("D5:D" & LRE)
.NumberFormat = "0.00"
.HorizontalAlignment = xlCenter
End With
With Wss.Range("E5:E" & LRE)
.NumberFormat = "0.0"
.HorizontalAlignment = xlCenter
End With
Wss.Range("C1").Select
Wkd.Activate
Wsd.Activate
Wsd.Range("A3").Select
For j = 1 To 53
With Wsd.Cells(1, j)
Set c = .Find(Nm, LookIn:=xlValues, LookAt:=xlPart, MatchCase:=True)
If Not c Is Nothing Then
With Wsd.Cells(1, j + 1)
Set d = .Find("True", LookIn:=xlValues, LookAt:=xlPart)
If Not d Is Nothing Then
Wss.Range("A5:B" & LRT).Copy Destination:=Wsd.Cells(3, j)
End If
Set d = .Find("Engg", LookIn:=xlValues, LookAt:=xlPart)
If Not d Is Nothing Then
Wss.Range("D5:E" & LRE).Copy Destination:=Wsd.Cells(3, j)
End If
End With
End If
End With
Next j
' Wks.Save
Wks.Close SaveChanges:=False
Wkd.Activate
Wsd.Activate
Line1:
Next i
But basically, that's the end of the program...loop through everything, get the file name, trim the raw data, place the correct raw data in the correct activated sheet.
If anyone could help me do this, I would appreciate it.
Thanks
Bookmarks