Excel/VBA Code

Advertisement


Question
Tom

I have figured out pretty much everything, even the listbox and combo box issues...

my final issue, and you won't (shouldnt hear from me for awhile) is the code below which works great, i modified it to work with what needed to be done and now all is good even with the macros...


THE ONLY issue is that if we save the file as a different name or if a client does which can and will happen more than likely, is it possible to have it name the workbook as the filename of the workbook, whether its the active one or if it gets saved as say ES 800 Demo, can we make it so it renames the workbook to that given name that it is changed to - without having to go back into it every time and change that macro code?

this is the code...

Sub SaveRecord()
Application.ScreenUpdating = False
  Dim sh2 As Worksheet, sh1 As Worksheet, sh3 As Worksheet, sh4 As Worksheet, sh5 As Worksheet
    Dim bk1 As Workbook
    Dim r1 As Range, r2 As Range
    Dim NextRow As Long
   Cancel = True
    ' source workbook
    Set bk1 = Workbooks("ES 800 v11.00.xlsm")
    bk1.Activate
    Set sh1 = bk1.Worksheets("customers sold")
    Set r1 = sh1.Range("B2:N2")
    ' Target workbook
    Set sh2 = bk1.Worksheets("Sold Clients")
    Set r2 = sh2.Range("c4:o4")
    sh2.Unprotect
    NextRow = sh2.Range("c65536").End(xlUp).Row + 1
    If NextRow < 3 Then NextRow = 3
    r1.Copy
    sh2.Cells(NextRow, "C").PasteSpecial xlValues
    'r2.PasteSpecial xlValues  <== this line turned off; now a comment
    sh2.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
    
    
    Set sh1 = bk1.Worksheets("customers sold")
    Set r1 = sh1.Range("q2:ad2")
    ' Target workbook
    Set sh3 = bk1.Worksheets("Sold Inventory")
    Set r2 = sh2.Range("c5:p5")
    sh3.Unprotect
    NextRow = sh3.Range("c65536").End(xlUp).Row + 1
    If NextRow < 3 Then NextRow = 3
    r1.Copy
    sh3.Cells(NextRow, "C").PasteSpecial xlValues
    'r2.PasteSpecial xlValues  <== this line turned off; now a comment
    sh3.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
    
    
    Set sh4 = bk1.Worksheets("Database Index")
    Set r1 = sh4.Range("c2:cv2")
    ' Target workbook
    Set sh4 = bk1.Worksheets("Database Index")
    Set r2 = sh4.Range("c6:cv6")
    sh4.Unprotect
    NextRow = sh4.Range("c65536").End(xlUp).Row + 1
    If NextRow < 3 Then NextRow = 3
    r1.Copy
    sh4.Cells(NextRow, "C").PasteSpecial xlValues
    'r2.PasteSpecial xlValues  <== this line turned off; now a comment
    sh4.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
    

    Set sh5 = bk1.Worksheets("Inventory Index")
    Set r1 = sh5.Range("c2:ba2")
    ' Target workbook
    Set sh5 = bk1.Worksheets("Inventory Index")
    Set r2 = sh5.Range("c7:ba7")
    sh5.Unprotect
    NextRow = sh5.Range("c65536").End(xlUp).Row + 1
    If NextRow < 3 Then NextRow = 3
    r1.Copy
    sh5.Cells(NextRow, "C").PasteSpecial xlValues
    'r2.PasteSpecial xlValues  <== this line turned off; now a comment
    sh5.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
    Application.ScreenUpdating = True
End Sub

again, like alway Tom, thanks ahead of time

Hope i made sense of that above....
Tony

Answer
Tony,

if the code is doing its work in the same workbook that contains the code (and it sounds like you are saying that) then you can change



Set bk1 = Workbooks("ES 800 v11.00.xlsm")

to

Set bk1 = ThisWorkbook

Then you won't need to know the name of the workbook.

--
Regards,
Tom Ogilvy  
About Excel
This topic answers questions related to Microsoft Excel spreadsheet (or workbook) stand-alone or Mircrosoft Office Excel including Excel 2003, Excel 2007, Office 2000, and Office XP. You can get Excel help on Excel formulas(or functions), Excell macros, charting in Excel, advanced features, and the general use of Excel. This does not provide a general Excel tutorial nor the basics of using a spreadsheet. It provides specific answers to using Microsoft Excel only. If you do not see your Excel question answered in this area then please ask an Excel question here

Excel

All Answers


Answers by Expert:


Ask Experts

Volunteer


Tom Ogilvy

Expertise

Selected as an Excel MVP by Microsoft since 1999. Answering Excel questions in Allexperts since its inception in 2001. Able to answer questions on almost all aspects of Excel's internal capabilities. If seeking a VBA solution, please specify that in your question itself so I give you the answer you want. [Excel has weak protection - if you are distributing an application, I don't answer questions on how to protect your project from your users.]

Experience

Extensive experience.

Education/Credentials
Master of Science (MS) degree Operations Research (ORSA)

Awards and Honors
Microsoft MVP in Excel.

©2016 About.com. All rights reserved.