Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Reading in cell values as long as they is something there and naming it.

Posted on 2014-04-22
9
Medium Priority
?
233 Views
Last Modified: 2014-04-22
Starting at cell B1, there is a value there.  I would like to read that value and name it for future use.  Move one cell to the right, check it for a value, if one is there read the value, name it, and move to the right as long as there is a value.

Example: B1 is 123_456, name that Finish1,  cell C1 is 456_789, name that Finish2 and so on until it finds a blank cell.

I need to use Finish1, Finish2 .... in a different procedure, but wanted to read all the values at once and name them.

How would I do this is VBA?
0
Comment
Question by:RWayneH
[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
9 Comments
 
LVL 43

Expert Comment

by:Saqib Husain, Syed
ID: 40015470
Try

Sub Macro1()
Dim cel As Range
For Each cel In Range(Range("B1"), Range("B1").End(xlToRight))
    ActiveWorkbook.Names.Add Name:="Finish" & cel.Column - 1, RefersToR1C1:="=Sheet1!" & cel.Address(, , xlR1C1)
Next cel
End Sub
0
 
LVL 52

Expert Comment

by:Rgonzo1971
ID: 40015506
Hi,

pls try this with trailing zeros


Sub Macro()

cellCount = Range(Range("B1"), Range("B1").End(xlToRight)).Count
Idx = 1
For Each cell In Range(Range("B1"), Range("B1").End(xlToRight))
    ActiveWorkbook.Names.Add Name:="Finish" & Format(Idx, WorksheetFunction.Rept("0", Len(cellCount))), _
    RefersTo:="=" & ActiveSheet.Name & "!" & cell.Address
    Idx = Idx + 1
Next
End Sub

Open in new window

Regards
0
 

Author Comment

by:RWayneH
ID: 40015783
Not sure how to words this.

Is there a way the include a spot for my procedure that I need to do on each Finish?  Perhaps I do not need to name each one?  I need to gather some data on each one of the Finishx values.  And at the end delete the name/s from the Name Manager.  I need the name stored in a cell or in a name to grab it, but I also need to get back to it and check the next one.  

Or perhaps a For Selection loop might work better?  Does a For Selection work on a horizontal range?  Range starts at B1, but I need to know what name was assigned to the selection when reading it, so I can write that name out when gather data on it.   Hope this makes sense.
0
Industry Leaders: 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!

 
LVL 34

Accepted Solution

by:
Norie earned 2000 total points
ID: 40015786
If you can afford to insert a row temporarily at the top of the sheet you could try this.
Sub AddNames()
Dim rng As Range

    Set rng = Range("B1", Cells(1, Columns.Count).End(xlToLeft))
    
    Rows(1).Insert
    
    With rng
    
        .Offset(-1).Cells(1, 1).Value = "Finish1"

        .Offset(-1).Cells(1, 1).AutoFill .Offset(-1)

        .Offset(-1).Resize(2).CreateNames Top:=True, Left:=False, Bottom:=False, Right:=False
        
    End With
    
    Rows(1).Delete

End Sub

Open in new window


By the way, have you considered naming the entire range 'Finish'?

Then you could refer to the individual cells using Range("Finish")(x).Value.

For example for C1 you would use Range("Finish")(2).Value

You could also loop through the range like this.
For Each cl in Range("Finish").Columns
    ' do stuff with value in cl
Next cl

Open in new window

0
 

Author Comment

by:RWayneH
ID: 40015808
I am just not sure how to write out to a cell the value the For loop is on, so I can use to again when grabbing the data I need for it.
0
 

Author Comment

by:RWayneH
ID: 40015824
to grab a value and bring it into SAP I was grabbing an absolute cell value.

session.findById("wnd[0]/usr/ctxtS_MBDAT-LOW").Text = Worksheets("ZSHORTV2DataPrevious").Cells(2, 8)

row 2 column 8 of sheet ZSHORTV2....
0
 
LVL 34

Expert Comment

by:Norie
ID: 40015841
To get the value of the cell in the code I posted all you need is cl.Value.

For example if you wanted to replace the hard-coded value in the code you just posted.
session.findById("wnd[0]/usr/ctxtS_MBDAT-LOW").Text = cl.Value

Open in new window

0
 

Author Closing Comment

by:RWayneH
ID: 40016020
Worked!!  Thanks for the help.
0
 

Author Comment

by:RWayneH
ID: 40016086
I can write the value to SAP and it is taking it, however how come I can not work cel.Value to another part of the worksheet?  How is what I ended up with.

Dim cel As Range
'For each parsed off finish code, goto the finish db and gather data
For Each cel In Range(Range("B1"), Range("B1").End(xlToRight))
'    ActiveWorkbook.Names.Add Name:="Finish" & cel.Column - 1, RefersToR1C1:="=CatcodeFinishCodes!" & cel.Address(, , xlR1C1)
    
session.findById("wnd[0]/tbar[0]/btn[3]").press
session.findById("wnd[0]/tbar[0]/btn[3]").press 'send SAP session back to main menu
session.findById("wnd[0]/tbar[0]/okcd").Text = "/nZGMEG017_FMAT"  'finish db tcode
session.findById("wnd[0]").sendVKey 0  'Enter
session.findById("wnd[0]/usr/ctxtSCREEN_DATA-FCODE").Text = cel.Value
session.findById("wnd[0]").sendVKey 0  'Enter

Range("A3").Select
ActiveCell.FormulaR1C1 = cel.Value  'Input FINISH_CODE that the For loop is working on.


'now you are in the finish code in the finish db
'writer the gather CH_ info and data sub
    
    
    
Next cel

Open in new window

0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
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 Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

688 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