jadams0173
Technical User
- Feb 18, 2005
- 1,210
I'm struggling with getting the correct number of used rows. I've read thread707-1246047 but I can't get my arms around if. I'm very much a newbie when it comes to excel VBA. The problem is the used range for the rows doesn't include all the used rows after the insertion of some blank rows. I tried to 'reset' the value but to no avail. Also there is a lot of rem'd out code as I just found an example and started working with it trying to get it to where I needed to be.
Also if it isn't asking being overly 'greedy'. Why can't I do all my formating and adding text to the cells using the oSheet variable?
I tried to have patience but it took to long!
-DW
Also if it isn't asking being overly 'greedy'. Why can't I do all my formating and adding text to the cells using the oSheet variable?
Code:
Private Sub FormatProductsWorksheet()
Dim oSheet As Worksheet, oHdr As Range, nCols, nRows As Long
Dim x As Integer
Set oSheet = ThisWorkbook.Worksheets("Training")
With oSheet
nRows = .UsedRange.Rows.Count
nCols = .UsedRange.Columns.Count
Set oHdr = .Range(oSheet.Cells(1, 1), oSheet.Cells(1, nCols))
oHdr.Font.Bold = True
.Columns.AutoFit
End With
ThisWorkbook.Worksheets("Training").Activate
'insert 4 blank rows
ActiveSheet.Rows("1:6").Select
Selection.Insert shift:=xlDown
Cells(1, 1).Select
[red] 'RESET THE BVALUE TO THE NUMBER OF USED ROWS
nRows = ThisWorkbook.Worksheets("TRAINING").UsedRange.Rows.Count
Debug.Print nRows
[/red]
'Report Title
ActiveSheet.Cells(1, 3) = "Employee Gap Analysis by Job"
With ActiveSheet.Range("a1", "d1")
.Merge
.Font.Bold = True
.Font.Size = 14
.HorizontalAlignment = xlCenter
End With
'Employee number comes from X8. Inserted into sheet by vb app
ActiveSheet.Cells(2, 3) = "Employee Number: " & Cells(8, 24)
With ActiveSheet.Range("a2", "d2")
.Merge
.Font.Bold = True
.Font.Size = 14
.HorizontalAlignment = xlCenter
End With
'Employee name comes from First name (V8) Last name (W8)
ActiveSheet.Cells(3, 3) = "Employee Name: " & Cells(8, 22) & " " & Cells(8, 23)
With ActiveSheet.Range("a3", "d3")
.Merge
.Font.Bold = True
.Font.Size = 10
.HorizontalAlignment = xlCenter
End With
'Required Courses label and job number
ActiveSheet.Cells(5, 3) = "Required Courses for Job: " & Cells(8, 25)
With ActiveSheet.Range("a5", "d5")
.Merge
.Font.Bold = 10
.HorizontalAlignment = xlCenter
End With
[red]
'APPLY CONDITIONAL FORMATING TO MAKE EXPIRED DAETS RED IF THEY ARE LESS THAN OR = TODAY'S DATE
Cells(8, 4).Select
With ThisWorkbook.Worksheets("TRAINING").Range("d8", "d" & nRows).Select
' Columns("D:D").Select
Selection.FormatConditions.Delete
Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlLessEqual, _
Formula1:="=today()"
Selection.FormatConditions(1).Font.ColorIndex = 3
End With
[/red]
'Centers the data in the entire sheet
' With ActiveSheet.Range(ActiveSheet.Cells(7, 1), ActiveSheet.Cells(nRows, nCols))
' .HorizontalAlignment = xlCenter
' End With
Cells(1, 1).Select
End Sub
I tried to have patience but it took to long!