I was wondering if someone could help out with some code. I'm doing a project at the moment and having difficulty starting it!!!!! (Newbie!) Basically i've got a blank workbook, with a button in it. When this button is clicked i want it to open up a dialog box so i can choose an excel file, and i want it then to copy the contents of that excel file and copy it to the workbook which contains the button.
Any help would be much appreciated as im very new to this and still at the early learning stages!
ok so ive pretty much got it working, here's my code:
Sub CopyRanges()
Dim wb As Workbook
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
ws.UsedRange.ClearContents
Next ws
fName = Application.GetOpenFilename("*,*.xls", , "Please select file to open")
If wb Is Nothing Then
Set wb = Workbooks.Open(fName)
End If
wb.ActiveSheet.Range("A1:Z1000").Copy
ThisWorkbook.ActiveSheet.Range("A5:Z200").PasteSpecial Paste:=xlValues
wb.Close
End Sub
Theres one thing that i want to change but not sure how to exactly. When it pastes in everything, i want cell values, formatting etc to be pasted also, so if a cell has a border its pasted in, how do i go about doing this?
cheers that worked perfectly, one other thing i forgot to mention, when pasting in, how can i get it to just paste in values, some cells use formulas to get there values, i dont wont to copy this in, just the value thats currently in the cell
Columns h and j are the ones that get their values from formulas, every other value is directly inputted
so is there no way to paste everything as their values without specifying exactly which ones you want to do? like the last adjustment you made to my piece for pasting in formatting etc., is there nothing i can add to that to say paste in values only?
I attached a sample file. The file i'll be importing will change slightly everytime (number of students and courses might change between courses or years) so therefore i cant give an exact range when importing the file (which is why i import a1:z200, just to be sure i get everything) from my file.
The column m values are gotten from a formula, but when my macro pastes this into the workbook im working on it gives me formula errors for column m's, which i why i want to just input the values. The formulas aren't needed when i paste them in, the original values suffice.
Is this what you are trying? If not then do let me know...
Code:
'~~> Paste with All formatting
ThisWorkbook.ActiveSheet.Range("A5:Z200").PasteSpecial Paste:=xlPasteAll, _
Operation:=xlNone, SkipBlanks:=False, Transpose:=False
'~~> Convert Column A to L into Values
ThisWorkbook.ActiveSheet.Columns("A:L").PasteSpecial Paste:=xlPasteValues, _
Operation:=xlNone, SkipBlanks:=False, Transpose:=False
'~~> Convert Column N to Z into Values. this will ensure
'~~> that Column M will retain formulas
ThisWorkbook.ActiveSheet.Columns("N:Z").PasteSpecial Paste:=xlPasteValues, _
Operation:=xlNone, SkipBlanks:=False, Transpose:=False
A good exercise for the Heart is to bend down and help another up...
Please Mark your Thread "Resolved", if the query is solved
MyGear:
★ CPU ★ Ryzen 5 5800X
★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
★ Keyboard ★ TVS Electronics Gold Keyboard
★ Mouse ★ Logitech G502 Hero