PDA

View Full Version : Using Cell Value in VBA



ib175
09-15-2015, 10:49 AM
I am reasonably new to VBA but have put together the following routine to scroll through a list and print each version of the sheet that the loop creates. The list is in column "h".

I do however have different versions of the list in other columns, is there anyway I can get the routine to look up the value in a excel cell to determine what column to use. i.e. Cell A1 has the value "v" in it therefore the routine will use the list in column "v".

Many thanks.

Sub PrintfromDVList()
Dim ws As Worksheet
Dim i As Long

Set ws = Sheets("Team RAW Data")

For i = 1 To ws.Cells(Rows.Count, cell(1, "h")).End(xlUp).Row
If ws.Cells(i, cell(1, "h")).Value = "zzz" Then Exit Sub
With Sheets("AP QTD Trend")
.Range("B1").Value = ws.Cells(i, cell(1, "h")).Value
.PrintOut
End With
Next i

End Sub

Simon Lloyd
09-15-2015, 01:51 PM
Assuming the code you provided works (as i haven't checked it), the following should work:
Sub PrintfromDVList()
Dim ws As Worksheet
Dim i As Long

Set ws = Sheets("Team RAW Data")

For i = 1 To ws.Cells(Rows.Count, cell(1, ws.Range("A1").Value)).End(xlUp).Row
If ws.Cells(i, cell(1, ws.Range("A1").Value)).Value = "zzz" Then Exit Sub
With Sheets("AP QTD Trend")
.Range("B1").Value = ws.Cells(i, cell(1, ws.Range("A1").Value)).Value
.PrintOut
End With
Next i

End Sub

ib175
09-17-2015, 01:36 AM
Hi thanks for your post, I have tried the code and get a compile error : sub or function not defined.

I have rechecked the original code and manual changed the column reference which works for each of my list, do you have any other suggestions?

Many thanks

Simon Lloyd
09-23-2015, 11:16 AM
It didnt compile because your original code didnt compile, here's your errors fixed.
Sub PrintfromDVList()
Dim ws As Worksheet
Dim i As Long

Set ws = Sheets("Team RAW Data")

For i = 1 To ws.Cells(Rows.Count, Cells(1, ws.Range("A1").Value)).End(xlUp).Row
If ws.Cells(i, Cells(1, ws.Range("A1").Value)).Value = "zzz" Then Exit Sub
With Sheets("AP QTD Trend")
.Range("B1").Value = ws.Cells(i, Cells(1, ws.Range("A1").Value)).Value
.PrintOut
End With
Next i

End Sub