Forum

Notifications
Clear all

Vba for logistic

17 Posts
3 Users
0 Reactions
234 Views
(@neda)
Posts: 10
Active Member
Topic starter
 

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

 
Posted : 25/08/2022 7:17 am
(@catalinb)
Posts: 1937
Member Admin
 

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

 
Posted : 25/08/2022 11:53 pm
(@neda)
Posts: 10
Active Member
Topic starter
 

ok

 
Posted : 28/08/2022 12:47 am
(@catalinb)
Posts: 1937
Member Admin
 

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

 
Posted : 28/08/2022 3:57 am
(@neda)
Posts: 10
Active Member
Topic starter
 

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

 
Posted : 28/08/2022 6:42 am
(@catalinb)
Posts: 1937
Member Admin
 

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.

 
Posted : 28/08/2022 10:34 am
(@neda)
Posts: 10
Active Member
Topic starter
 

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

 
Posted : 29/08/2022 12:08 am
(@neda)
Posts: 10
Active Member
Topic starter
 

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

 
Posted : 29/08/2022 12:12 am
Philip Treacy
(@philipt)
Posts: 1629
Member Admin
 

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

 
Posted : 29/08/2022 12:16 am
(@neda)
Posts: 10
Active Member
Topic starter
 

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?

 
Posted : 29/08/2022 12:27 am
(@catalinb)
Posts: 1937
Member Admin
 

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)

 
Posted : 29/08/2022 12:41 am
(@neda)
Posts: 10
Active Member
Topic starter
 

I need both of them!

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

 
Posted : 29/08/2022 12:53 am
(@neda)
Posts: 10
Active Member
Topic starter
 

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?

 
Posted : 29/08/2022 1:49 am
(@catalinb)
Posts: 1937
Member Admin
 

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.

 
Posted : 29/08/2022 2:20 am
(@neda)
Posts: 10
Active Member
Topic starter
 

So

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

Is it possible?

 
Posted : 29/08/2022 2:36 am
Page 1 / 2
Share: