简体   繁体   中英

VB excel macro code to remove colum data in sheet and insert to other sheet and list empty column

I am having issues with my code. I have an excel sheet a that has 7 columns. I want to copy and add first 5 columns of the selected/highlighted row and add them to sheet b 's very first empty row and deleting the remaining 2 columns all while pressing update button:

Sub Macro1()
'
' Macro1 Macro
'

'
    Range("F:G").Select
    Range("A:E").Select
    Selection.Cut
    Sheets("Instock").Select
    Range("A280").Select
    ActiveSheet.Paste
    Cells.Select
    Range("A256").Activate
    ActiveWorkbook.Worksheets("Instock").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Instock").Sort.SortFields.Add Key:=Range( _
        "B2:B4096"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortNormal
    ActiveWorkbook.Worksheets("Instock").Sort.SortFields.Add Key:=Range( _
        "A2:A4096"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortNormal
    With ActiveWorkbook.Worksheets("Instock").Sort
        .SetRange Range("A1:G4096")
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    Sheets("Checked Out").Select
    Rows("3:3").Select
    Selection.Delete Shift:=xlUp
    Range("H17").Select
End Sub

From what I gather you have 2 worksheets

  • Instock
  • Checked Out

When you press update you want to:

  • Add the first 5 column values of the selected row from [Checked Out] to [Instock]
  • Clear the values in column 6 and 7 of the selected row from [Checked Out]
  • Sort [Instock]

Try this:

Public Sub MoveStockItems()
    If Not (ActiveSheet.Name = "Checked Out") Then Exit Sub
    Dim newRow As Long, rw As Long

    rw = ActiveCell.Row
    With Sheets("Instock")
        newRow = .Cells(.Rows.Count, 1).End(xlUp).Row + 1
        .Cells(newRow, 1) = Cells(rw, 1)
        .Cells(newRow, 2) = Cells(rw, 2)
        .Cells(newRow, 3) = Cells(rw, 3)
        .Cells(newRow, 4) = Cells(rw, 4)
        .Cells(newRow, 5) = Cells(rw, 5)
        Cells(rw, 6) = ""
        Cells(rw, 7) = ""
    End With
    Call SortInstock
End Sub

Public Sub SortInstock()
    Dim rowCount As Long
    With Worksheets("Instock")
         rowCount = .UsedRange.Rows.Count
        .Sort.SortFields.Clear
        .Sort.SortFields.Add Key:=.Range("B2:B" & rowCount), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        .Sort.SortFields.Add Key:=.Range("A2:A" & rowCount), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    End With

    With Worksheets("Instock").Sort
        .SetRange Worksheets("Instock").UsedRange
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
End Sub

Paste this code into a code module then assign a shortcut key to MoveStockItems.

Select a cell on [Checked Out] press the shortcut k to run the macro. 在[已检出]上选择一个单元,然后按快捷方式k运行宏。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM