Format copying not working

rossross

New Member
Joined
Apr 11, 2022
Messages
39
Office Version
  1. 365
Platform
  1. Windows
This is my full code

VBA Code:
Sub StripData()

Call InsertData
Call FormatData
'Call DragData

End Sub

Sub InsertData()

Dim myWb As Workbook
Dim myRowsToCopy As Range

Set wbtemplate = Workbooks("Template.xlsm").Worksheets("Raw")
Set formatrange = Range("B2:CE2")
Set formularange = Range("AK2:CE2")
Set myWb = Workbooks.Open("C:\Users\me\Desktop\Practice\Data_Pull_Dummy.xlsx")

With myWb.Sheets("Data").Range("A:XFD")
    Set myRowsToCopy = Range(.Cells(.Rows.Count, 1).End(xlUp), .Cells(2, .Columns.Count))
End With

myRowsToCopy.Copy
wbtemplate.Range("3:3").Insert Shift:=xlDown
Application.CutCopyMode = False


End Sub

Sub FormatData()

Dim wbtemplate As Workbook
Set template = Workbooks("Template.xlsm").Worksheets("Raw")
template.Range("B2:CE2").Copy Destination:=Sheets("Raw").Range("B" & Rows.Count).End(xlUp).Offset(-7)

End Sub



Essentially, I'm pulling in data and inserting it. Then I need to copy the format down from the top row to the bottom row. Cannot get it to work correctly.

Any help at all please
 
Maybe try something like this:
VBA Code:
Sub FormatData()

Dim wstemplate As Worksheet
Dim lr As Long

Set wstemplate = Workbooks("Template.xlsm").Worksheets("Raw")
lr = wstemplate.Cells(Rows.Count, "B").End(xlUp).Row

wstemplate.Range("B2:CE2").Copy
wstemplate.Range("B3:CE" & lr).PasteSpecial Paste:=xlPasteFormats

End Sub

Yes! This almost hits the nail on the head only issue is it went down 2 too many rows. Do I add offset(-2) behind the last row? adding it in the last lr = bit gives me an object error.
 
Upvote 0

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
Just subtract 2 from the lr calculation, i.e.
Rich (BB code):
lr = wstemplate.Cells(Rows.Count, "B").End(xlUp).Row - 2
 
Upvote 0
Solution
Just subtract 2 from the lr calculation, i.e.
Rich (BB code):
lr = wstemplate.Cells(Rows.Count, "B").End(xlUp).Row - 2
Okay I just realized it actually dropped the formats down to row 31,961 which is way past where I needed it to be. Should've stopped at 1956 in this instance. But we're close, and I appreciate this!
 
Upvote 0
See if this fixes it:
VBA Code:
lr = wstemplate.Cells(wstemplate.Rows.Count, "B").End(xlUp).Row - 2

If not, is there something in column down by row 31,961 or 31,963?
Are you pre-formatting or something all the way down?

Could maybe also try going from the top down, i.e.
VBA Code:
lr = wstemplate.Range("B2").End(xlDown).Row
 
Last edited:
Upvote 0
See if this fixes it:
VBA Code:
lr = wstemplate.Cells(wstemplate.Rows.Count, "B").End(xlUp).Row[B] - 2[/B]

If not, is there something in column down by row 31,961 or 31,963?
Are you pre-formatting or something all the way down?

Could maybe also try going from the top down, i.e.
VBA Code:
lr = wstemplate.Range("B2").End(xlDown).Row
That second one results in same issue and for some reason it's not letting me insert so those brackets [ ] or i'm just doing it wrong. again, i'm brand new to this. And no, no data down there. ctrl + end puts me right at the end of my data where it should be.
 
Upvote 0
That second one results in same issue and for some reason it's not letting me insert so those brackets [ ] or i'm just doing it wrong. again, i'm brand new to this. And no, no data down there. ctrl + end puts me right at the end of my data where it should be.
Sorry, those "B"s were not supposed to be there.
I tried adding bold formatting to my VBA code, and you cannot do that.
I went back and updated the reply.
Can you try that one again?
 
Upvote 0
Son of a gun.. I changed the -2 to -7 and then i realized you had the paste range as "B3: CE" instead of CE3" and i changed that and now it works!
 
Upvote 0
Son of a gun.. I changed the -2 to -7 and then i realized you had the paste range as "B3: CE" instead of CE3" and i changed that and now it works!
Well, that solves that mystery! Yes, since we are calculating the ending row number, we want to totally remove what was there before.
This is why I usually recommend using Copy/Paste, so we can be sure that we are comparing apples-to-apples, and remove typos from the possibility of differences in what we are talking about.
 
Upvote 0
Well, that solves that mystery! Yes, since we are calculating the ending row number, we want to totally remove what was there before.
This is why I usually recommend using Copy/Paste, so we can be sure that we are comparing apples-to-apples, and remove typos from the possibility of differences in what we are talking about.
I wanted to get the hang on actually writing it, but yes, that would've been the better practice. I did the same for the formulas, and that works as well. You have no idea how much of a help you have been.
 
Upvote 0
Excellent!
I am just glad that we got it all sorted out.
 
Upvote 0

Forum statistics

Threads
1,223,214
Messages
6,170,772
Members
452,353
Latest member
strainu

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top