Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations waross on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Return value of cell next to empty cell

Status
Not open for further replies.

jkate

Mechanical
Nov 5, 2011
66
0
0
US
I need some help with VBA code to look at a two column array (columns A&B) called POLog, find the last empty cell in cell Bx (with "x" being the row) and return the value that's in cell Ax. I then want the value from Ax to be placed in a cell called "PONumber". Can someone help me with that?

Thanks.
 
Replies continue below

Recommended for you

Range.Rows.Count - Range.Row gives you the row number of the last row of the range.

Loop backwards through rows in column B until you find an "empty".

Get the value from the corresponding cell in column A and set that as the value of "PONumber"
 
Hi,

Assuming a Range Name of PONumber...
Code:
'
   [PONumber].Value = [B1].End(xlDown).Offset(1, -1).Value

Skip,
[sub]
[glasses]Just traded in my OLD subtlety...
for a NUance![tongue][/sub]
 
Code:
'
   [PONumber].Value = Worksheets("Your sheet name").[B1].End(xlDown).Offset(1, -1).Value

Skip,
[sub]
[glasses]Just traded in my OLD subtlety...
for a NUance![tongue][/sub]
 
This seems to work, but I get an error. It doesn't like .Rows

Sub PONumber()
Dim lastRow As Long
Worksheets(POLog).Activate
lastRow = Cells(.Rows.Count, "B").End(xlUp).Row
End With
Range("PONumber2").Value = lastRow + 1
PONo = Range("A" & lastRow + 1).Value
Range("PONo").Value = PONo
End Sub
 
It didn't. I didn't have the most recent code in my last post.

Below is the current code, but I still get an error. It doesn't like .Rows. When I run the macro, .Rows comes back highlighted in blue.

Sub PONumber()
Dim lastRow As Long
Worksheets("POLog").Activate
lastRow = .Cells(.Rows.Count, "b").End(xlUp).Row
Range("PONumber2") = Range("A" & lastRow + 1).Value
End Sub
 
Code:
Sub PONumber()
   Dim lastRow As Long

   With Worksheets(POLog)
       lastRow = .Cells(.Rows.Count, "B").End(xlUp).Row
   End With

   Range("PONumber2").Value = lastRow + 1
   
   Range("PONo").Value = Cells(lastrow + 1, "A").Value
End Sub

Skip,
[sub]
[glasses]Just traded in my OLD subtlety...
for a NUance![tongue][/sub]
 
Ok. Thanks for your help. Everything seems to work except for 1 thing. The code is note placing the value of Range("A" & lastRow + 1).Value into cell PONo on worksheet POLog. Below is the code. The code is more than I've posted before because it needs to preform other functions like clearing the PO Form.


Sub DisplayForm()

Dim lastRow As Long

With Worksheets("POLog")
lastRow = .Cells(.Rows.Count, "B").End(xlUp).Row
End With

Range("PONumber2").Value = lastRow + 1

Range("PONo") = Range("A" & lastRow + 1).Value


Range("POnumber").Value = ""
Range("SubTotal").Value = 0
NextItem = Range("NextItem").Value
PORowStart = 13

Range("B" & PORowStart + 1, "M" & PORowStart + 1).ClearContents

If NextItem > 2 Then
For i = (PORowStart + (NextItem - 1)) To (PORowStart + 2) Step -1
Rows(i).EntireRow.Delete
Next i
End If

Range("A1").Value = 1

ProductSelection.Show
Range("POnumber").Value = Range("PONumber2").Value
End Sub
 

Code:
 Range("PONo") = [highlight #FCE94F]Worksheets("POLog").[/highlight]Range("A" & lastRow + 1).Value

Skip,
[sub]
[glasses]Just traded in my OLD subtlety...
for a NUance![tongue][/sub]
 
I'd also disambiguate ALL the sheet references.

Code:
Sub DisplayForm()

    Dim lastRow As Long, NextItem, PORowStart, i
    Dim wsSUM As Worksheet
    
    Set wsSUM = ActiveSheet     '[b]I would prefer a specific sheet name here[/b]
    
    PORowStart = 13             '[b]this ought to be derived[/b]
    
    With Worksheets("POLog")
        lastRow = .Cells(.Rows.Count, "B").End(xlUp).Row
    
        wsSUM.Range("PONumber2").Value = lastRow + 1
    
        wsSUM.Range("PONo") = .Range("A" & lastRow + 1).Value
        
        wsSUM.Range("POnumber").Value = ""
        wsSUM.Range("SubTotal").Value = 0
        NextItem = wsSUM.Range("NextItem").Value
        
        .Range(.Range("B" & PORowStart + 1), .Range("M" & PORowStart + 1)).ClearContents
        
        If NextItem > 2 Then
        For i = (PORowStart + (NextItem - 1)) To (PORowStart + 2) Step -1
            .Rows(i).EntireRow.Delete
        Next i
        End If
        
        Range("A1").Value = 1       '[b]Which sheet???[/b]
    
    '    ProductSelection.Show
        wsSUM.Range("POnumber").Value = wsSUM.Range("PONumber2").Value
    End With
End Sub



Skip,
[sub]
[glasses]Just traded in my OLD subtlety...
for a NUance![tongue][/sub]
 
Status
Not open for further replies.
Back
Top