• Skip to main content
  • Skip to header right navigation
  • Skip to site footer

My Online Training Hub

Learn Dashboards, Excel, Power BI, Power Query, Power Pivot

  • Courses
  • Pricing
    • Free Courses
    • Power BI Course
    • Excel Power Query Course
    • Power Pivot and DAX Course
    • Excel Dashboard Course
    • Excel PivotTable Course – Quick Start
    • Advanced Excel Formulas Course
    • Excel Expert Advanced Excel Training
    • Excel Tables Course
    • Excel, Word, Outlook
    • Financial Modelling Course
    • Excel PivotTable Course
    • Excel for Customer Service Professionals
    • Excel for Operations Management Course
    • Excel for Decision Making Under Uncertainty Course
    • Excel for Finance Course
    • Excel Analysis ToolPak Course
    • Multi-User Pricing
  • Resources
    • Free Downloads
    • Excel Functions Explained
    • Excel Formulas
    • Excel Add-ins
    • IF Function
      • Excel IF Statement Explained
      • Excel IF AND OR Functions
      • IF Formula Builder
    • Time & Dates in Excel
      • Excel Date & Time
      • Calculating Time in Excel
      • Excel Time Calculation Tricks
      • Excel Date and Time Formatting
    • Excel Keyboard Shortcuts
    • Excel Custom Number Format Guide
    • Pivot Tables Guide
    • VLOOKUP Guide
    • ALT Codes
    • Excel VBA & Macros
    • Excel User Forms
    • VBA String Functions
  • Members
    • Login
  • Blog
  • Excel Webinars
  • Excel Forum
    • Register as Forum Member

Vba for logistic|VBA & Macros|Excel Forum|My Online Training Hub

You are here: Home / Vba for logistic|VBA & Macros|Excel Forum|My Online Training Hub

vba course banner

Avatar
sp_LogInOut Log In sp_Registration Register
sp_Search Search
Advanced Search|Last Search Results
Search
Forum Scope




Match



Forum Options



Minimum search word length is 3 characters - maximum search word length is 84 characters
sp_Search Search
sp_RankInfo
Lost password?
sp_CrumbsHome HomeExcel ForumVBA & MacrosVba for logistic
sp_PrintTopic sp_TopicIcon
Vba for logistic
Avatar
Neda Sheikhi
Member
Members
Level 0
Forum Posts: 10
Member Since:
August 24, 2022
sp_UserOfflineSmall Offline
1
August 24, 2022 - 9:17 pm
sp_Permalink sp_Print

Hi,

I have a excel with this code:

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column <> 12 Or Target.Columns.Count > 1 Then _
If Target.Column <> 14 Or Target.Columns.Count > 1 Then _
If Target.Column <> 16 Or Target.Columns.Count > 1 Then _
Exit Sub
Dim tmp As Variant
tmp = Cells(Target.Row, 16).Formula 'save contents
On Error GoTo Enable_Events
Application.EnableEvents = False
Cells(Target.Row, 16) = "#$"
Range("A1").Sort Key1:=Range("A1"), Order1:=xlAscending, Header:=xlYes
Cells(Application.Match("#$", Columns(16), 0), 1).Select
Range("L1").Sort Key1:=Range("L1"), Order1:=xlAscending, Header:=xlYes
Cells(Application.Match("#$", Columns(16), 0), 12).Select
Range("N1").Sort Key1:=Range("N1"), Order1:=xlAscending, Header:=xlYes
Cells(Application.Match("#$", Columns(16), 0), 14).Select
Range("P1").Sort Key1:=Range("P1"), Order1:=xlDescending, Header:=xlYes
Cells(Application.Match("#$", Columns(16), 0), 16).Select
Cells(Selection.Row, 16) = tmp 'restore contents
Enable_Events:
Application.EnableEvents = True
End Sub

 

now, I want to create new column, like this:

if Q1<G1, then insert new row below, then copy all formatting and numbers there, and G2=G1-Q1)

 

How can I use something like that in the Vba code?

I attached file here

Avatar
Catalin Bombea
Iasi, Romania
Admin
Level 10
Forum Posts: 1824
Member Since:
November 8, 2013
sp_UserOfflineSmall Offline
2
August 25, 2022 - 1:53 pm
sp_Permalink sp_Print

No file attached, please make sure you press the Start Upload button after you use the Add Files to Queue button.

Avatar
Neda Sheikhi
Member
Members
Level 0
Forum Posts: 10
Member Since:
August 24, 2022
sp_UserOfflineSmall Offline
3
August 27, 2022 - 2:47 pm
sp_Permalink sp_Print

ok

Avatar
Catalin Bombea
Iasi, Romania
Admin
Level 10
Forum Posts: 1824
Member Since:
November 8, 2013
sp_UserOfflineSmall Offline
4
August 27, 2022 - 5:57 pm
sp_Permalink sp_Print

The code you have is triggered by Worksheet Change event.

You want to insert new rows based on sheet changes as well? 

If column Q should insert rows, then the code should look like this:

If Not Intersect(Target, Range("Q2:Q1000")) Is Nothing Then
If Cells(Target.Row, "G") <> 0 And Target < Cells(Target.Row, "G") Then

Target.Offset(1, 0).EntireRow.Insert
Cells(Target.Row, "G").Offset(1, 0).Formula = "=" & Cells(Target.Row, "G").Address(False, False) & "-" & Target.Address(False, False)
End If
End If

 

You should also allow column Q to trigger the code:

 

If Target.Column <> 12 Or Target.Columns.Count > 1 Then _
If Target.Column <> 14 Or Target.Columns.Count > 1 Then _
If Target.Column <> 17 Or Target.Columns.Count > 1 Then _
Exit Sub

Avatar
Neda Sheikhi
Member
Members
Level 0
Forum Posts: 10
Member Since:
August 24, 2022
sp_UserOfflineSmall Offline
5
August 27, 2022 - 8:42 pm
sp_Permalink sp_Print

appreciate for answering me

Look at this file:

I always use Q column for green rows, when I enter the quantity less than G column, I want to insert row below that cells

now it happened at the end and newG=Gup-Qup (G10=G9-Q9) but for example: I enter in Q4, so new row insert in below, and G5=G4-Q4, and all information in row 4 copy to row 5

Avatar
Catalin Bombea
Iasi, Romania
Admin
Level 10
Forum Posts: 1824
Member Since:
November 8, 2013
sp_UserOfflineSmall Offline
6
August 28, 2022 - 12:34 am
sp_Permalink sp_Print sp_EditHistory

Ok, added code to copy row formats and values:

If Not Intersect(Target, Range("Q2:Q1000")) Is Nothing Then
If Cells(Target.Row, "G") <> 0 And Target < Cells(Target.Row, "G") Then
Target.Offset(1, 0).EntireRow.Insert
Range("A" & Target.Row & ":Q" & Target.Row).Copy _
Destination:=Range("A" & Target.Row + 1 & ":Q" & Target.Row + 1)
Cells(Target.Row, "G").Offset(1, 0).Formula = "=" & Cells(Target.Row, "G").Address(False, False) & "-" & Target.Address(False, False)
End If
End If

The part in red is the only addition.

I always use Q column for green rows, when I enter the quantity less than G column, I want to insert row below that cells

now it happened at the end and newG=Gup-Qup (G10=G9-Q9) but for example: I enter in Q4, so new row insert in below, and G5=G4-Q4, and all information in row 4 copy to row 5

Please keep in mind that the filtering you do after inserting the row is what pushes down the new row, the row is properly inserted below the target row.

Avatar
Neda Sheikhi
Member
Members
Level 0
Forum Posts: 10
Member Since:
August 24, 2022
sp_UserOfflineSmall Offline
7
August 28, 2022 - 2:08 pm
sp_Permalink sp_Print

Thank you

I add that code and look at the video attached

when I enter the quantity in Q cell, it is repeated in copy passed cell too. I want not to repeated there again

Avatar
Neda Sheikhi
Member
Members
Level 0
Forum Posts: 10
Member Since:
August 24, 2022
sp_UserOfflineSmall Offline
8
August 28, 2022 - 2:12 pm
sp_Permalink sp_Print

I can not uploaded video here

when I change in L column to "الصاق شد" then, I want to enter quantity in Q column. now when I enter in Q column, the quantity repeated in new row. I do not the Q cell repeated

Avatar
Philip Treacy
Admin
Level 10
Forum Posts: 1529
Member Since:
October 5, 2010
sp_UserOfflineSmall Offline
9
August 28, 2022 - 2:16 pm
sp_Permalink sp_Print

Hi Neda,

You can use Loom to record your screen and host the video then link to it from here.  If we hosted everyone's videos we'd run out of disk space very quickly.

regards

Phil

Avatar
Neda Sheikhi
Member
Members
Level 0
Forum Posts: 10
Member Since:
August 24, 2022
sp_UserOfflineSmall Offline
10
August 28, 2022 - 2:27 pm
sp_Permalink sp_Print

I do it.

in the code: Range("A" & Target.Row & ":Q" & Target.Row).Copy _

I use P instead of Q and solved!

Now, I want it:

for example, enter Q9 , then insert new row below and information copy and G10=G9-Q9,  now I want G9=Q9

how can do this?

Avatar
Catalin Bombea
Iasi, Romania
Admin
Level 10
Forum Posts: 1824
Member Since:
November 8, 2013
sp_UserOfflineSmall Offline
11
August 28, 2022 - 2:41 pm
sp_Permalink sp_Print

The formula below means G10=G9-Q9:

Cells(Target.Row, "G").Offset(1, 0).Formula = "=" & Cells(Target.Row, "G").Address(False, False) & "-" & Target.Address(False, False)

This one means G9=Q9:

Cells(Target.Row, "G").Formula = "=" & Target.Address(False, False)

Avatar
Neda Sheikhi
Member
Members
Level 0
Forum Posts: 10
Member Since:
August 24, 2022
sp_UserOfflineSmall Offline
12
August 28, 2022 - 2:53 pm
sp_Permalink sp_Print

I need both of them!

Q9 enter and G10=G9-Q9 then G9=Q9

Avatar
Neda Sheikhi
Member
Members
Level 0
Forum Posts: 10
Member Since:
August 24, 2022
sp_UserOfflineSmall Offline
13
August 28, 2022 - 3:49 pm
sp_Permalink sp_Print

I have another question too,

this excel is in the share and the sheet and the workbook is protected, so the code is not available?

Avatar
Catalin Bombea
Iasi, Romania
Admin
Level 10
Forum Posts: 1824
Member Since:
November 8, 2013
sp_UserOfflineSmall Offline
14
August 28, 2022 - 4:20 pm
sp_Permalink sp_Print

I need both of them!

then use both codes provided.

this excel is in the share and the sheet and the workbook is protected, so the code is not available?

Code works in Excel desktop, not in Excel Online.

Avatar
Neda Sheikhi
Member
Members
Level 0
Forum Posts: 10
Member Since:
August 24, 2022
sp_UserOfflineSmall Offline
15
August 28, 2022 - 4:36 pm
sp_Permalink sp_Print

So

Can I use Vba code for unprotect sheet and do something then protect sheet?

Is it possible?

Avatar
Catalin Bombea
Iasi, Romania
Admin
Level 10
Forum Posts: 1824
Member Since:
November 8, 2013
sp_UserOfflineSmall Offline
16
August 28, 2022 - 7:10 pm
sp_Permalink sp_Print

You can use Me.Unprotect at the beginning of the code, and Me.Protect at the end of code. (will be protected without a password)

But you have to make sure you have the target cells unlocked to allow changes in the cells that can trigger the code. If no cells are editable and unlocked, the code will not be triggered.

If you want to set a password:

Me.Unprotect Password:="MySecretPassword"

Me.Protect Password:="MySecretPassword"

Avatar
Neda Sheikhi
Member
Members
Level 0
Forum Posts: 10
Member Since:
August 24, 2022
sp_UserOfflineSmall Offline
17
August 28, 2022 - 7:49 pm
sp_Permalink sp_Print

thank you

but it can not be used by multiple users?
can I use code for not share workbook and do something then share it again?

sp_Feed
Go to top
Forum Timezone: Australia/Brisbane
Most Users Ever Online: 245
Currently Online:
Guest(s) 10
Currently Browsing this Page:
1 Guest(s)
Top Posters:
SunnyKow: 1432
Anders Sehlstedt: 873
Purfleet: 414
Frans Visser: 346
David_Ng: 306
lea cohen: 222
Jessica Stewart: 218
A.Maurizio: 202
Aye Mu: 201
jaryszek: 183
Newest Members:
Gilbert Lemek
Ashleigh Farquharson
Jayz Luu
Fred Smith
Charles DeGraffenreaid
Cathi Giard
Sarah Young
Henry Delgado
Alita Nieuwoudt
KL KOH
Forum Stats:
Groups: 3
Forums: 24
Topics: 6360
Posts: 27812

 

Member Stats:
Guest Posters: 49
Members: 32340
Moderators: 3
Admins: 4
Administrators: Mynda Treacy, Philip Treacy, Catalin Bombea, FT
Moderators: MOTH Support, Velouria, Riny van Eekelen
© Simple:Press —sp_Information

Sidebar

Blog Categories

  • Excel
  • Excel Charts
  • Excel Dashboard
  • Excel Formulas
  • Excel PivotTables
  • Excel Shortcuts
  • Excel VBA
  • General Tips
  • Online Training
  • Outlook
  • Power Apps
  • Power Automate
  • Power BI
  • Power Pivot
  • Power Query
microsoft mvp logo
trustpilot excellent rating
Secured by Sucuri Badge
MyOnlineTrainingHub on YouTube Mynda Treacy on Linked In Mynda Treacy on Instagram Mynda Treacy on Twitter Mynda Treacy on Pinterest MyOnlineTrainingHub on Facebook
 

Company

  • About My Online Training Hub
  • Disclosure Statement
  • Frequently Asked Questions
  • Guarantee
  • Privacy Policy
  • Terms & Conditions
  • Testimonials
  • Become an Affiliate

Support

  • Contact
  • Forum
  • Helpdesk - For Technical Issues

Copyright © 2023 · My Online Training Hub · All Rights Reserved. Microsoft and the Microsoft Office logo are trademarks or registered trademarks of Microsoft Corporation in the United States and/or other countries. Product names, logos, brands, and other trademarks featured or referred to within this website are the property of their respective trademark holders.