Niste rekli da je promenljiva vrednost kolona i sl.
U svakom slucaju imali ste primer f-je GetLastRows() u poslednjem primer koji ste mogli da iskoristite kao polaznu osnovu da to odradite na praktican nacin i da istu malo modifikujete.
U nastavku vam dostavljam primer korekcije na osnovu uvida u vas poslednji post.
Ulazne vrednosti su Sheet kao Worksheet objekat, Total Row Column Index i Value Column index.
F-ja na osnovu zadatih vrednosti vraca vrednost poslednje zauzete kolene tj. prve od pozadi.
Code:
Function GetLastRows(ByVal ThisSheet As Excel.Worksheet, ByVal TotalRowsColIndex As Integer, ByVal ValueColIndex As Integer) As Long
Dim iTotalRows As Long
' Return value
GetLastRows = 0
With ThisSheet
' Get total rows for given column
iTotalRows = .UsedRange.Columns(TotalRowsColIndex).Rows.Count
' Go from last row till first row from back which has data in cell
For i = iTotalRows To 1 Step -1
If .Cells(i, ValueColIndex).Value <> "" Then
' Return value
GetLastRows = i
Exit For
End If
Next
End With
End Function
Primer pozivanja f-je u kod bi bilo nesto kao:
Code:
Dim sSheetName As String
sSheetName = "Sheet1"
iRow = GetLastRows(Excel.Sheets(sSheetName), 9, 9)
' ili
iRow = GetLastRows(Excel.Sheets("Sheet1"), 9, 9)
If iRow = 0 Then
MsgBox "No rows for given column."
Exit Sub
End If
' ...
Primer koda na osnovu predzadnjeg koda gde je bila data ista f-ja ali bez poslednja 2 ulazna parametra f-je a sada sa dodatim parametrima.
Code:
Sub Print4MeSheet1()
Dim iTotalRows As Long
Dim iRow As Long
Dim i As Long
Dim sSheetName As String
Dim sRange As String
sSheetName = "Sheet1"
' Get last row
iRow = GetLastRows(Excel.Sheets(sSheetName), 6, 6)
If iRow = 0 Then
MsgBox "No rows for given column."
Exit Sub
End If
With Excel.Sheets(sSheetName)
.Activate
.Select
.Range("B1:G" & iRow).Select
sRange = Range("B1:G" & iRow).Address
End With
' Do printing job for me
Print4Me Excel.Sheets(sSheetName), sRange
End Sub
U prilogu je dat i xls file sa ovim izmenama.
p.s. Sto se same stampe tice treba obratiti na orijentaciju papira kao i na margine.