ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

搜索
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 效率神器,一键搞定繁琐工作
HR薪酬管理数字化实战 Excel 2021函数公式学习大典 Excel数据透视表实战秘技 打造核心竞争力的职场宝典
让更多数据处理,一键完成 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南
查看: 301|回复: 1

[求助] 帮忙代码消除无用的部分

[复制链接]

TA的精华主题

TA的得分主题

发表于 2023-3-15 14:58 来自手机 | 显示全部楼层 |阅读模式
有一个分离文件,打开如图所示,想把显示框圈出来的部分删除,删除哪些代码不会影响使用?
另如果把行的值固定为2,列固定为A应该修改哪里?
Public continue As Boolean

Function GetColumn(mColumn)



  Select Case mColumn

    Case Is >= 53

      GetColumn = "B" & Chr(mColumn + 12)

    Case Is >= 26

      GetColumn = "A" & Chr(mColumn + 38)

    Case Else

      GetColumn = Chr(mColumn + 64)

  End Select



End Function

Sub Auto_Open()



  'Load forms

  Load Progress

  Load Separate

  Load Warning



  'Open Warning form

' If Range("H15").Value = False Then

   ' continue = False

   ' Do

      'Warning.Show

     ' Warning.Hide

     ' If continue = True Then Exit Do

   ' Loop

'  End If



  'Open form

  continue = False

  Do

    Separate.Show

    If continue = True Then Exit Do

    MsgBox "Insufficient data.", 16, "Warning!"

  Loop



  'Change forms

  Separate.Hide

  Progress.Show



  'Capture errors

'   On Error GoTo ErrorHandler



  'Turn application alerts off

  Application.DisplayAlerts = False

  Application.ScreenUpdating = False



  'Open new workbook (if necessary)

  If Separate.Email = True Then

    Workbooks.Add

    eFile = ActiveWorkbook.Name

    Range("A1").Select

    ActiveCell.FormulaR1C1 = "Date/Time Sent"

    Range("B1").Select

    ActiveCell.FormulaR1C1 = "Filename"

    Range("C1").Select

    ActiveCell.FormulaR1C1 = "E-mail Address"

    Range("D1").Select

    ActiveCell.FormulaR1C1 = "Subject:"

    Range("D2").Select

    ActiveCell.FormulaR1C1 = "Message:"

    Range("A1:C1").Select

    Selection.Font.Bold = True

    Selection.HorizontalAlignment = xlCenter

    Range("D1:D2").Select

    Selection.Font.Bold = True

    Selection.HorizontalAlignment = xlRight

  End If



  'Open filename

  Workbooks.Open Separate.Filename



  'Get current workbook name

  MyFile = ActiveWorkbook.Name 'Left(ActiveWorkbook.Name, Len(ActiveWorkbook.Name) - 4)



  'Get last row of data

  Range(Separate.sort & Separate.first).Select

  Selection.End(xlDown).Select

  lastRow = ActiveCell.Row



  'Get right most column

  Range("A" & IIf(Separate.first = 1, 1, Separate.first - 1)).Select

  Selection.End(xlToRight).Select

  rightColumn = GetColumn(ActiveCell.Column)

   

  'Initiate variable

  tfiles = 0



  'Loop through data

  eRow = 2

  start_row = Separate.first

  For ktr = Separate.first To lastRow + 1

   

    'Identify data

    sort_data = Trim(UCase(Range(Separate.sort & start_row).Value))

   

    'End of same data

    If sort_data <> Trim(UCase(Range(Separate.sort & ktr).Value)) Then

     

      'Copy and paste header

      new_file = Trim(Left(Range(Separate.sort & start_row).Value, 26)) & ".xls"

      Range("A1:" & rightColumn & IIf(Separate.first = 1, 1, Separate.first - 1)).Copy

      Workbooks.Add

      ActiveSheet.Paste

      NewFile = ActiveWorkbook.Name

      Windows(MyFile).Activate

     

      'Copy and paste data

      Range("A" & start_row, rightColumn & ktr - 1).Copy

      Windows(NewFile).Activate

      Range("A" & Separate.first).Select

      ActiveSheet.Paste

     

      'Bold header rows

      'Rows(Separate.first).Select

      Range("A1:" & rightColumn & IIf(Separate.first = 1, 1, Separate.first - 1)).Select

      Selection.Font.Bold = True

     

      'Auto fit

      Cells.Select

      Cells.EntireColumn.AutoFit

      Cells.EntireRow.AutoFit


      'Protect columns (if applicable)

      If Separate.password <> "" Then

        Cells.Select

        Selection.Locked = False

        Columns(Separate.protect_left & ":" & Separate.protect_right).Select

        Selection.Locked = True

        ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True, password:= _

         Separate.password

        ActiveSheet.EnableSelection = xlUnlockedCells

      End If

     

      'Save workbook

      Range("A" & Separate.first).Select

      ActiveWorkbook.SaveAs Filename:=new_file, FileFormat:= _

        xlNormal, password:="", WriteResPassword:="", _

        ReadOnlyRecommended:=False, CreateBackup:=False

     

      'Close workbook and return to original

      ActiveWorkbook.Close

      tfiles = tfiles + 1

      start_row = ktr

   

      'Paste filename in eFile

      If Separate.Email = True Then

        Windows(eFile).Activate

        Range("B" & eRow).Select

        ActiveCell.FormulaR1C1 = new_file

        eRow = eRow + 1

        Windows(MyFile).Activate

      End If

   

    End If

   

    'Update status

    Progress.Caption = "Progress (" & Int((ktr - Separate.first) / _

     (lastRow + 1 - Separate.first) * 100 + 0.999) & "%)"

    mWidth = (ktr - Separate.first) / (lastRow + 1 - Separate.first) * 192

    Progress.CommandButton1.Width = IIf(mWidth > 0.1, mWidth, 0.1)

    Progress.Repaint

      

  Next ktr



  'Format and close eFile

  If Separate.Email = True Then

    Windows(eFile).Activate

    Range("A2:A" & lastRow).Select

    Selection.NumberFormat = "mm/dd/yy hh:mm AM/PM"

    Columns("A:B").EntireColumn.AutoFit

    Columns("C:C").ColumnWidth = 30

    Columns("D:D").ColumnWidth = 9

    Columns("E:E").ColumnWidth = 40

    Range("E2:E11").Select

    Selection.MergeCells = True

    Selection.WrapText = True

    Selection.VerticalAlignment = xlTop

    Range("Z1").Select

    ActiveCell.FormulaR1C1 = "Yes"

    Range("Z2").Select

    ActiveCell.FormulaR1C1 = "No"

    Range("D12").Select

    With Selection.Validation

      .Delete

      .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _

       xlBetween, Formula1:="=$Z$1:$Z$2"

      .IgnoreBlank = True

      .InCellDropdown = True

      .InputTitle = ""

      .ErrorTitle = ""

      .InputMessage = "Filename within subject"

      .ErrorMessage = "Yes or No"

      .ShowInput = True

      .ShowError = True

    End With

    ActiveCell.FormulaR1C1 = "No"

    Selection.HorizontalAlignment = xlCenter

    Range("E1").Select

    Selection.HorizontalAlignment = xlLeft

    Range("E12").Select

    ActiveCell.FormulaR1C1 = "<---- Filename within subject"

    Selection.Font.Bold = True

    Range("D13").Select

    ActiveCell.FormulaR1C1 = "bcc:"

    Selection.Font.Bold = True

    Selection.HorizontalAlignment = xlRight

   

    'Start VEN 06-NOV-09

    Range("D14").Select

    ActiveCell.FormulaR1C1 = "Cc:"

    Selection.Font.Bold = True

    Selection.HorizontalAlignment = xlRight

    'End Ven 06-NOV-09

   

    Range("A2").Select

    ActiveWorkbook.SaveAs Filename:="File list for e-mailing.xls"

    ActiveWorkbook.Close

  End If



  'Flash message

  MsgBox "Files separated:  " & tfiles & IIf(Separate.Email = True, _

   Chr(13) & Chr(13) & "E-mail list saved!", ""), vbExclamation, "Macro Complete!"



  'Close macro

  ActiveWorkbook.Close

  Unload Progress

  Range("H15").Select

  ActiveCell.FormulaR1C1 = Warning.showWarning

  ActiveWorkbook.Save

  ActiveWorkbook.Close

  Application.DisplayAlerts = True

  Application.ScreenUpdating = True

  Exit Sub



ErrorHandler:



  'Activate original spreadsheet

  Windows(MyFile).Activate



  'Splash warning

  bCell = UCase(Separate.sort) & start_row

  cCell = UCase(Separate.sort) & ktr - 1

  IRange = IIf(bCell = cCell, bCell, bCell & ":" & cCell)

  Unload Separate

  If Err.Number = 1004 Then

    MsgBox "The file will not save because there is" & _

     Chr(13) & "an illegal character in " & IIf(Len(IRange) _

     <= 4, "cell " & IRange, "the range " & IRange) & "." & _

     Chr(13) & "Please correct the problem before" & _

     Chr(13) & "proceeding.", vbCritical, "WARNING!"

  Else

    Dim Msg

    Msg = "Error # " & Str(Err.Number) & " was generated by " _

            & Err.Source & Chr(13) & Err.Description

    MsgBox Msg, , "Error", Err.HelpFile, Err.HelpContext



  End If



  'Close macro

  Unload Progress

  ActiveWorkbook.Close

  ActiveWorkbook.Close

  Range("H15").Select

  ActiveCell.FormulaR1C1 = Warning.showWarning

  ActiveWorkbook.Save

  ActiveWorkbook.Close

  Application.DisplayAlerts = True

  Application.ScreenUpdating = True



End Sub
IMG_20230315_145309.jpg

TA的精华主题

TA的得分主题

 楼主| 发表于 2023-3-15 15:03 来自手机 | 显示全部楼层
因为圈出来的部分看不懂不会用,所以想把界面修改简单点
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

手机版|关于我们|联系我们|ExcelHome

GMT+8, 2024-11-18 11:17 , Processed in 0.029411 second(s), 11 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

沪公网安备 31011702000001号 沪ICP备11019229号-2

本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任!     本站特聘法律顾问:李志群律师

快速回复 返回顶部 返回列表