Link to home
Start Free TrialLog in
Avatar of RWayneH
RWayneHFlag for United States of America

asked on

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

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?
Avatar of Saqib Husain
Saqib Husain
Flag of Pakistan image

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
Avatar of Rgonzo1971
Rgonzo1971

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
Avatar of RWayneH

ASKER

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.
ASKER CERTIFIED SOLUTION
Avatar of Norie
Norie

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of RWayneH

ASKER

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.
Avatar of RWayneH

ASKER

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....
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

Avatar of RWayneH

ASKER

Worked!!  Thanks for the help.
Avatar of RWayneH

ASKER

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