Solved

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

Posted on 2014-04-22
9
225 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 49

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Vb.net dynamic formulas in runtime 11 63
Filling Blank Cells 14 21
Pivot help - Display only Is Not Null 7 17
Issues with DAX Calculated Columns 6 14
Convert between Excel file formats (.XLS, .XLSX, .XLSM) with/without macro option David Miller (dlmille) Intro Over this past Fall, I've had the opportunity to see several similar requests and have developed a couple related solutions associate…
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.
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…

862 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

25 Experts available now in Live!

Get 1:1 Help Now