Adjusting Column Height/Width using VBA

JP801

New Member
Joined
Oct 10, 2023
Messages
7
Office Version
  1. 365
Platform
  1. Windows
Hey everyone!

I am running into an issue pasting a section from one sheet to another and the column height/width is not adjusting accordingly. The section pops up when I press a specific radio button but it's pretty spread out since the columns aren't adjusting. I have attached some photos to give you an idea. Section1 is the form the end user will be using. The Mileage sheet is where the data is coming from and what I want it to look like on Sheet1.

I must note, I am on my 4th day using VBA and have no clue what I am doing. Any help would be appreciated!

This is what my code currently looks like:
VBA Code:
Private Sub OptAdd_Click()
Sheet2.UsedRange.Copy
Sheet1.Range("C11").PasteSpecial xlPasteAll
End Sub

Private Sub OptEdit_Click()
Sheet3.UsedRange.Copy
Sheet1.Range("C11").PasteSpecial xlPasteAll
End Sub
 

Attachments

  • vba1.png
    vba1.png
    60.2 KB · Views: 6
  • vba2.png
    vba2.png
    38.9 KB · Views: 6

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
You could try a paste special, column widths? Unfortunately, there is no such option to paste row heights. You'll have to traverse through all rows and set their heights.
 
Upvote 0
You could try a paste special, column widths? Unfortunately, there is no such option to paste row heights. You'll have to traverse through all rows and set their heights.
That worked perfect for the widths. Is there a specific set of codes you recommend to adjust the heights?
 
Upvote 0
Something like this?
VBA Code:
Private Sub OptAdd_Click()
    Dim rw As Range
    Sheet2.UsedRange.Copy
    Sheet1.Range("C11").PasteSpecial xlPasteAll
    Sheet1.Range("C11").PasteSpecial xlPasteColumnWidths
    For Each rw In Sheet2.UsedRange.Columns(1).Cells
      Sheet1.Range(rw.Address).Offset(10,2).EntireRow.RowHeight = rw.EntireRow.RowHeight
    Next
End Sub
The .Offset(10,2) is there because apparently you are not pasting in A1.
 
Upvote 1
Solution
Something like this?
VBA Code:
Private Sub OptAdd_Click()
    Dim rw As Range
    Sheet2.UsedRange.Copy
    Sheet1.Range("C11").PasteSpecial xlPasteAll
    Sheet1.Range("C11").PasteSpecial xlPasteColumnWidths
    For Each rw In Sheet2.UsedRange.Columns(1).Cells
      Sheet1.Range(rw.Address).Offset(10,2).EntireRow.RowHeight = rw.EntireRow.RowHeight
    Next
End Sub
The .Offset(10,2) is there because apparently you are not pasting in A1.
Thank you! This did the job!
 
Upvote 0

Forum statistics

Threads
1,215,069
Messages
6,122,954
Members
449,095
Latest member
nmaske

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