Solved

Public Sub, Empty Cell

Posted on 2016-07-18
7
76 Views
Last Modified: 2016-07-19
I have many textboxes to change data.  the first Sub below was working, but with a few hundred textboxes this code slowed down the program.  So I made a public sum, as seen below, but i kept getting the value of c = "Empty"  

what am i doing wrong?

Private Sub TextBox1_Change()
   
     If wsMenu.Cells(12, 3).Value = "081" Then wsRoll.Cells(2, 7) = TextBox1.Value
     If wsMenu.Cells(12, 3).Value = "082" Then wsRoll.Cells(494, 7) = TextBox1.Value
     If wsMenu.Cells(12, 3).Value = "083" Then wsRoll.Cells(986, 7) = TextBox1.Value
     If wsMenu.Cells(12, 3).Value = "084" Then wsRoll.Cells(1478, 7) = TextBox1.Value
     If wsMenu.Cells(12, 3).Value = "085" Then wsRoll.Cells(1970, 7) = TextBox1.Value
     If wsMenu.Cells(12, 3).Value = "086" Then wsRoll.Cells(2462, 7) = TextBox1.Value
     If wsMenu.Cells(12, 3).Value = "087" Then wsRoll.Cells(2954, 7) = TextBox1.Value
     If wsMenu.Cells(12, 3).Value = "099" Then wsRoll.Cells(3446, 7) = TextBox1.Value
    
End Sub

Open in new window

_____________________________________________


Public Sub CellLocation()

    Dim c As Integer
    
     If wsMenu.Cells(12, 3).Value = "081" Then c = 2
     If wsMenu.Cells(12, 3).Value = "082" Then c = 494
     If wsMenu.Cells(12, 3).Value = "083" Then c = 986
     If wsMenu.Cells(12, 3).Value = "084" Then c = 1478
     If wsMenu.Cells(12, 3).Value = "085" Then c = 1970
     If wsMenu.Cells(12, 3).Value = "086" Then c = 2462
     If wsMenu.Cells(12, 3).Value = "087" Then c = 2954
     If wsMenu.Cells(12, 3).Value = "099" Then c = 3446

End Sub

Open in new window

Private Sub TextBox1_Change()
     CellLocation
     wsRoll.Cells(c, 7).Value = TextBox1.Value
UpdateSheet
End Sub

Open in new window

0
Comment
Question by:AaronBanker
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
7 Comments
 
LVL 70

Accepted Solution

by:
Qlemo earned 500 total points
ID: 41717588
You should use Select Case in your sub ;-).

The reason it does not work that way is because you define c inside of the sub, that makes it local to it, and dismissed after the sub has been called. Writing CellLocation as a function is a better idea:
Public Function CellLocation() As Integer
  Select Case wsMenu.Cells(12, 3).Value
    Case "081": CellLocation = 2
    Case "082": CellLocation = 494
    Case "083": CellLocation = 986
    Case "084": CellLocation = 1478
    Case "085": CellLocation = 1970
    Case "086": CellLocation = 2462
    Case "087": CellLocation = 2954
    Case "099": CellLocation = 3446
  End Select
End Function

Private Sub TextBox1_Change()
  wsRoll.Cells(CellLocation(), 7).Value = TextBox1.Value
  UpdateSheet
End Sub

Open in new window

0
 

Author Comment

by:AaronBanker
ID: 41719221
GrahamSkan:  I have no idea what "Code snippets in the question are now in code boxes." means
0
 

Author Comment

by:AaronBanker
ID: 41719222
Qlemo:

would the next textbox be


Private Sub TextBox2_Change()
  wsRoll.Cells(CellLocation()+1, 7).Value = TextBox1.Value
  UpdateSheet
End Sub

Private Sub TextBox2_Change()
  wsRoll.Cells(CellLocation()+2, 7).Value = TextBox1.Value
  UpdateSheet
End Sub
0
 
LVL 70

Expert Comment

by:Qlemo
ID: 41719250
I cannot tell whether your code is correct for what you are after, but syntactically it would work that way - if you name the subs correctly (the last sub needs to be TextBox3_Change()). And I suppose you want to change Textbox1.value to the respective one.
0
 

Author Comment

by:AaronBanker
ID: 41719646
this doesn't seem to work
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question