简体   繁体   English

单击带有宏的按钮时Excel崩溃

[英]Excel Crashing when button with macro attached clicked

I have a macro that is attached to a button. 我有一个附加到按钮的宏。 However, when I test it in the VBA Editor, it runs fine. 但是,当我在VBA编辑器中对其进行测试时,它运行良好。 When the button is clicked with the macro attached it crashes excel. 单击带有宏的按钮时,Excel崩溃。 I have other macros attached which run fine. 我附有其他运行良好的宏。

Here is my code: 这是我的代码:

Option Explicit

Sub rma_new()

Dim Sno As Long, LRow As Long, NewRow As Long
Dim wsO As Worksheet, wsI As Worksheet
Dim customeRef, customerName, customerCountry
Dim customerCompany, datePaid, dateShipped
Dim webInvoiceNumber, invoiceNumber, postCode
Dim assignedTo, salesChnl, orderValue

Set wsO = ThisWorkbook.Sheets("RMA")
Set wsI = ThisWorkbook.Sheets("Sheet 1")

'~~> Get values from Sheet1
With wsI
    customeRef = .Range("c" & (ActiveCell.Row))
    customerName = .Range("d" & (ActiveCell.Row))
    customerCountry = .Range("e" & (ActiveCell.Row))
    customerCompany = .Range("f" & (ActiveCell.Row))
    datePaid = .Range("g" & (ActiveCell.Row))
    dateShipped = .Range("h" & (ActiveCell.Row))
    webInvoiceNumber = .Range("i" & (ActiveCell.Row))
    invoiceNumber = .Range("j" & (ActiveCell.Row))
    postCode = .Range("k" & (ActiveCell.Row))
    salesChnl = .Range("o" & (ActiveCell.Row))
    orderValue = .Range("a" & (ActiveCell.Row))


End With

'~~> Work with RMA Sheet
With wsO
    '~~> Get the last Row
    LRow = .Range("A" & .Rows.Count).End(xlUp).Row
    '~~> Increment the number
    Sno = .Range("A" & LRow).Value + 1
    '~~> New row where we need to write
    NewRow = LRow + 1

    .Cells(NewRow, 1) = Sno
    .Cells(NewRow, 2) = customeRef
    .Cells(NewRow, 3) = customerName
    .Cells(NewRow, 4) = customerCountry
    .Cells(NewRow, 5) = customerCompany
    .Cells(NewRow, 6) = datePaid
    .Cells(NewRow, 7) = dateShipped
    .Cells(NewRow, 8) = webInvoiceNumber
    .Cells(NewRow, 9) = invoiceNumber
    .Cells(NewRow, 10) = postCode
    .Cells(NewRow, 12) = salesChnl
    .Cells(NewRow, 14) = orderValue

End With

End Sub

I wrote this code and now my excel doesn't crash! 我写了这段代码,现在我的excel不会崩溃! I hope it helps. 希望对您有所帮助。

Sub rma_New ()

    Dim LRow As Long, Sno As Long
    Dim myData As Workbook

    '--Get data from current sheet--
    ActiveWorkbook.ActiveSheet.Select
    orderValue = Range("a" & (ActiveCell.Row))
    customerRef = Range("c" & (ActiveCell.Row))
    customerName = Range("d" & (ActiveCell.Row))
    customerCountry = Range("e" & (ActiveCell.Row))

    '--Open Sheet 2--
    Worksheets("Sheet 2").Select

    '--Get value of previous cell in the row above in column A and add 1, inside Sheet 2--
    LRow = Range("A" & Rows.Count).End(xlUp).Row
    Sno = Range("A" & LRow).Value + 1

    '--Find the next empty row and append a new line with the data in the corresponding cells--
    Worksheets("Sheet 2").Range("a8").Select
    RowCount = Worksheets("Sheet 2").Range("a8").CurrentRegion.Rows.Count
    With Worksheets("Sheet 2").Range("a8")

  .Offset(RowCount, 0) = Sno
  .Offset(RowCount, 1) = customerRef
  .Offset(RowCount, 2) = customerName
  .Offset(RowCount, 3) = customerCountry

  End With

End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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