Solved

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

Posted on 2014-04-22
9
224 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
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 48

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
 
LVL 33

Accepted Solution

by:
Norie earned 500 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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

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 33

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Drop Down List with Unique/Distinct Values (enhancing the Combo-Box with a few steps and a little code) David miller (dlmille) Intro Have you ever created a data validation list from a database field or spreadsheet column (e.g., Zip Codes or Co…
How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

762 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