Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 239
  • Last Modified:

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?
0
RWayneH
Asked:
RWayneH
1 Solution
 
Saqib Husain, SyedEngineerCommented:
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
 
Rgonzo1971Commented:
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
 
RWayneHAuthor Commented:
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
NorieData ProcessorCommented:
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
 
RWayneHAuthor Commented:
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
 
RWayneHAuthor Commented:
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
 
NorieData ProcessorCommented:
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
 
RWayneHAuthor Commented:
Worked!!  Thanks for the help.
0
 
RWayneHAuthor Commented:
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

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now