Excel Range less clumsy [on hold]
up vote
-1
down vote
favorite
As an old dog (age 73) learning new (Excel VBA) tricks, I am reasonably happy with putting together the code below. But I think it could be cleaner. How would you have coded it?
Private Sub Workbook_Open()
Dim lastRow As Long 'last row with data
Dim thisDate As Double 'start timestamp
thisDate = Now()
With Sheets("Pressure Log")
lastRow = .Range("B" & .Rows.Count).End(xlUp).Row 'populate next row with date/time
.Range("B" & lastRow + 1 & ":G" & lastRow + 1).Borders.LineStyle = xlContinuous
.Range("B" & lastRow).Offset(1) = Format(thisDate, "dddd")
.Range("B" & lastRow).Offset(1, 1) = Format(thisDate, "mm/dd/yyyy")
.Range("B" & lastRow).Offset(1, 2) = Format(thisDate, "hh:mm AM/PM")
.Range("B" & lastRow).Offset(1, 3).Select 'position for data
End With
End Sub
vba excel
New contributor
put on hold as off-topic by πάντα ῥεῖ, Sᴀᴍ Onᴇᴌᴀ, Hosch250, alecxe, Toby Speight 56 mins ago
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Lacks concrete context: Code Review requires concrete code from a project, with sufficient context for reviewers to understand how that code is used. Pseudocode, stub code, hypothetical code, obfuscated code, and generic best practices are outside the scope of this site." – πάντα ῥεῖ, Sᴀᴍ Onᴇᴌᴀ, Hosch250, alecxe
If this question can be reworded to fit the rules in the help center, please edit the question.
add a comment |
up vote
-1
down vote
favorite
As an old dog (age 73) learning new (Excel VBA) tricks, I am reasonably happy with putting together the code below. But I think it could be cleaner. How would you have coded it?
Private Sub Workbook_Open()
Dim lastRow As Long 'last row with data
Dim thisDate As Double 'start timestamp
thisDate = Now()
With Sheets("Pressure Log")
lastRow = .Range("B" & .Rows.Count).End(xlUp).Row 'populate next row with date/time
.Range("B" & lastRow + 1 & ":G" & lastRow + 1).Borders.LineStyle = xlContinuous
.Range("B" & lastRow).Offset(1) = Format(thisDate, "dddd")
.Range("B" & lastRow).Offset(1, 1) = Format(thisDate, "mm/dd/yyyy")
.Range("B" & lastRow).Offset(1, 2) = Format(thisDate, "hh:mm AM/PM")
.Range("B" & lastRow).Offset(1, 3).Select 'position for data
End With
End Sub
vba excel
New contributor
put on hold as off-topic by πάντα ῥεῖ, Sᴀᴍ Onᴇᴌᴀ, Hosch250, alecxe, Toby Speight 56 mins ago
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Lacks concrete context: Code Review requires concrete code from a project, with sufficient context for reviewers to understand how that code is used. Pseudocode, stub code, hypothetical code, obfuscated code, and generic best practices are outside the scope of this site." – πάντα ῥεῖ, Sᴀᴍ Onᴇᴌᴀ, Hosch250, alecxe
If this question can be reworded to fit the rules in the help center, please edit the question.
2
Cross post from: stackoverflow.com/questions/53683704/excel-range-less-clumsy
– Vogel612♦
16 hours ago
Welcome to Code Review! The current question title, which states your concerns about the code, is too general to be useful here. Please edit to the site standard, which is for the title to simply state the task accomplished by the code. Please see How to get the best value out of Code Review: Asking Questions for guidance on writing good question titles.
– Toby Speight
56 mins ago
add a comment |
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
As an old dog (age 73) learning new (Excel VBA) tricks, I am reasonably happy with putting together the code below. But I think it could be cleaner. How would you have coded it?
Private Sub Workbook_Open()
Dim lastRow As Long 'last row with data
Dim thisDate As Double 'start timestamp
thisDate = Now()
With Sheets("Pressure Log")
lastRow = .Range("B" & .Rows.Count).End(xlUp).Row 'populate next row with date/time
.Range("B" & lastRow + 1 & ":G" & lastRow + 1).Borders.LineStyle = xlContinuous
.Range("B" & lastRow).Offset(1) = Format(thisDate, "dddd")
.Range("B" & lastRow).Offset(1, 1) = Format(thisDate, "mm/dd/yyyy")
.Range("B" & lastRow).Offset(1, 2) = Format(thisDate, "hh:mm AM/PM")
.Range("B" & lastRow).Offset(1, 3).Select 'position for data
End With
End Sub
vba excel
New contributor
As an old dog (age 73) learning new (Excel VBA) tricks, I am reasonably happy with putting together the code below. But I think it could be cleaner. How would you have coded it?
Private Sub Workbook_Open()
Dim lastRow As Long 'last row with data
Dim thisDate As Double 'start timestamp
thisDate = Now()
With Sheets("Pressure Log")
lastRow = .Range("B" & .Rows.Count).End(xlUp).Row 'populate next row with date/time
.Range("B" & lastRow + 1 & ":G" & lastRow + 1).Borders.LineStyle = xlContinuous
.Range("B" & lastRow).Offset(1) = Format(thisDate, "dddd")
.Range("B" & lastRow).Offset(1, 1) = Format(thisDate, "mm/dd/yyyy")
.Range("B" & lastRow).Offset(1, 2) = Format(thisDate, "hh:mm AM/PM")
.Range("B" & lastRow).Offset(1, 3).Select 'position for data
End With
End Sub
vba excel
vba excel
New contributor
New contributor
edited 16 hours ago
Vogel612♦
21.3k346128
21.3k346128
New contributor
asked 17 hours ago
grNadpa
6
6
New contributor
New contributor
put on hold as off-topic by πάντα ῥεῖ, Sᴀᴍ Onᴇᴌᴀ, Hosch250, alecxe, Toby Speight 56 mins ago
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Lacks concrete context: Code Review requires concrete code from a project, with sufficient context for reviewers to understand how that code is used. Pseudocode, stub code, hypothetical code, obfuscated code, and generic best practices are outside the scope of this site." – πάντα ῥεῖ, Sᴀᴍ Onᴇᴌᴀ, Hosch250, alecxe
If this question can be reworded to fit the rules in the help center, please edit the question.
put on hold as off-topic by πάντα ῥεῖ, Sᴀᴍ Onᴇᴌᴀ, Hosch250, alecxe, Toby Speight 56 mins ago
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Lacks concrete context: Code Review requires concrete code from a project, with sufficient context for reviewers to understand how that code is used. Pseudocode, stub code, hypothetical code, obfuscated code, and generic best practices are outside the scope of this site." – πάντα ῥεῖ, Sᴀᴍ Onᴇᴌᴀ, Hosch250, alecxe
If this question can be reworded to fit the rules in the help center, please edit the question.
2
Cross post from: stackoverflow.com/questions/53683704/excel-range-less-clumsy
– Vogel612♦
16 hours ago
Welcome to Code Review! The current question title, which states your concerns about the code, is too general to be useful here. Please edit to the site standard, which is for the title to simply state the task accomplished by the code. Please see How to get the best value out of Code Review: Asking Questions for guidance on writing good question titles.
– Toby Speight
56 mins ago
add a comment |
2
Cross post from: stackoverflow.com/questions/53683704/excel-range-less-clumsy
– Vogel612♦
16 hours ago
Welcome to Code Review! The current question title, which states your concerns about the code, is too general to be useful here. Please edit to the site standard, which is for the title to simply state the task accomplished by the code. Please see How to get the best value out of Code Review: Asking Questions for guidance on writing good question titles.
– Toby Speight
56 mins ago
2
2
Cross post from: stackoverflow.com/questions/53683704/excel-range-less-clumsy
– Vogel612♦
16 hours ago
Cross post from: stackoverflow.com/questions/53683704/excel-range-less-clumsy
– Vogel612♦
16 hours ago
Welcome to Code Review! The current question title, which states your concerns about the code, is too general to be useful here. Please edit to the site standard, which is for the title to simply state the task accomplished by the code. Please see How to get the best value out of Code Review: Asking Questions for guidance on writing good question titles.
– Toby Speight
56 mins ago
Welcome to Code Review! The current question title, which states your concerns about the code, is too general to be useful here. Please edit to the site standard, which is for the title to simply state the task accomplished by the code. Please see How to get the best value out of Code Review: Asking Questions for guidance on writing good question titles.
– Toby Speight
56 mins ago
add a comment |
2 Answers
2
active
oldest
votes
up vote
1
down vote
Properly formatting and indenting code is always a good start.
Using Option Explicit
at the top of every module is a must. You may already do this, just thought I would mention it.
You declare thisDate
as a Double
but you use it as a Date
. Declare it as a Date
.
Make your life a little easier and set a range to the start of your new row instead of calling a calculated range. Example below:
Private Sub Workbook_Open()
Dim lastRow As Long 'last row with data
Dim thisDate As Date 'start timestamp
Dim entryRange as Range
thisDate = Now()
With Sheets("Pressure Log")
lastRow = .Range("B" & .Rows.Count).End(xlUp).Row 'populate next row with date/time
Set entryRange = .Range("B" & lastRow+1) ` There are other ways of doing this too.
End With
entryRange.resize(1, 6).Borders.LineStyle = xlContinuous ' Yes, could do this in a With block as well.
entryRange.Value = Format(thisDate, "dddd")
entryRange.Offset(, 1).Value = Format(thisDate, "mm/dd/yyyy")
entryRange.Offset(, 2) = Format(thisDate, "hh:mm AM/PM")
entryRange.Offset(, 3).Select 'position for data
End Sub
thank you AJD "Properly formatting and indenting code is always a good start ." Agreed. My script code is indented but I have trouble with this forum's code block.
– grNadpa
11 hours ago
thank you @AJD "Properly formatting and indenting code is always a good start ." Agreed. My script code is indented but I have trouble with this forum's code block. (as is trying to line break in this comment) "Using Option Explicit at the top of every module is a must. " I understood the concept, but thought that Option Explicit was once for the entire workbook so I did not know where to put it. Now I know. Thank you. "You declare thisDate as a Double but you use it as a Date. Declare it as a Date." I thought Date was a method, not a type as well. Will make the change
– grNadpa
11 hours ago
" Set entryRange = .Range("B" & lastRow+1) ` There are other ways of doing this too." I gather you recommend this as the better alternative. What might be some of the other ways?
– grNadpa
11 hours ago
@grNadpa:Set entryRange = .Range("B" & lastRow).Offset(1,0)
is an easy alternative. You could also use.Cells(lastRow+1,2)
(not preferable becauseCells
is relative to a range and which cells are selected can be confused if you change the base range). In some cases, just comes down to personal preferences.
– AJD
3 hours ago
add a comment |
up vote
0
down vote
Another With
statement would make the code easier to read.
With Sheets("Pressure Log")
lastRow = .Range("B" & .Rows.Count).End(xlUp).Row 'populate next row with date/time
.Range("B" & lastRow + 1 & ":G" & lastRow + 1).Borders.LineStyle = xlContinuous
With .Range("B" & lastRow)
.Offset(1) = Format(thisDate, "dddd")
.Offset(1, 1) = Format(thisDate, "mm/dd/yyyy")
.Offset(1, 2) = Format(thisDate, "hh:mm AM/PM")
.Offset(1, 3).Select 'position for data
End With
End With
It is better to format entire columns and rows. These reduces the file size by combining multiple css rules.
There are many way to skin this cat. For variety I set a range variable ( newRow ) to the next empty row. This allows me to uses ranges relative to the new row.
Dim newRow As Range
With Worksheets("Pressure Log")
Set newRow = .Range("B" & .Rows.Count).End(xlUp).Offset(1).EntireRow
newRow.Range("B1:G1").Borders.LineStyle = xlContinuous
newRow.Range("B1:D1").Value = Now
.Columns("C").NumberFormat = "ddd"
.Columns("D").NumberFormat = "mm/dd/yyyy"
.Columns("E").NumberFormat = "hh:mm AM/PM"
.Columns("C:G").AutoFit
newRow.Columns("E").Select
End With
With this setup we can copy all the formats from the row above except NumberFormats by using Range().FillDown
.
newRow.FillDown
Alternately, we could copy all the formats from the row above like this:
newRow.Offset(-1).Copy
newRow.PasteSpecial Paste:=xlPasteFormats
Application.CutCopyMode = False
Your code is pretty solid but if you truly want to learn Excel VBA I would start by watching this series: Excel VBA Introduction.
You should also use Rubberduck VBA. It is a free add-in with many features to help you write better code. The code formatter alone is invaluable.
Last but not least start answering questions on StackOverflow. This will give you exposure to questions that you would never think to ask and solutions that you didn't know where possible.
1
Advice on the last suggestion ("Last but not least start answering questions on StackOverflow.") - while providing good exposure it will also open you up to criticism, especially as you learn how to provide a good answer. A thick skin and not taking anything as personal helps weather the early attempts!
– AJD
3 hours ago
@AJD I agree. I have my share of callouses. But I improved more in the 2 years of SO than I did in the previous 6 years.
– TinMan
2 hours ago
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
Properly formatting and indenting code is always a good start.
Using Option Explicit
at the top of every module is a must. You may already do this, just thought I would mention it.
You declare thisDate
as a Double
but you use it as a Date
. Declare it as a Date
.
Make your life a little easier and set a range to the start of your new row instead of calling a calculated range. Example below:
Private Sub Workbook_Open()
Dim lastRow As Long 'last row with data
Dim thisDate As Date 'start timestamp
Dim entryRange as Range
thisDate = Now()
With Sheets("Pressure Log")
lastRow = .Range("B" & .Rows.Count).End(xlUp).Row 'populate next row with date/time
Set entryRange = .Range("B" & lastRow+1) ` There are other ways of doing this too.
End With
entryRange.resize(1, 6).Borders.LineStyle = xlContinuous ' Yes, could do this in a With block as well.
entryRange.Value = Format(thisDate, "dddd")
entryRange.Offset(, 1).Value = Format(thisDate, "mm/dd/yyyy")
entryRange.Offset(, 2) = Format(thisDate, "hh:mm AM/PM")
entryRange.Offset(, 3).Select 'position for data
End Sub
thank you AJD "Properly formatting and indenting code is always a good start ." Agreed. My script code is indented but I have trouble with this forum's code block.
– grNadpa
11 hours ago
thank you @AJD "Properly formatting and indenting code is always a good start ." Agreed. My script code is indented but I have trouble with this forum's code block. (as is trying to line break in this comment) "Using Option Explicit at the top of every module is a must. " I understood the concept, but thought that Option Explicit was once for the entire workbook so I did not know where to put it. Now I know. Thank you. "You declare thisDate as a Double but you use it as a Date. Declare it as a Date." I thought Date was a method, not a type as well. Will make the change
– grNadpa
11 hours ago
" Set entryRange = .Range("B" & lastRow+1) ` There are other ways of doing this too." I gather you recommend this as the better alternative. What might be some of the other ways?
– grNadpa
11 hours ago
@grNadpa:Set entryRange = .Range("B" & lastRow).Offset(1,0)
is an easy alternative. You could also use.Cells(lastRow+1,2)
(not preferable becauseCells
is relative to a range and which cells are selected can be confused if you change the base range). In some cases, just comes down to personal preferences.
– AJD
3 hours ago
add a comment |
up vote
1
down vote
Properly formatting and indenting code is always a good start.
Using Option Explicit
at the top of every module is a must. You may already do this, just thought I would mention it.
You declare thisDate
as a Double
but you use it as a Date
. Declare it as a Date
.
Make your life a little easier and set a range to the start of your new row instead of calling a calculated range. Example below:
Private Sub Workbook_Open()
Dim lastRow As Long 'last row with data
Dim thisDate As Date 'start timestamp
Dim entryRange as Range
thisDate = Now()
With Sheets("Pressure Log")
lastRow = .Range("B" & .Rows.Count).End(xlUp).Row 'populate next row with date/time
Set entryRange = .Range("B" & lastRow+1) ` There are other ways of doing this too.
End With
entryRange.resize(1, 6).Borders.LineStyle = xlContinuous ' Yes, could do this in a With block as well.
entryRange.Value = Format(thisDate, "dddd")
entryRange.Offset(, 1).Value = Format(thisDate, "mm/dd/yyyy")
entryRange.Offset(, 2) = Format(thisDate, "hh:mm AM/PM")
entryRange.Offset(, 3).Select 'position for data
End Sub
thank you AJD "Properly formatting and indenting code is always a good start ." Agreed. My script code is indented but I have trouble with this forum's code block.
– grNadpa
11 hours ago
thank you @AJD "Properly formatting and indenting code is always a good start ." Agreed. My script code is indented but I have trouble with this forum's code block. (as is trying to line break in this comment) "Using Option Explicit at the top of every module is a must. " I understood the concept, but thought that Option Explicit was once for the entire workbook so I did not know where to put it. Now I know. Thank you. "You declare thisDate as a Double but you use it as a Date. Declare it as a Date." I thought Date was a method, not a type as well. Will make the change
– grNadpa
11 hours ago
" Set entryRange = .Range("B" & lastRow+1) ` There are other ways of doing this too." I gather you recommend this as the better alternative. What might be some of the other ways?
– grNadpa
11 hours ago
@grNadpa:Set entryRange = .Range("B" & lastRow).Offset(1,0)
is an easy alternative. You could also use.Cells(lastRow+1,2)
(not preferable becauseCells
is relative to a range and which cells are selected can be confused if you change the base range). In some cases, just comes down to personal preferences.
– AJD
3 hours ago
add a comment |
up vote
1
down vote
up vote
1
down vote
Properly formatting and indenting code is always a good start.
Using Option Explicit
at the top of every module is a must. You may already do this, just thought I would mention it.
You declare thisDate
as a Double
but you use it as a Date
. Declare it as a Date
.
Make your life a little easier and set a range to the start of your new row instead of calling a calculated range. Example below:
Private Sub Workbook_Open()
Dim lastRow As Long 'last row with data
Dim thisDate As Date 'start timestamp
Dim entryRange as Range
thisDate = Now()
With Sheets("Pressure Log")
lastRow = .Range("B" & .Rows.Count).End(xlUp).Row 'populate next row with date/time
Set entryRange = .Range("B" & lastRow+1) ` There are other ways of doing this too.
End With
entryRange.resize(1, 6).Borders.LineStyle = xlContinuous ' Yes, could do this in a With block as well.
entryRange.Value = Format(thisDate, "dddd")
entryRange.Offset(, 1).Value = Format(thisDate, "mm/dd/yyyy")
entryRange.Offset(, 2) = Format(thisDate, "hh:mm AM/PM")
entryRange.Offset(, 3).Select 'position for data
End Sub
Properly formatting and indenting code is always a good start.
Using Option Explicit
at the top of every module is a must. You may already do this, just thought I would mention it.
You declare thisDate
as a Double
but you use it as a Date
. Declare it as a Date
.
Make your life a little easier and set a range to the start of your new row instead of calling a calculated range. Example below:
Private Sub Workbook_Open()
Dim lastRow As Long 'last row with data
Dim thisDate As Date 'start timestamp
Dim entryRange as Range
thisDate = Now()
With Sheets("Pressure Log")
lastRow = .Range("B" & .Rows.Count).End(xlUp).Row 'populate next row with date/time
Set entryRange = .Range("B" & lastRow+1) ` There are other ways of doing this too.
End With
entryRange.resize(1, 6).Borders.LineStyle = xlContinuous ' Yes, could do this in a With block as well.
entryRange.Value = Format(thisDate, "dddd")
entryRange.Offset(, 1).Value = Format(thisDate, "mm/dd/yyyy")
entryRange.Offset(, 2) = Format(thisDate, "hh:mm AM/PM")
entryRange.Offset(, 3).Select 'position for data
End Sub
answered 14 hours ago
AJD
1,1961213
1,1961213
thank you AJD "Properly formatting and indenting code is always a good start ." Agreed. My script code is indented but I have trouble with this forum's code block.
– grNadpa
11 hours ago
thank you @AJD "Properly formatting and indenting code is always a good start ." Agreed. My script code is indented but I have trouble with this forum's code block. (as is trying to line break in this comment) "Using Option Explicit at the top of every module is a must. " I understood the concept, but thought that Option Explicit was once for the entire workbook so I did not know where to put it. Now I know. Thank you. "You declare thisDate as a Double but you use it as a Date. Declare it as a Date." I thought Date was a method, not a type as well. Will make the change
– grNadpa
11 hours ago
" Set entryRange = .Range("B" & lastRow+1) ` There are other ways of doing this too." I gather you recommend this as the better alternative. What might be some of the other ways?
– grNadpa
11 hours ago
@grNadpa:Set entryRange = .Range("B" & lastRow).Offset(1,0)
is an easy alternative. You could also use.Cells(lastRow+1,2)
(not preferable becauseCells
is relative to a range and which cells are selected can be confused if you change the base range). In some cases, just comes down to personal preferences.
– AJD
3 hours ago
add a comment |
thank you AJD "Properly formatting and indenting code is always a good start ." Agreed. My script code is indented but I have trouble with this forum's code block.
– grNadpa
11 hours ago
thank you @AJD "Properly formatting and indenting code is always a good start ." Agreed. My script code is indented but I have trouble with this forum's code block. (as is trying to line break in this comment) "Using Option Explicit at the top of every module is a must. " I understood the concept, but thought that Option Explicit was once for the entire workbook so I did not know where to put it. Now I know. Thank you. "You declare thisDate as a Double but you use it as a Date. Declare it as a Date." I thought Date was a method, not a type as well. Will make the change
– grNadpa
11 hours ago
" Set entryRange = .Range("B" & lastRow+1) ` There are other ways of doing this too." I gather you recommend this as the better alternative. What might be some of the other ways?
– grNadpa
11 hours ago
@grNadpa:Set entryRange = .Range("B" & lastRow).Offset(1,0)
is an easy alternative. You could also use.Cells(lastRow+1,2)
(not preferable becauseCells
is relative to a range and which cells are selected can be confused if you change the base range). In some cases, just comes down to personal preferences.
– AJD
3 hours ago
thank you AJD "Properly formatting and indenting code is always a good start ." Agreed. My script code is indented but I have trouble with this forum's code block.
– grNadpa
11 hours ago
thank you AJD "Properly formatting and indenting code is always a good start ." Agreed. My script code is indented but I have trouble with this forum's code block.
– grNadpa
11 hours ago
thank you @AJD "Properly formatting and indenting code is always a good start ." Agreed. My script code is indented but I have trouble with this forum's code block. (as is trying to line break in this comment) "Using Option Explicit at the top of every module is a must. " I understood the concept, but thought that Option Explicit was once for the entire workbook so I did not know where to put it. Now I know. Thank you. "You declare thisDate as a Double but you use it as a Date. Declare it as a Date." I thought Date was a method, not a type as well. Will make the change
– grNadpa
11 hours ago
thank you @AJD "Properly formatting and indenting code is always a good start ." Agreed. My script code is indented but I have trouble with this forum's code block. (as is trying to line break in this comment) "Using Option Explicit at the top of every module is a must. " I understood the concept, but thought that Option Explicit was once for the entire workbook so I did not know where to put it. Now I know. Thank you. "You declare thisDate as a Double but you use it as a Date. Declare it as a Date." I thought Date was a method, not a type as well. Will make the change
– grNadpa
11 hours ago
" Set entryRange = .Range("B" & lastRow+1) ` There are other ways of doing this too." I gather you recommend this as the better alternative. What might be some of the other ways?
– grNadpa
11 hours ago
" Set entryRange = .Range("B" & lastRow+1) ` There are other ways of doing this too." I gather you recommend this as the better alternative. What might be some of the other ways?
– grNadpa
11 hours ago
@grNadpa:
Set entryRange = .Range("B" & lastRow).Offset(1,0)
is an easy alternative. You could also use .Cells(lastRow+1,2)
(not preferable because Cells
is relative to a range and which cells are selected can be confused if you change the base range). In some cases, just comes down to personal preferences.– AJD
3 hours ago
@grNadpa:
Set entryRange = .Range("B" & lastRow).Offset(1,0)
is an easy alternative. You could also use .Cells(lastRow+1,2)
(not preferable because Cells
is relative to a range and which cells are selected can be confused if you change the base range). In some cases, just comes down to personal preferences.– AJD
3 hours ago
add a comment |
up vote
0
down vote
Another With
statement would make the code easier to read.
With Sheets("Pressure Log")
lastRow = .Range("B" & .Rows.Count).End(xlUp).Row 'populate next row with date/time
.Range("B" & lastRow + 1 & ":G" & lastRow + 1).Borders.LineStyle = xlContinuous
With .Range("B" & lastRow)
.Offset(1) = Format(thisDate, "dddd")
.Offset(1, 1) = Format(thisDate, "mm/dd/yyyy")
.Offset(1, 2) = Format(thisDate, "hh:mm AM/PM")
.Offset(1, 3).Select 'position for data
End With
End With
It is better to format entire columns and rows. These reduces the file size by combining multiple css rules.
There are many way to skin this cat. For variety I set a range variable ( newRow ) to the next empty row. This allows me to uses ranges relative to the new row.
Dim newRow As Range
With Worksheets("Pressure Log")
Set newRow = .Range("B" & .Rows.Count).End(xlUp).Offset(1).EntireRow
newRow.Range("B1:G1").Borders.LineStyle = xlContinuous
newRow.Range("B1:D1").Value = Now
.Columns("C").NumberFormat = "ddd"
.Columns("D").NumberFormat = "mm/dd/yyyy"
.Columns("E").NumberFormat = "hh:mm AM/PM"
.Columns("C:G").AutoFit
newRow.Columns("E").Select
End With
With this setup we can copy all the formats from the row above except NumberFormats by using Range().FillDown
.
newRow.FillDown
Alternately, we could copy all the formats from the row above like this:
newRow.Offset(-1).Copy
newRow.PasteSpecial Paste:=xlPasteFormats
Application.CutCopyMode = False
Your code is pretty solid but if you truly want to learn Excel VBA I would start by watching this series: Excel VBA Introduction.
You should also use Rubberduck VBA. It is a free add-in with many features to help you write better code. The code formatter alone is invaluable.
Last but not least start answering questions on StackOverflow. This will give you exposure to questions that you would never think to ask and solutions that you didn't know where possible.
1
Advice on the last suggestion ("Last but not least start answering questions on StackOverflow.") - while providing good exposure it will also open you up to criticism, especially as you learn how to provide a good answer. A thick skin and not taking anything as personal helps weather the early attempts!
– AJD
3 hours ago
@AJD I agree. I have my share of callouses. But I improved more in the 2 years of SO than I did in the previous 6 years.
– TinMan
2 hours ago
add a comment |
up vote
0
down vote
Another With
statement would make the code easier to read.
With Sheets("Pressure Log")
lastRow = .Range("B" & .Rows.Count).End(xlUp).Row 'populate next row with date/time
.Range("B" & lastRow + 1 & ":G" & lastRow + 1).Borders.LineStyle = xlContinuous
With .Range("B" & lastRow)
.Offset(1) = Format(thisDate, "dddd")
.Offset(1, 1) = Format(thisDate, "mm/dd/yyyy")
.Offset(1, 2) = Format(thisDate, "hh:mm AM/PM")
.Offset(1, 3).Select 'position for data
End With
End With
It is better to format entire columns and rows. These reduces the file size by combining multiple css rules.
There are many way to skin this cat. For variety I set a range variable ( newRow ) to the next empty row. This allows me to uses ranges relative to the new row.
Dim newRow As Range
With Worksheets("Pressure Log")
Set newRow = .Range("B" & .Rows.Count).End(xlUp).Offset(1).EntireRow
newRow.Range("B1:G1").Borders.LineStyle = xlContinuous
newRow.Range("B1:D1").Value = Now
.Columns("C").NumberFormat = "ddd"
.Columns("D").NumberFormat = "mm/dd/yyyy"
.Columns("E").NumberFormat = "hh:mm AM/PM"
.Columns("C:G").AutoFit
newRow.Columns("E").Select
End With
With this setup we can copy all the formats from the row above except NumberFormats by using Range().FillDown
.
newRow.FillDown
Alternately, we could copy all the formats from the row above like this:
newRow.Offset(-1).Copy
newRow.PasteSpecial Paste:=xlPasteFormats
Application.CutCopyMode = False
Your code is pretty solid but if you truly want to learn Excel VBA I would start by watching this series: Excel VBA Introduction.
You should also use Rubberduck VBA. It is a free add-in with many features to help you write better code. The code formatter alone is invaluable.
Last but not least start answering questions on StackOverflow. This will give you exposure to questions that you would never think to ask and solutions that you didn't know where possible.
1
Advice on the last suggestion ("Last but not least start answering questions on StackOverflow.") - while providing good exposure it will also open you up to criticism, especially as you learn how to provide a good answer. A thick skin and not taking anything as personal helps weather the early attempts!
– AJD
3 hours ago
@AJD I agree. I have my share of callouses. But I improved more in the 2 years of SO than I did in the previous 6 years.
– TinMan
2 hours ago
add a comment |
up vote
0
down vote
up vote
0
down vote
Another With
statement would make the code easier to read.
With Sheets("Pressure Log")
lastRow = .Range("B" & .Rows.Count).End(xlUp).Row 'populate next row with date/time
.Range("B" & lastRow + 1 & ":G" & lastRow + 1).Borders.LineStyle = xlContinuous
With .Range("B" & lastRow)
.Offset(1) = Format(thisDate, "dddd")
.Offset(1, 1) = Format(thisDate, "mm/dd/yyyy")
.Offset(1, 2) = Format(thisDate, "hh:mm AM/PM")
.Offset(1, 3).Select 'position for data
End With
End With
It is better to format entire columns and rows. These reduces the file size by combining multiple css rules.
There are many way to skin this cat. For variety I set a range variable ( newRow ) to the next empty row. This allows me to uses ranges relative to the new row.
Dim newRow As Range
With Worksheets("Pressure Log")
Set newRow = .Range("B" & .Rows.Count).End(xlUp).Offset(1).EntireRow
newRow.Range("B1:G1").Borders.LineStyle = xlContinuous
newRow.Range("B1:D1").Value = Now
.Columns("C").NumberFormat = "ddd"
.Columns("D").NumberFormat = "mm/dd/yyyy"
.Columns("E").NumberFormat = "hh:mm AM/PM"
.Columns("C:G").AutoFit
newRow.Columns("E").Select
End With
With this setup we can copy all the formats from the row above except NumberFormats by using Range().FillDown
.
newRow.FillDown
Alternately, we could copy all the formats from the row above like this:
newRow.Offset(-1).Copy
newRow.PasteSpecial Paste:=xlPasteFormats
Application.CutCopyMode = False
Your code is pretty solid but if you truly want to learn Excel VBA I would start by watching this series: Excel VBA Introduction.
You should also use Rubberduck VBA. It is a free add-in with many features to help you write better code. The code formatter alone is invaluable.
Last but not least start answering questions on StackOverflow. This will give you exposure to questions that you would never think to ask and solutions that you didn't know where possible.
Another With
statement would make the code easier to read.
With Sheets("Pressure Log")
lastRow = .Range("B" & .Rows.Count).End(xlUp).Row 'populate next row with date/time
.Range("B" & lastRow + 1 & ":G" & lastRow + 1).Borders.LineStyle = xlContinuous
With .Range("B" & lastRow)
.Offset(1) = Format(thisDate, "dddd")
.Offset(1, 1) = Format(thisDate, "mm/dd/yyyy")
.Offset(1, 2) = Format(thisDate, "hh:mm AM/PM")
.Offset(1, 3).Select 'position for data
End With
End With
It is better to format entire columns and rows. These reduces the file size by combining multiple css rules.
There are many way to skin this cat. For variety I set a range variable ( newRow ) to the next empty row. This allows me to uses ranges relative to the new row.
Dim newRow As Range
With Worksheets("Pressure Log")
Set newRow = .Range("B" & .Rows.Count).End(xlUp).Offset(1).EntireRow
newRow.Range("B1:G1").Borders.LineStyle = xlContinuous
newRow.Range("B1:D1").Value = Now
.Columns("C").NumberFormat = "ddd"
.Columns("D").NumberFormat = "mm/dd/yyyy"
.Columns("E").NumberFormat = "hh:mm AM/PM"
.Columns("C:G").AutoFit
newRow.Columns("E").Select
End With
With this setup we can copy all the formats from the row above except NumberFormats by using Range().FillDown
.
newRow.FillDown
Alternately, we could copy all the formats from the row above like this:
newRow.Offset(-1).Copy
newRow.PasteSpecial Paste:=xlPasteFormats
Application.CutCopyMode = False
Your code is pretty solid but if you truly want to learn Excel VBA I would start by watching this series: Excel VBA Introduction.
You should also use Rubberduck VBA. It is a free add-in with many features to help you write better code. The code formatter alone is invaluable.
Last but not least start answering questions on StackOverflow. This will give you exposure to questions that you would never think to ask and solutions that you didn't know where possible.
answered 4 hours ago
TinMan
99519
99519
1
Advice on the last suggestion ("Last but not least start answering questions on StackOverflow.") - while providing good exposure it will also open you up to criticism, especially as you learn how to provide a good answer. A thick skin and not taking anything as personal helps weather the early attempts!
– AJD
3 hours ago
@AJD I agree. I have my share of callouses. But I improved more in the 2 years of SO than I did in the previous 6 years.
– TinMan
2 hours ago
add a comment |
1
Advice on the last suggestion ("Last but not least start answering questions on StackOverflow.") - while providing good exposure it will also open you up to criticism, especially as you learn how to provide a good answer. A thick skin and not taking anything as personal helps weather the early attempts!
– AJD
3 hours ago
@AJD I agree. I have my share of callouses. But I improved more in the 2 years of SO than I did in the previous 6 years.
– TinMan
2 hours ago
1
1
Advice on the last suggestion ("Last but not least start answering questions on StackOverflow.") - while providing good exposure it will also open you up to criticism, especially as you learn how to provide a good answer. A thick skin and not taking anything as personal helps weather the early attempts!
– AJD
3 hours ago
Advice on the last suggestion ("Last but not least start answering questions on StackOverflow.") - while providing good exposure it will also open you up to criticism, especially as you learn how to provide a good answer. A thick skin and not taking anything as personal helps weather the early attempts!
– AJD
3 hours ago
@AJD I agree. I have my share of callouses. But I improved more in the 2 years of SO than I did in the previous 6 years.
– TinMan
2 hours ago
@AJD I agree. I have my share of callouses. But I improved more in the 2 years of SO than I did in the previous 6 years.
– TinMan
2 hours ago
add a comment |
2
Cross post from: stackoverflow.com/questions/53683704/excel-range-less-clumsy
– Vogel612♦
16 hours ago
Welcome to Code Review! The current question title, which states your concerns about the code, is too general to be useful here. Please edit to the site standard, which is for the title to simply state the task accomplished by the code. Please see How to get the best value out of Code Review: Asking Questions for guidance on writing good question titles.
– Toby Speight
56 mins ago