Need help with vba code

evelynbonto12

New Member
Joined
May 24, 2023
Messages
7
Office Version
  1. 365
Platform
  1. Windows
Hi everyone, this is my first time with VBA. I need help extracting data. This is what I want to do:

1. Open the source workbook then search column A with labels from P1-P168 (there are other labels in this column like B1, T2 etc.)
2. From the multiple P1 found, I want to find the minimum value (usually 0) and maximum value in column B.
3.When found, I want to copy the corresponding values of V2 and V3 for the minimum and maximum values and paste in on my destination workbook. (minimum is start and maximum is end)

Im not good at English so I hope this gets understood very well.
 

Attachments

  • destination.jpg
    destination.jpg
    60.7 KB · Views: 8
  • source.jpg
    source.jpg
    247.4 KB · Views: 5

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Could you post a copy of your 2 sheets using the XL2BB add in, or alternatively, share your file via Dropbox, Google Drive or similar file sharing platform.
 
Upvote 0
Thanks for that (y) I note that your 'source' file has a different format in column A than what you showed us in post #1. Will the data have a "P" prefix before the number or not? Also, do you expect to run the code with both the source and destination files already open, or should both sheets be in the same workbook, and if they are in separate workbooks, which one do you want to house the code in?
 
Upvote 0
In the source file column A, there are a lot of labels (even with just numbers), I just want the ones with the prefix P. Also, I want to run the code in the destination files only if that's ok. The source file is also not opened initially. Thank you so much. This is for my thesis and I just want to automate extracting data since I will have 20 source files.
 
Upvote 0
Try the following on a copy of your workbook. The code sits in a standard module in the destination file, and assumes your layouts are exactly as you've indicated in your shared files. The code will ask you to locate & select the source file first. A link to the destination file is also provided here.

destination macro.xlsm

VBA Code:
Option Explicit
Sub evelynbonto12()
    Application.ScreenUpdating = False
    Dim wb1 As Workbook, wb2 As Workbook
    Dim FileName As String
    Set wb1 = ThisWorkbook
    
    FileName = Application.GetOpenFilename _
    (filefilter:="Excel files (*.xlsx),*.xlsx", MultiSelect:=False)
    If Dir(FileName) <> "" Then
        Set wb2 = Workbooks.Open(FileName)
        
        Dim ws1 As Worksheet, ws2 As Worksheet
        Set ws1 = wb1.Worksheets(1)
        Set ws2 = wb2.Worksheets(1)
        
        With ws2.Range("A3:G" & ws2.Cells(Rows.Count, "G").End(xlUp).Row)
            .AutoFilter 1, "P*"
            .Offset(1).Copy ws1.Range("H5")
        End With
        wb2.Close 0
        
        Dim LR As Long
        LR = ws1.Range("H:N").Find("*", , xlFormulas, , xlByRows, xlPrevious).Row
        
        With ws1.Range("C5:C" & ws1.Cells(Rows.Count, "B").End(xlUp).Row)
            .FormulaR1C1 = "=SUMPRODUCT(--(R5C9:R" & LR & "C9=MINIFS(R5C9:R" & LR & "C9,R5C8:R" & LR & _
            "C8,RC2))*(R5C8:R" & LR & "C8=RC2),R5C13:R" & LR & "C13)"
        End With
        With ws1.Range("D5:D" & ws1.Cells(Rows.Count, "B").End(xlUp).Row)
            .FormulaR1C1 = "=SUMPRODUCT(--(R5C9:R" & LR & "C9=MAXIFS(R5C9:R" & LR & "C9,R5C8:R" & LR & _
            "C8,RC2))*(R5C8:R" & LR & "C8=RC2),R5C13:R" & LR & "C13)"
        End With
        With ws1.Range("E5:E" & ws1.Cells(Rows.Count, "B").End(xlUp).Row)
            .FormulaR1C1 = "=SUMPRODUCT(--(R5C9:R" & LR & "C9=MINIFS(R5C9:R" & LR & "C9,R5C8:R" & LR & _
            "C8,RC2))*(R5C8:R" & LR & "C8=RC2),R5C14:R" & LR & "C14)"
        End With
        With ws1.Range("F5:F" & ws1.Cells(Rows.Count, "B").End(xlUp).Row)
            .FormulaR1C1 = "=SUMPRODUCT(--(R5C9:R" & LR & "C9=MAXIFS(R5C9:R" & LR & "C9,R5C8:R" & LR & _
            "C8,RC2))*(R5C8:R" & LR & "C8=RC2),R5C14:R" & LR & "C14)"
        End With
        
        With ws1
            With .Range("C5:F" & LR)
                .Value2 = .Value2
            End With
            .Range("H:N").EntireColumn.Delete
        End With
    End If
    Application.ScreenUpdating = True
End Sub

Destination sheet before running code (partial view)
destination macro.xlsm
ABCDEF
1
2
3V2V3
4StartEndStartEnd
5P1
6P2
7P3
8P4
9P5
10P6
11P7
12P8
13P9
14P10
Sheet1


After running the code:
destination macro.xlsm
ABCDEF
1
2
3V2V3
4StartEndStartEnd
5P1-0.580990.8571780.340668-0.34067
6P2-0.831810.9120450.340668-0.34067
7P3-1.208311.3687950.306601-0.3066
8P4-0.912940.9335180.306601-0.3066
9P5-1.435481.4695840.306601-0.3066
10P6-0.945070.9292740.306601-0.3066
11P7-1.514551.5207280.306601-0.3066
12P8-0.854630.806790.306601-0.3066
13P9-1.515091.5070440.306601-0.3066
14P10-0.683810.6325950.306601-0.3066
15P11-1.48541.4787140.306601-0.3066
16P12-0.559190.5511320.306601-0.3066
17P13-1.4791.4859210.306601-0.3066
18P14-0.551960.5610490.306601-0.3066
19P15-1.508391.5170240.306601-0.3066
20P16-0.639730.6920510.306601-0.3066
21P17-1.523951.5184440.306601-0.3066
22P18-0.815460.8620030.306601-0.3066
23P19-1.475531.4422920.306601-0.3066
24P20-0.93380.9484140.306601-0.3066
25P21-1.377031.2284930.306601-0.3066
26P22-0.935510.9155790.306601-0.3066
27P23-0.870080.5852910.340668-0.34067
28P24-0.921040.8386790.340668-0.34067
29P250.615834-0.666160.511662-0.51166
30P26-0.921441.009150.511662-0.51166
31P270.473797-0.423570.460496-0.4605
32P28-0.99181.0174720.460496-0.4605
33P290.299347-0.265160.460496-0.4605
34P30-1.025761.0057830.460496-0.4605
35P310.211482-0.201150.460496-0.4605
36P32-0.900270.835560.460496-0.4605
37P330.197221-0.201680.460496-0.4605
38P34-0.642230.5598890.460496-0.4605
39P350.215545-0.219540.460496-0.4605
40P36-0.392650.3522950.460496-0.4605
41P370.218846-0.214330.460496-0.4605
42P38-0.354820.3969390.460496-0.4605
43P390.199427-0.194720.460496-0.4605
44P40-0.567280.649420.460496-0.4605
45P410.198469-0.208780.460496-0.4605
46P42-0.840490.9045530.460496-0.4605
47P430.262553-0.2970.460496-0.4605
48P44-1.009691.0292810.460496-0.4605
49P450.42238-0.472190.460496-0.4605
50P46-1.021480.9965910.460496-0.4605
51P470.666746-0.617260.511662-0.51166
52P48-1.014460.9261830.511662-0.51166
53P490.696139-0.812860.502662-0.50266
54P50-0.89110.9757730.502662-0.50266
55P510.676614-0.639230.452396-0.4524
56P523.446718-3.523212.569183-2.61884
57P530.564181-0.544450.452396-0.4524
58P543.441143-3.519962.583987-2.61225
59P550.519859-0.518640.452396-0.4524
60P563.263223-3.341642.436417-2.44154
61P570.530736-0.539380.452396-0.4524
62P583.047079-3.056192.202013-2.16381
63P590.559092-0.5640.452396-0.4524
64P602.75031-2.713051.930331-1.89146
65P610.563414-0.558030.452396-0.4524
66P622.721343-2.762291.897085-1.93875
67P630.537437-0.528590.452396-0.4524
68P643.059261-3.053382.169445-2.20925
69P650.516266-0.517460.452396-0.4524
70P663.348563-3.272792.448332-2.44405
71P670.541993-0.561760.452396-0.4524
72P683.527689-3.451912.617884-2.59087
73P690.636661-0.674260.452396-0.4524
74P703.535471-3.460642.626318-2.5777
75P710.811176-0.697070.502662-0.50266
76P72-0.98070.895370.502662-0.50266
77P731.141251-1.247730.742684-0.74268
78P744.712274-4.599713.190013-3.17828
79P751.000253-0.983870.629443-0.63665
80P76-0.970050.9997810.452396-0.4524
81P770.922692-0.893630.636648-0.62944
82P78-1.013380.9941830.452396-0.4524
83P790.884273-0.90460.629443-0.63665
84P80-0.882870.8125950.452396-0.4524
85P810.932687-0.932260.636648-0.62944
86P82-0.596490.5015010.452396-0.4524
87P830.963365-0.985990.629443-0.63665
88P84-0.307790.2611120.452396-0.4524
89P850.98526-0.962110.636648-0.62944
90P86-0.264780.3137670.452396-0.4524
91P870.930125-0.930360.629443-0.63665
92P88-0.510880.6059330.452396-0.4524
93P890.902294-0.882020.636648-0.62944
94P90-0.820150.8894050.452396-0.4524
95P910.8913-0.920410.629443-0.63665
96P92-0.998261.0170520.452396-0.4524
97P930.981621-0.998110.636648-0.62944
98P94-1.003250.9739990.452396-0.4524
99P951.246145-1.142590.742684-0.74268
100P964.582884-4.685853.172354-3.17939
101P970.551211-0.605380.502662-0.50266
102P98-0.869760.9649860.502662-0.50266
103P990.486538-0.456270.452396-0.4524
104P100-0.968321.000360.452396-0.4524
105P1010.403036-0.393640.452396-0.4524
106P102-1.018371.000820.452396-0.4524
107P1030.400135-0.413450.452396-0.4524
108P104-0.89350.8240620.452396-0.4524
109P1050.463653-0.487550.452396-0.4524
110P106-0.605090.5074690.452396-0.4524
111P1070.537438-0.549550.452396-0.4524
112P108-0.305220.2559830.452396-0.4524
113P1090.548474-0.535640.452396-0.4524
114P110-0.259170.3106650.452396-0.4524
115P1110.484539-0.460490.452396-0.4524
116P112-0.515740.6133310.452396-0.4524
117P1130.410523-0.39740.452396-0.4524
118P114-0.829570.8981050.452396-0.4524
119P1150.391548-0.401090.452396-0.4524
120P116-1.004321.0216610.452396-0.4524
121P1170.454775-0.48510.452396-0.4524
122P118-1.004490.9731550.452396-0.4524
123P1190.604201-0.550690.502662-0.50266
124P120-0.971860.8751330.502662-0.50266
125P1213.495531-3.45472.001808-1.9956
126P1223.036375-2.923132.294635-2.28839
127P1230.443363-0.424490.556921-0.56199
128P124-0.564890.5826070.569552-0.57516
129P1250.380395-0.366410.561991-0.55692
130P126-0.597310.5920820.575155-0.56955
131P1270.39304-0.427590.556921-0.56199
132P128-0.491650.4158930.569552-0.57516
133P1290.509142-0.535950.561991-0.55692
134P130-0.215840.1377910.575155-0.56955
135P1310.613951-0.64360.556921-0.56199
136P1320.049078-0.106420.569552-0.57516
137P1330.642363-0.611790.561991-0.55692
138P1340.102875-0.043330.575155-0.56955
139P1350.532477-0.505570.556921-0.56199
140P136-0.146630.224660.569552-0.57516
141P1370.424807-0.390530.561991-0.55692
142P138-0.422790.4975940.575155-0.56955
143P1390.364181-0.378270.556921-0.56199
144P140-0.595880.6007560.569552-0.57516
145P1410.42217-0.440990.561991-0.55692
146P142-0.585850.5684690.575155-0.56955
147P1433.455806-3.493541.997739-2.00256
148P1442.935016-3.046382.296068-2.30158
149P145-1.50891.6133090.328287-0.32829
150P146-0.443590.4305910.331668-0.33167
151P147-1.523551.5434470.295459-0.29546
152P148-0.434770.45570.298501-0.2985
153P149-1.561351.555940.295459-0.29546
154P150-0.468180.4582390.298501-0.2985
155P151-1.510471.4793770.295459-0.29546
156P152-0.39610.3557850.298501-0.2985
157P153-1.380851.3366860.295459-0.29546
158P154-0.22920.1721630.298501-0.2985
159P155-1.243381.2199770.295459-0.29546
160P156-0.053250.024580.298501-0.2985
161P157-1.221331.2456520.295459-0.29546
162P158-0.02640.0566160.298501-0.2985
163P159-1.34011.3842460.295459-0.29546
164P160-0.177550.2345380.298501-0.2985
165P161-1.481561.5122750.295459-0.29546
166P162-0.359820.3995430.298501-0.2985
167P163-1.557281.562580.295459-0.29546
168P164-0.460280.4699960.298501-0.2985
169P165-1.545061.5253570.295459-0.29546
170P166-0.457320.4365690.298501-0.2985
171P167-1.616051.5113130.328287-0.32829
172P168-0.432530.4448720.331668-0.33167
Sheet1
 
Upvote 1
Solution
OMG Thank you. Ill have to try this. In what part do you I think I could change to set the file path of the source file?
 
Upvote 0

Forum statistics

Threads
1,215,205
Messages
6,123,634
Members
449,109
Latest member
Sebas8956

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top