Using VBA Code for a Data Entry Form

UpToTheGills

New Member
Joined
Aug 26, 2022
Messages
38
Office Version
  1. 2019
Platform
  1. MacOS
… because macOS excel ¯\_(ツ)_/¯

(Using Excel 2019 v.16.65 on macOS so no built-in Forms creation functionality.)

From googling, the alternative solution if you're on macOS is to use VBA code and I found this helpful webpage with example code so I'm attempting to adapt it for my own use case but I'm not familiar with VBA.

Screenshot 2022-09-16 at 09.56.41.png


My use case is for a trade journal spreadsheet.
To use a data entry form on a worksheet (called Data Input 👆) to insert trades into the trade journal worksheet.

The data entry cells are the ones coloured green:
VBA Code:
myCopy = "F5,F6,F8,F9,F11,F12,F13,F14,F16,F17,F18,F19,F20"

  • F5 and F6 contain the formula =TODAY() to save input however if overwritten with a date other than the current day, I want the code to check cells F5 and F6 and reinsert the =TODAY() if necessary.
  • The worksheet where the data is to be inserted is arranged with the latest data at the top rather than at the bottom whereby additional lines are inserted when needed. I need the code to check that there are free rows to insert the data, if not, to stop and show a warning OR ideally, to run one of the macros I have to insert new rows.
  • One additional point is that although the date and time are separate on the Data Input worksheet, in the journal worksheet they are combined in a single cell.
This is as far as I've got with the code:

VBA Code:
Sub UpdateTradeJournal()

    'some of these DIMs may either not be needed or incorrect or perhaps need additional ones?
    Dim historyWks As Worksheet
    Dim inputWks As Worksheet

    Dim nextRow As Long
    Dim oCol As Long

    Dim myRng As Range
    Dim myCopy As String
    Dim myCell As Range
    
    'cells to copy from Input sheet
    myCopy = "F5,F6,F8,F9,F11,F12,F13,F14,F16,F17,F18,F19,F20"

    Set inputWks = Worksheets("Data Input")
    Set historyWks = Worksheets("IC Live 4004xxxx")

    'haven't tried to amend any of the below code as yet
    With historyWks
        nextRow = .Cells(.Rows.Count, "A").End(xlUp).Offset(1, 0).Row
    End With

    With inputWks
        Set myRng = .Range(myCopy)

        If Application.CountA(myRng) <> myRng.Cells.Count Then
            MsgBox "Please fill in all the cells!"
            Exit Sub
        End If
    End With

    With historyWks
        With .Cells(nextRow, "A")
            .Value = Now
            .NumberFormat = "mm/dd/yyyy hh:mm:ss"
        End With
        .Cells(nextRow, "B").Value = Application.UserName
        oCol = 3
        For Each myCell In myRng.Cells
            historyWks.Cells(nextRow, oCol).Value = myCell.Value
            oCol = oCol + 1
        Next myCell
    End With
    
    'clear input cells that contain constants
    With inputWks
      On Error Resume Next
         With .Range(myCopy).Cells.SpecialCells(xlCellTypeConstants)
              .ClearContents
              Application.Goto .Cells(1) ', Scroll:=True
         End With
      On Error GoTo 0
    End With

End Sub

I will use XL2BB to get a section of the journal worksheet posted up shortly.
Looking forward to learning something new and gratefully for any and all help received. Thanks in advance.
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
Trading Journal LIVE.xlsm
ABCEFGHIJKLMNOPQRSTUVW
1PricesProfit / Loss
2#Broker Entry TimeBroker Exit TimeExit TimeTypePairDirectionPos. SizeEntryStop LossTake ProfitExitGross P&LW / LRRRTrade DurationComm.Swap£ (Net)%PipsA/c Balance
3         
4475         
5474         
6473         
7472         
847114-Sep 17:0114-Sep 18:4714-Sep 16:47FXGBPAUDShort0.101.716981.716001.713451.7134420.66WinRF/BE1h 46m0.5020.161.01%352,023.64
947014-Sep 13:5114-Sep 14:2014-Sep 12:20FXGBPUSDShort0.501.156011.154691.1546260.19Win 29m2.5057.692.96%142,003.48
IC Live 4004xxx
Cell Formulas
RangeFormula
E3:E9E3=IF(C3>0,C3-(1/12),"")
O3:O9O3=IF(AND(D3>0,M3>0),IFS(OR(AND(H3="Short",J3>M3),AND(H3="Long",J3<M3)),"Win",OR(AND(H3="Short",J3<M3),AND(H3="Long",J3>M3)),"Loss",J3-M3=0,"n/a"),"")
P3:P9P3=IF(AND(ISNUMBER(B3),OR(AND(H3="Short",ISNUMBER(K3),K3<J3),AND(H3="Long",K3>J3))),"RF/BE",IF(AND(ISNUMBER(B3),OR(AND(H3="Short",K3>J3,ISNUMBER(L3)),AND(H3="Long",ISNUMBER(K3),K3<J3,ISNUMBER(L3)))),ROUND((L3-J3)/(J3-K3),1),""))
Q3:Q9Q3=IF(ISNUMBER(C3),C3-B3,"")
R3:R9R3=IF(AND(B3>0,F3="FX"),(I3/0.01)*0.05,IF(OR(AND(B3>0,OR(F3="Crypto",F3="Indices")),ISBLANK(B3)),""))
T3:T9T3=IF(ISBLANK(N3),"",IF(AND(ISNUMBER(R3),ISNUMBER(S3)),N3-R3+S3,IF(AND(NOT(ISNUMBER(R3)),ISNUMBER(S3)),N3+S3,IF(AND(ISNUMBER(N3),ISNUMBER(R3)),N3-R3,N3))))
U3U3=IF(C3>0,T3/#REF!,"")
V3:V9V3=IF(AND(F3="FX",H3="Short"),(J3-M3)*10000,IF(AND(F3="FX",H3="Long"),(M3-J3)*10000,IF(AND(F3="Crypto",H3="Short"),(J3-M3),IF(AND(F3="Crypto",H3="Long"),(M3-J3),""))))
W3W3=IF(B3>0,X3-#REF!+#REF!+T3,"")
U4:U9U4=IF(C4>0,T4/W5,"")
W4:W9W4=IF(B4>0,X4-X5+W5+T4,"")
Cells with Conditional Formatting
CellConditionCell FormatStop If True
O3:O478Cell Valuecontains "Win"textNO
O3:O478Cell Valuecontains "Loss"textNO
O3:O478Cell Value="n/a"textNO
R479,Q3:Q478Cell Value<0.04167textYES
R479,Q3:Q478Cell Value<1textNO
J3:M478Expression=AND($F3="FX",$G3="GBP JPY")textYES
O479,N3:N478Cell Value>0textNO
O479,N3:N478Cell Value<0textNO
J3:M478Expression=$F3="FX"textNO
J3:M478Expression=OR($F3="Crypto",$F3="Indices")textNO
H3:H479Cell Value="Long"textNO
H3:H479Cell Value="Short"textNO
S479:W479,R3:V478Cell Value<0textNO
Cells with Data Validation
CellAllowCriteria
N3:N9Custom=IF(O3="Loss",N3<0,IF(O3="Win",N3>0,""))
E3:E478Text length=0
F3:F177List='Data Lists'!$A$3:$A$6
G3:G478List='Data Lists'!$C$3:$C$11
H3:H478List='Data Lists'!$E$3:$E$4


Screenshot 2022-09-16 at 10.44.07.png
 
Upvote 0
OK, first thing first, need to map the data from the Data Input worksheet to the correct cells in the journal worksheet:

Column B = F5 and F8
Column C = F6 and F9

Column F = F11
Column G = F12
Column H = F13
Column I = F14

Column J = F16
Column K = F17
Column L = F18
Column M = F19
Column N = F20

So it is mostly contiguous across columns, skipping D and E only.
 
Upvote 0
Oops, I deleted some rows on the journal worksheet which has resulted in some #REF! errors, reposting...

Trading Journal LIVE.xlsm
ABCEFGHIJKLMNOPQRSTUVW
1PricesProfit / Loss
2#Broker Entry TimeBroker Exit TimeExit TimeTypePairDirectionPos. SizeEntryStop LossTake ProfitExitGross P&LW / LRRRTrade DurationComm.Swap£ (Net)%PipsA/c Balance
3         
4475         
5474         
6473         
7472         
847114-Sep 17:0114-Sep 18:4714-Sep 16:47FXGBPAUDShort0.101.716981.716001.713451.7134420.66WinRF/BE1h 46m0.5020.161.01%352,023.64
947014-Sep 13:5114-Sep 14:2014-Sep 12:20FXGBPUSDShort0.501.156011.154691.1546260.19Win 29m2.5057.692.96%142,003.48
IC Live 4004xxx
Cell Formulas
RangeFormula
E3:E9E3=IF(C3>0,C3-(1/12),"")
O3:O9O3=IF(AND(D3>0,M3>0),IFS(OR(AND(H3="Short",J3>M3),AND(H3="Long",J3<M3)),"Win",OR(AND(H3="Short",J3<M3),AND(H3="Long",J3>M3)),"Loss",J3-M3=0,"n/a"),"")
P3:P9P3=IF(AND(ISNUMBER(B3),OR(AND(H3="Short",ISNUMBER(K3),K3<J3),AND(H3="Long",K3>J3))),"RF/BE",IF(AND(ISNUMBER(B3),OR(AND(H3="Short",K3>J3,ISNUMBER(L3)),AND(H3="Long",ISNUMBER(K3),K3<J3,ISNUMBER(L3)))),ROUND((L3-J3)/(J3-K3),1),""))
Q3:Q9Q3=IF(ISNUMBER(C3),C3-B3,"")
R3:R9R3=IF(AND(B3>0,F3="FX"),(I3/0.01)*0.05,IF(OR(AND(B3>0,OR(F3="Crypto",F3="Indices")),ISBLANK(B3)),""))
T3:T9T3=IF(ISBLANK(N3),"",IF(AND(ISNUMBER(R3),ISNUMBER(S3)),N3-R3+S3,IF(AND(NOT(ISNUMBER(R3)),ISNUMBER(S3)),N3+S3,IF(AND(ISNUMBER(N3),ISNUMBER(R3)),N3-R3,N3))))
U3:U9U3=IF(C3>0,T3/W4,"")
V3:V9V3=IF(AND(F3="FX",H3="Short"),(J3-M3)*10000,IF(AND(F3="FX",H3="Long"),(M3-J3)*10000,IF(AND(F3="Crypto",H3="Short"),(J3-M3),IF(AND(F3="Crypto",H3="Long"),(M3-J3),""))))
W3:W9W3=IF(B3>0,X3-X4+W4+T3,"")
Cells with Conditional Formatting
CellConditionCell FormatStop If True
O3:O478Cell Valuecontains "Win"textNO
O3:O478Cell Valuecontains "Loss"textNO
O3:O478Cell Value="n/a"textNO
R479,Q3:Q478Cell Value<0.04167textYES
R479,Q3:Q478Cell Value<1textNO
J3:M478Expression=AND($F3="FX",$G3="GBP JPY")textYES
O479,N3:N478Cell Value>0textNO
O479,N3:N478Cell Value<0textNO
J3:M478Expression=$F3="FX"textNO
J3:M478Expression=OR($F3="Crypto",$F3="Indices")textNO
H3:H479Cell Value="Long"textNO
H3:H479Cell Value="Short"textNO
S479:W479,R3:V478Cell Value<0textNO
Cells with Data Validation
CellAllowCriteria
N3:N9Custom=IF(O3="Loss",N3<0,IF(O3="Win",N3>0,""))
E3:E478Text length=0
F3:F177List='Data Lists'!$A$3:$A$6
G3:G478List='Data Lists'!$C$3:$C$11
H3:H478List='Data Lists'!$E$3:$E$4
 
Upvote 0
Am going to have a stab at selecting the topmost empty cell in Column B on the trade journal worksheet.

In the example, it is:
VBA Code:
    With historyWks
        nextRow = .Cells(.Rows.Count, "A").End(xlUp).Offset(1, 0).Row
    End With

Am amending to:
VBA Code:
    With historyWks
        nextRow = .Cells(.Rows.Count, "B").End(xlDown).Offset(-1, 0).Row
    End With

Would this work to select cell B7 which is where I want the first piece of data from the input worksheet to be inserted?

Actually, watching a YouTube video entitled #Excel VBA to Select the Next Empty Cell or Row - Macros I'm thinking the below code could work better:
VBA Code:
    With historyWks
        nextRow = .Cells("B3").End(xlDown).Offset(-1, 0).Row
    End With
 
Last edited:
Upvote 0
👆 think the syntax in that last code example was wrong, s/b:
VBA Code:
    With historyWks
        nextRow = .Cells(3, 2).End(xlDown).Offset(-1, 0).Row
    End With

This is to select cell B3 then CTRL + ↓ and offset back up by 1 to result in cell B7 being selected.
 
Upvote 0
Can get rid of:
VBA Code:
        With .Cells(nextRow, "A")
            .Value = Now
            .NumberFormat = "mm/dd/yyyy hh:mm:ss"
        End With
as not relevant to my use case.
 
Upvote 0
TL;DR

I may have asked for too much so I'll put this summary of what I am trying to achieve and try to keep it as brief as possible. Then I'll start to research and work through each step myself and if anyone can offer any ideas or pointers, that will be gratefully received.
  1. I need to create a Data Entry Worksheet Form using VBA. Why? Because I'm using Excel 2019 on macOS so the functionality of Forms doesn't exist. Am using the code from here and adapting it to my own use case.
  2. To adapt for my own use case I need to:
    1. insert data at the top rather than bottom of the table where the data is stored
    2. check to see if there is space to insert data at the top; if not, insert additional lines using a separate macro
    3. date and time are separate on the form but need to be combined in the same cell in the table where the data is stored ¹
    4. when the macro runs, there needs to be check to make sure that there is data in all mandatory fields on the form, which is all of them except Stop Loss and Take Profit ²
    5. on the form, the two cells default to the value of =TODAY() but if that has been overwritten with a different date a check needs to happen when the form is cleared so that the formula is put back if not there
The range of cells on the Worksheet Form is F5,F6,F8,F9,F11,F12,F13,F14,F16,F17,F18,F19,F20 of which
  • ¹ F5,F8 and F6,F9 need to be combined in a single cells where the data is stored
  • ² F5,F6,F8,F9,F11,F12,F13,F14,F16,F19,F20 are mandatory and F17,F18 are optional
  • Data Entry Worksheet Form is called Data Input
  • The Worksheet where the data is stored is called IC Live 4004xxxx
 
Upvote 0
Re: 2. 1. as previously mentioned, to navigate to the correct starting point on the data worksheet, I am hoping that
VBA Code:
    With historyWks
        nextRow = .Cells(3, 2).End(xlDown).Offset(-1, 0).Row
    End With
will work?
 
Upvote 0
Useful video: Build Professional Forms in Excel - Data Entry Professional from TeachExcel

A more basic approach and would overcome the issue that where the data is inserted isn't one contiguous block.
 
Upvote 0

Forum statistics

Threads
1,215,003
Messages
6,122,655
Members
449,091
Latest member
peppernaut

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