VBA Magician required

mikemcbain

Board Regular
Joined
Nov 14, 2005
Messages
152
Office Version
  1. 365
Platform
  1. Windows
I use Excel for Office 365 in Windows 10.

I have a spreadsheet called C:/Desktop/tkxl_f.xlsm which can have from two to twenty five sheets each with different and variable names and always located between a sheet called Blank and a sheet called Cover.

I wish to copy and paste the range A1:K133 from each of these sheets onto a worksheet called Today in another spreadsheet called C:/Desktop/20190101_2019.xlsm one below the other.

Is it possible or am I asking too much? My macro skills are limited to recording simple stuff.

In anticipation

Mike
Tasmania.
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
This assumes that both workbooks are closed and the code is in a third workbook that is open. (or in your personal.xlsb)
I don't think that your path to the desktop is right, if that is where the workbooks are saved and not in a Folder named "Desktop" on the C Drive.
To get your path to the desktop, you can use this.
Code:
Sub ABCDE()
MsgBox Environ("USERPROFILE") & "\Desktop"
End Sub
or you can change the two lines in question to
Set wbFrom = Workbooks.Open(CreateObject("WScript.Shell").specialfolders("Desktop") & "\tkxl_f.xlsm")
Set wbTo = Workbooks.Open(CreateObject("WScript.Shell").specialfolders("Desktop") & "\20190101_2019.xlsm")


Code:
Sub Maybe()
Dim wbFrom As Workbook, wbTo As Workbook, sh1 As Worksheet, i As Long
Set wbFrom = Workbooks.Open("C:/Desktop/tkxl_f.xlsm")
Set wbTo = Workbooks.Open("C:/Desktop/20190101_2019.xlsm")
Set sh1 = wbTo.Sheets("Today")
    For i = wbFrom.Sheets("Blank").Index + 1 To wbFrom.Sheets("Cover").Index - 1
        With sh1
            .Cells(.Rows.Count, 1).End(xlUp).Offset(1).Resize(133, 11).Value = wbFrom.Sheets(i).Range("A1:K133").Value
        End With
    Next i
wbFrom.Close False
wbTo.Close True
End Sub
 
Upvote 0
jolivanes

You are a true magician!
Once I corrected my Desktop location your simple VBA worked an absolute treat.
I am so grateful and can't wait to exploit your work which will save me much time and angst.

Mike.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,178
Members
453,021
Latest member
Justyna P

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