Making packing list in VBA

mehidy1437

Active Member
Joined
Nov 15, 2019
Messages
348
Office Version
  1. 365
  2. 2016
  3. 2013
Platform
  1. Windows
  2. Mobile
  3. Web
Hi There,

I have data in Sheet1 as below.

If the cell value is greater than 40, then I want to divide the cell by 40, put balance value beneath(if there is any balance), also I want to put the number of division done in column T.
How can I do this in Excel VBA?

My desire output in Sheet2 as below.

Sheet1
2233.xlsm
HIJKLMN
3STYLECOLORORD NOSMLXL
4MHT01WHITE111100300100100
5MHT01RED11110051305
6MHT01BLUE112120111
7MHT01BLACK112111130
Sheet1




Sheet2
2233.xlsm
EFGHIJKLMNOPQRSTU
3STYLECOLORORD NOSMLXLQTY/CTNCTNTTL
4MHT01WHITE1114040280
5MHT01WHITE1112020120
6MHT01WHITE11140407280
7MHT01WHITE1112020120
8MHT01WHITE1114040280
9MHT01WHITE1112020120
10MHT01WHITE1114040280
11MHT01WHITE1112020120
12MHT01RED1114040280
13MHT01RED1112020120
14MHT01RED11140403120
15MHT01RED1111010110
16MHT01RED1115510110
17MHT01BLUE11240403120
18MHT01BLUE112111313
19MHT01BLACK11240403120
20MHT01BLACK1121010110
21MHT01BLACK112111313
Sheet2
Cell Formulas
RangeFormula
S4:S21S4=SUM(H4:K4)
U4:U21U4=SUM(H4:K4)*T4
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
So far, I have done this much.

VBA Code:
Option Explicit

Sub CreatPackingList()
    Dim Rng As Range
    Dim WorkRng As Range
    Dim xNum As Integer
    Dim nThNumber As Double
    Dim nThNumberNoDecimil As Single
    Dim nThNumberNoDecInt As Integer
    Dim coPyRowNThTime As Integer
    Dim xTitleId As String
    Dim totalValue As Integer
    Dim balanceValue As Integer
    Dim cUrrentCellCol As Integer
    Dim cUrrentCellRow As Integer
    Dim leftColNumber As Integer
    Dim rightColNumber As Integer

On Error Resume Next
xTitleId = "Input box--"
Set WorkRng = Application.Selection
Set WorkRng = Application.InputBox("Range", xTitleId, WorkRng.Address, Type:=8)
xNum = Application.InputBox("Division num", xTitleId, Type:=1)

For Each Rng In WorkRng
If Rng.Value > xNum Then
    nThNumber = Rng.Value / xNum
    nThNumberNoDecimil = nThNumber
    'to remove decimil data
    nThNumberNoDecInt = CInt(Fix(nThNumberNoDecimil))
    totalValue = nThNumberNoDecInt * xNum
    balanceValue = Rng.Value - totalValue
    Rng.Value = xNum
    Rng.EntireRow.Copy
    cUrrentCellCol = Range(Rng.Offset(0, 0), Rng.Offset(0, 0)).Column
    cUrrentCellRow = Range(Rng.Offset(0, 0), Rng.Offset(0, 0)).Row


    'coPyRowNThTime
    '----------------------
        If balanceValue > 0 Then
           coPyRowNThTime = 2
                Range(Rng.Offset(1, 0), Rng.Offset(coPyRowNThTime, 0)).EntireRow.Insert Shift:=xlDown
                Range(Rng.Offset(1, 0), Rng.Offset(1, 0)).Value = balanceValue
                Range(Rng.Offset(2, 0), Rng.Offset(2, 0)).ClearContents
                'Ctn no at column T
                Cells(cUrrentCellRow, 20).Value = nThNumberNoDecInt
                Cells(cUrrentCellRow, 20).Offset(1, 0).Value = 1

                        If cUrrentCellCol = 8 Then
                        
                        Range(Cells(cUrrentCellRow, 9), Cells(cUrrentCellRow, 19)).ClearContents
                        Range(Cells(cUrrentCellRow, 9).Offset(1, 0), Cells(cUrrentCellRow, 19).Offset(1, 0)).ClearContents
                          ElseIf cUrrentCellCol = 19 Then
                        Range(Cells(cUrrentCellRow, 8), Cells(cUrrentCellRow, 18)).ClearContents
                        Range(Cells(cUrrentCellRow, 8).Offset(1, 0), Cells(cUrrentCellRow, 18).Offset(1, 0)).ClearContents
                            
                        Else
                            leftColNumber = cUrrentCellCol - 1
                            rightColNumber = cUrrentCellCol + 1
                            
                        Range(Cells(cUrrentCellRow, 8), Cells(cUrrentCellRow, leftColNumber)).ClearContents
                        Range(Cells(cUrrentCellRow, 8).Offset(1, 0), Cells(cUrrentCellRow, leftColNumber).Offset(1, 0)).ClearContents
                        Range(Cells(cUrrentCellRow, rightColNumber), Cells(cUrrentCellRow, 19)).ClearContents
                        Range(Cells(cUrrentCellRow, rightColNumber).Offset(1, 0), Cells(cUrrentCellRow, 19).Offset(1, 0)).ClearContents
                                  
                        End If
                        'delete emptye row in H-S column, if qty is nothing
                         If WorksheetFunction.CountA(Range(Cells(cUrrentCellRow, 8).Offset(2, 0), Cells(cUrrentCellRow, 19).Offset(2, 0))) = 0 Then
                         Range(Cells(cUrrentCellRow, 8).Offset(2, 0), Cells(cUrrentCellRow, 18).Offset(2, 0)).EntireRow.Delete
                         End If

        Else
           coPyRowNThTime = 1
                    Range(Rng.Offset(1, 0), Rng.Offset(coPyRowNThTime, 0)).EntireRow.Insert Shift:=xlDown
                    Range(Rng.Offset(1, 0), Rng.Offset(1, 0)).ClearContents
                    
                    'Ctn no at column T
                    Cells(cUrrentCellRow, 20).Value = nThNumberNoDecInt
                            If cUrrentCellCol = 8 Then
                            Range(Cells(cUrrentCellRow, 9), Cells(cUrrentCellRow, 19)).ClearContents
                            
                            ElseIf cUrrentCellCol = 19 Then
                            Range(Cells(cUrrentCellRow, 8), Cells(cUrrentCellRow, 18)).ClearContents
                                
                            Else
                            leftColNumber = cUrrentCellCol - 1
                            rightColNumber = cUrrentCellCol + 1
                            Range(Cells(cUrrentCellRow, 8), Cells(cUrrentCellRow, leftColNumber)).ClearContents
                            Range(Cells(cUrrentCellRow, rightColNumber), Cells(cUrrentCellRow, 19)).ClearContents
    
                           End If
                        'delete emptye row in H-S column, if qty is nothing
                         If WorksheetFunction.CountA(Range(Cells(cUrrentCellRow, 8).Offset(1, 0), Cells(cUrrentCellRow, 19).Offset(1, 0))) = 0 Then
                         Range(Cells(cUrrentCellRow, 8).Offset(1, 0), Cells(cUrrentCellRow, 18).Offset(1, 0)).EntireRow.Delete
                         End If
            
        End If
    '---------------------
    
 End If
Next

End Sub
 
Upvote 0
Hi Guys,

I'm having two issues with the above code.
01. If the range is containing only one row, like H12-K12 then it's only run for first cell I12 & then stop. But it should run till K15.
02. If the range is containing multiple rows as H12-K15, then it's run till I15 & after that, it stops. But it should run till K15.

What am I doing wrong?

Data.....
2233.xlsm
EFGHIJK
11STYLECOLORORD NOSMLXL
12MHT01WHITE111300100100
13MHT01RED1115130
14MHT01BLUE1121201
15MHT01BLACK1121201130
Sheet1


Output for RANGE H12 - S12

2233.xlsm
EFGHIJKLMNOPQRST
11STYLECOLORORD NOSMLXLCTN
12MHT01WHITE111506
13MHT01WHITE111100100
14MHT01RED1115130
15MHT01BLUE1121201
16MHT01BLACK1121201130
Sheet1


Output for RANGE H12 - S15

2233.xlsm
EFGHIJKLMNOPQRST
11STYLECOLORORD NOSMLXLCTN
12MHT01WHITE111506
13MHT01WHITE111502
14MHT01WHITE111502
15MHT01RED111502
16MHT01RED111301
17MHT01RED1115
18MHT01BLUE112502
19MHT01BLUE112201
20MHT01BLUE1121
21MHT01BLACK112502
22MHT01BLACK112201
23MHT01BLACK1121130
Sheet1
 
Upvote 0

Forum statistics

Threads
1,214,991
Messages
6,122,628
Members
449,095
Latest member
bsb1122

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