VBA to Capture Cell Value

jarett

Board Regular
Joined
Apr 12, 2021
Messages
165
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Trying to write a VBA script to capture the value in cell J2 each time we open the file, I would want to record the value in J2 in the rows below and maybe also record the date in the "I" column next to that value. Each time the file is opened it refreshes. I only got to the very first part of the VBA code, haven't found anything else on the web to help finish it off if its possible.
VBA Code:
Private Sub Workbook_Open()
Range("J3") = Range("J2").Value

End Sub
Order_Line_Fill_Rate.xlsx
ABCDEFGHIJ
1ItemCodeItemCodeDescWarehouseCodeQuantityOrderedQuantityBackorderedUDF_QTY_PULLEDUDF_INVENTORIED_ITEMTotal LinesBO LinesLine Fill %
21332L1332L TIG WELDING GLOVE CUT RE0004800Y149833078%
31332L1332L TIG WELDING GLOVE CUT RE00084084Y
410019063-GRAY-XLRMNS FR SLD VENT LS WRK SHRT SI000505Y
5CDKW038-NAVYBLUE20 oz. Viking Nova Tumbler00048480Y
610022599-GRAY-LGRMNS FR AIR HENLEY LS TOP SILVR000303Y
710012552-BLUE-3230MNS FR M4 RLX BSC BOOT CUT JEA000303Y
810012552-BLUE-4032MNS FR M4 RLX BSC BOOT CUT JEA000505Y
910019063-GRAY-XXLRMNS FR SLD VENT LS WRK SHRT SI000505Y
10SHE01-5000-ICE GREY-3XLGildan Heavy Cotton T-Shirt LE000505Y
11FQD810-10QUATRO DIELECTRIC REGULAR BOOT000101Y
12SHENHK01-G2XL-V3XL-R3XLSHELTON NEW HIRE KIT GLOVE 2XL000101Y
13FR44-YELLOW-3XLSealtex Flame FR Hi-Vis 50in C000101Y
Order_Fill_Rate
Cell Formulas
RangeFormula
H2H2=COUNTA(A:A)
I2I2=COUNTIF(E:E,"> 0")
J2J2=ABS((I2-H2)/H2)
Named Ranges
NameRefers ToCells
ExternalData_1=Order_Fill_Rate!$A$1:$G$1498H2
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
How about this?

VBA Code:
Private Sub Workbook_Open()
Dim LR As Long:     LR = Range("J" & Rows.Count).End(xlUp).Row + 1

Range("J" & LR) = Range("J2").Value
Range("I" & LR) = Now()
End Sub
 
Upvote 0
Solution
Question, the code worked and populated the row below my values, however when I just opened it again nothing happened? Will this only work once a day? Or do I have to run the VBA each time?
Order_Line_Fill_Rate.xlsm
HIJ
1Total LinesBO LinesLine Fill %
2160234678%
38/2/2023 13:0978%
4
5
6
Order_Fill_Rate
Cell Formulas
RangeFormula
H2H2=COUNTA(A:A)
I2I2=COUNTIF(E:E,"> 0")
J2J2=ABS((I2-H2)/H2)
Named Ranges
NameRefers ToCells
ExternalData_1=Order_Fill_Rate!$A$1:$G$1602H2
 
Upvote 0
No, it should work every time. I tested it a couple of times this morning before I sent the code. Seemed to be working as expected.

Record on Open.xlsm
ABCDEFGHIJ
1ItemCodeItemCodeDescWarehouseCodeQuantityOrderedQuantityBackorderedUDF_QTY_PULLEDUDF_INVENTORIED_ITEMTotal LinesBO LinesLine Fill %
21332L1332L TIG WELDING GLOVE CUT RE04800Y1310.923077
31332L1332L TIG WELDING GLOVE CUT RE084084Y8/2/2023 9:560.923077
410019063-GRAY-XLRMNS FR SLD VENT LS WRK SHRT SI0505Y8/2/2023 9:560.923077
5CDKW038-NAVYBLUE20 oz. Viking Nova Tumbler048480Y
610022599-GRAY-LGRMNS FR AIR HENLEY LS TOP SILVR0303Y
Sheet1


Off the top of my head, I can't think of a reason it wouldn't fire each time you open the workbook.
 
Upvote 0
No, it should work every time. I tested it a couple of times this morning before I sent the code. Seemed to be working as expected.

Record on Open.xlsm
ABCDEFGHIJ
1ItemCodeItemCodeDescWarehouseCodeQuantityOrderedQuantityBackorderedUDF_QTY_PULLEDUDF_INVENTORIED_ITEMTotal LinesBO LinesLine Fill %
21332L1332L TIG WELDING GLOVE CUT RE04800Y1310.923077
31332L1332L TIG WELDING GLOVE CUT RE084084Y8/2/2023 9:560.923077
410019063-GRAY-XLRMNS FR SLD VENT LS WRK SHRT SI0505Y8/2/2023 9:560.923077
5CDKW038-NAVYBLUE20 oz. Viking Nova Tumbler048480Y
610022599-GRAY-LGRMNS FR AIR HENLEY LS TOP SILVR0303Y
Sheet1


Off the top of my head, I can't think of a reason it wouldn't fire each time you open the workbook.
Hmm, when I open the workbook it runs a background query that I have pulling data from a SQL connection, could that be why? I've closed and opened it after a minute and waited 2 hours and I don't think the VBA ran except the first time I opened it.
 
Upvote 0
Does it work if you manually run it? I.E. open the VBE and hit F5.
 
Upvote 0
Are you sure that you are saving the file before closing each time to make sure those updates are saved to the file?
 
Upvote 0
OK, ran the VBA manually and it updated the line to this;
Order_Line_Fill_Rate.xlsm
HIJK
1Total LinesBO LinesLine Fill %
2162734779%
38/2/2023 13:0978%
48/2/2023 16:1579%
5
Order_Fill_Rate
Cell Formulas
RangeFormula
H2H2=COUNTA(A:A)
I2I2=COUNTIF(E:E,"> 0")
J2J2=ABS((I2-H2)/H2)
Named Ranges
NameRefers ToCells
ExternalData_1=Order_Fill_Rate!$A$1:$G$1627H2


Saved and closed the file, then reopened it and nothing happened. Opened the file again and opened VBA to manually run it and it worked.
1691011142699.png


Order_Line_Fill_Rate.xlsm
HIJ
1Total LinesBO LinesLine Fill %
2162734779%
38/2/2023 13:0978%
48/2/2023 16:1579%
58/2/2023 16:1879%
6
7
Order_Fill_Rate
Cell Formulas
RangeFormula
H2H2=COUNTA(A:A)
I2I2=COUNTIF(E:E,"> 0")
J2J2=ABS((I2-H2)/H2)
Named Ranges
NameRefers ToCells
ExternalData_1=Order_Fill_Rate!$A$1:$G$1627H2


The only thing thing I could see different is I am saving it under a "One Drive" folder, would that do something?
 
Upvote 0
That is a very important detail!
See here:
Well, at least we know the reason now, just trying to figure out if I can fix or have to get IT involved. Cant fix via the link you sent didn't work, tried moving the file to a regular folder on our physical server and then went into trust setting to enable all macros, VBA runs manually but still doesn't run when you open the file.
Getting IT involved now I guess.
 
Upvote 0

Forum statistics

Threads
1,215,159
Messages
6,123,351
Members
449,097
Latest member
thnirmitha

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