Solved

Public Sub, Empty Cell

Posted on 2016-07-18
7
57 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
  • 3
  • 2
7 Comments
 
LVL 68

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 68

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

758 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now