-
Feb 13th, 2021, 04:30 PM
#1
Thread Starter
Member
Excel not formatting when creating new row
Any excel experts that can point me in the right direction?
Code below from a WinForm that creates an excel spreadsheet.
Due to user input for an invoice, more lines sometimes have to be added.
It works fine and as expected apart from the cell borders for that row which arent copied.
Colors and functions are copied.
Whats the correct way of doing it?
Code:
If tmpRowStart > 1 Then
oSheet.Rows("2" & tmpRowStart + 1).EntireRow.Copy
oSheet.Rows("2" & tmpRowStart + 1).EntireRow.Insert(-4122)
End If
-
Feb 13th, 2021, 11:09 PM
#2
Re: Excel not formatting when creating new row
i am not sure that -4122 is a correct value for a valid constant
other than that it appears to work as desired, though it also copies any cell values as well as formatting
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Feb 13th, 2021, 11:57 PM
#3
Thread Starter
Member
Re: Excel not formatting when creating new row
I got the constant from https://docs.microsoft.com/en-us/off...el.xlpastetype but to be fair, nothing changes with or without so wondering if I have formatted the coding wrong
-
Feb 14th, 2021, 02:00 AM
#4
Re: Excel not formatting when creating new row
insert constants are different from paste constants
xlshiftdown should b the constant used, but for an entire row insert it is irrelevant as an entirerow can only go down
i had done a simple test here and appeared to work correctly, inserting an entire row from a given row range
i just noticed that you are concatenating the row instead of adding, so if tmpRowStart =2 you would likely be working with row 22 +1 instead of 4 +1
Code:
Dim r As excel.Range
r = Rows(2 + tmpRowStart + 1)
r.Copy
r.EntireRow.Insert xlShiftDown ' -4121
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Feb 14th, 2021, 06:28 AM
#5
Re: Excel not formatting when creating new row
it depends how is formatted the row you copy. It may appear that the original row has line up and bottom but it may be not the case: it may be that only the upper line is set and not the bottom on several rows so it appear that you have up and bottom lines but is fact no. You just have top lines. So when you copy the row, the result is only the up line is set and not the bottom
The best friend of any programmer is a search engine
"Don't wish it was easier, wish you were better. Don't wish for less problems, wish for more skills. Don't wish for less challenges, wish for more wisdom" (J. Rohn)
“They did not know it was impossible so they did it” (Mark Twain)
-
Feb 14th, 2021, 02:18 PM
#6
Thread Starter
Member
Re: Excel not formatting when creating new row
Originally Posted by westconn1
i just noticed that you are concatenating the row instead of adding, so if tmpRowStart =2 you would likely be working with row 22 +1 instead of 4 +1
Correct - working with 2x + 1
All other formatting and formulas work, so Im happy Im working with the right row and its inserting that row correctly. Just not the borders.
The borders for that cells are being copied are outline, all sides. It might be I just have to try and write the cell border after the fact
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|